<commit_before>/*
 * Copyright 2018 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.plaidapp.core.designernews.data.api.comments

import io.plaidapp.core.data.Result
import io.plaidapp.core.data.isSuccessful
import io.plaidapp.core.designernews.data.api.DesignerNewsService
import io.plaidapp.core.designernews.data.api.model.Comment
import kotlinx.coroutines.experimental.CompletableDeferred
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Test
import org.mockito.Mockito
import retrofit2.Response

/**
 * Tests for [DesignerNewsCommentsRepository] with mocked service
 */
class DesignerNewsCommentsRepositoryTest {

    private val service = Mockito.mock(DesignerNewsService::class.java)
    private val dataSource = DesignerNewsCommentsRemoteDataSource(service)
    private val repository = DesignerNewsCommentsRepository(
            dataSource,
            provideFakeCoroutinesContextProvider()
    )

    @Test
    fun getComments_noReplies_whenRequestSuccessful() {
        // Given that the service responds with success
        val apiResult = Response.success(listOf(reply1))
        Mockito.`when`(service.getComments("11")).thenReturn(CompletableDeferred(apiResult))
        var result: Result<List<Comment>?>? = null

        // When getting the replies
        repository.getComments(listOf(11L)) { it -> result = it }

        // Then the correct list of comments was requested from the API
        Mockito.verify(service).getComments("11")
        // Then the correct list is received
        assertEquals(listOf(reply1), (result as Result.Success).data)
    }

    @Test
    fun getComments_noReplies_whenRequestFailed() {
        // Given that the service responds with failure
        val apiResult = Response.error<List<Comment>>(400, errorResponseBody)
        Mockito.`when`(service.getComments("11")).thenReturn(CompletableDeferred(apiResult))
        var result: Result<List<Comment>?>? = null

        // When getting the comments
        repository.getComments(listOf(11L)) { it -> result = it }

        // Then the result is not successful
        assertNotNull(result)
        assertFalse(result!!.isSuccessful())
    }

    @Test<commit_msg>Test updated<commit_after>/*
 * Copyright 2018 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.plaidapp.core.designernews.data.api.comments

import io.plaidapp.core.data.Result
import io.plaidapp.core.data.isSuccessful
import io.plaidapp.core.designernews.data.api.DesignerNewsService
import io.plaidapp.core.designernews.data.api.model.Comment
import kotlinx.coroutines.experimental.CompletableDeferred
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Test
import org.mockito.Mockito
import retrofit2.Response

/**
 * Tests for [DesignerNewsCommentsRepository] with mocked service
 */
class DesignerNewsCommentsRepositoryTest {

    private val service = Mockito.mock(DesignerNewsService::class.java)
    private val dataSource = DesignerNewsCommentsRemoteDataSource(service)
    private val repository = DesignerNewsCommentsRepository(
            dataSource,
            provideFakeCoroutinesContextProvider()
    )

    @Test
    fun getComments_noReplies_whenRequestSuccessful() {
        // Given that the service responds with success
        val apiResult = Response.success(listOf(reply1))
        Mockito.`when`(service.getComments("11")).thenReturn(CompletableDeferred(apiResult))
        var result: Result<List<Comment>?>? = null

        // When getting the replies
        repository.getComments(listOf(11L)) { it -> result = it }

        // Then the correct list of comments was requested from the API
        Mockito.verify(service).getComments("11")
        // Then the correct list is received
        assertEquals(Result.Success(listOf(reply1)), result)
    }

    @Test
    fun getComments_noReplies_whenRequestFailed() {
        // Given that the service responds with failure
        val apiResult = Response.error<List<Comment>>(400, errorResponseBody)
        Mockito.`when`(service.getComments("11")).thenReturn(CompletableDeferred(apiResult))
        var result: Result<List<Comment>?>? = null

        // When getting the comments
        repository.getComments(listOf(11L)) { it -> result = it }

        // Then the result is not successful
        assertNotNull(result)
        assertFalse(result!!.isSuccessful())
    }

    @Test
{'fraction_non_alphanumeric': 0.0884781048097631, 'fraction_numerical': 0.008614501076812634, 'mean_word_length': 3.632585203657523, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5017769', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1480, 'n_words': 424}