package dp.kotul.solrapiapplication.service

import dp.kotul.solrapiapplication.util.buildSolrResponse
import org.hamcrest.core.StringContains.containsString
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.test.context.junit4.SpringRunner

const val ERROR_STRING = "testing error"
const val ERROR_RESPONSE = "{\"error\":{\"msg\":\"$ERROR_STRING\"}}"
const val ERROR_FIELD = "error"
const val OK_STRING = "OK"
const val OK_RESPONSE = "{\"response\":{\"msg\":\"$OK_STRING\"}}"

@RunWith(SpringRunner::class)
@SpringBootTest
class ApiResponseServiceTests {

    @Autowired
    lateinit var apiResponseService: ApiResponseService

    @Test
    fun testSelectResponseEntityReturnsErrorProperly() {
        val solrResponse = buildSolrResponse(ERROR_RESPONSE, BAD_REQUEST)
        val formattedResponse = apiResponseService.getSelectResponseEntity(solrResponse)
        Assert.assertThat(formattedResponse.body!!.get(ERROR_FIELD).asText(), containsString(ERROR_STRING))
    }

    @Test
    fun testUpdateResponseEntityReturnsErrorProperly() {
        val solrResponse = buildSolrResponse(ERROR_RESPONSE, BAD_REQUEST)
        val formattedResponse = apiResponseService.getUpdateResponseEntity(solrResponse)
        Assert.assertThat(formattedResponse.body!!.get(ERROR_FIELD).asText(), containsString(ERROR_STRING))
    }
}
