|
| 1 | +package client |
| 2 | + |
| 3 | +import TestUtil.Companion.mockPrivateProperty |
| 4 | +import okhttp3.Headers.Companion.toHeaders |
| 5 | +import okhttp3.ResponseBody |
| 6 | +import org.jaqpot.client.JaqpotApiClient |
| 7 | +import org.jaqpot.exception.JaqpotSDKException |
| 8 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 9 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 10 | +import org.junit.jupiter.api.BeforeEach |
| 11 | +import org.junit.jupiter.api.Test |
| 12 | +import org.junit.jupiter.api.assertThrows |
| 13 | +import org.mockito.Mockito.* |
| 14 | +import org.openapitools.client.api.DatasetApi |
| 15 | +import org.openapitools.client.api.ModelApi |
| 16 | +import org.openapitools.client.model.Dataset |
| 17 | +import retrofit2.Call |
| 18 | +import retrofit2.Response |
| 19 | + |
| 20 | + |
| 21 | +class JaqpotApiClientTest { |
| 22 | + |
| 23 | + private lateinit var modelApi: ModelApi |
| 24 | + |
| 25 | + private lateinit var datasetApi: DatasetApi |
| 26 | + |
| 27 | + private lateinit var jaqpotApiClient: JaqpotApiClient |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + fun setUp() { |
| 31 | + modelApi = mock(ModelApi::class.java) |
| 32 | + datasetApi = mock(DatasetApi::class.java) |
| 33 | + jaqpotApiClient = JaqpotApiClient("apiKey", "apiSecret") |
| 34 | + mockPrivateProperty(jaqpotApiClient, "modelApi", modelApi) |
| 35 | + mockPrivateProperty(jaqpotApiClient, "datasetApi", datasetApi) |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `predictSync should return dataset on success`() { |
| 41 | + val dataset = Dataset().apply { |
| 42 | + id = 1L |
| 43 | + status = Dataset.StatusEnum.SUCCESS |
| 44 | + } |
| 45 | + |
| 46 | + val mockCall = mock(Call::class.java) as Call<Void> |
| 47 | + val mockResponse = mock(Response::class.java) as Response<Void> |
| 48 | + val mockDatasetCall = mock(Call::class.java) as Call<Dataset> |
| 49 | + val mockDatasetResponse = mock(Response::class.java) as Response<Dataset> |
| 50 | + |
| 51 | + `when`(modelApi.predictWithModel(anyLong(), any(Dataset::class.java))).thenReturn(mockCall) |
| 52 | + `when`(mockCall.execute()).thenReturn(mockResponse) |
| 53 | + `when`(mockResponse.isSuccessful).thenReturn(true) |
| 54 | + `when`(mockResponse.headers()).thenReturn(mapOf("Location" to "/datasets/1").toHeaders()) |
| 55 | + `when`(datasetApi.getDatasetById(anyLong())).thenReturn(mockDatasetCall) |
| 56 | + `when`(mockDatasetCall.execute()).thenReturn(mockDatasetResponse) |
| 57 | + `when`(mockDatasetResponse.body()).thenReturn(dataset) |
| 58 | + |
| 59 | + val result = jaqpotApiClient.predictSync(1L, listOf()) |
| 60 | + |
| 61 | + assertEquals(dataset, result) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `predictSync should throw exception on failure`() { |
| 66 | + val mockCall = mock(Call::class.java) as Call<Void> |
| 67 | + val mockResponse = mock(Response::class.java) as Response<Void> |
| 68 | + |
| 69 | + `when`(modelApi.predictWithModel(anyLong(), any(Dataset::class.java))).thenReturn(mockCall) |
| 70 | + `when`(mockCall.execute()).thenReturn(mockResponse) |
| 71 | + `when`(mockResponse.isSuccessful).thenReturn(false) |
| 72 | + `when`(mockResponse.errorBody()).thenReturn(mock(ResponseBody::class.java)) |
| 73 | + |
| 74 | + val exception = assertThrows<JaqpotSDKException> { |
| 75 | + jaqpotApiClient.predictSync(1L, listOf()) |
| 76 | + } |
| 77 | + |
| 78 | + assertTrue(exception.message!!.contains("Prediction failed")) |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `predictSync should throw exception on dataset failure`() { |
| 83 | + val dataset = Dataset().apply { |
| 84 | + id = 1L |
| 85 | + status = Dataset.StatusEnum.FAILURE |
| 86 | + failureReason = "Some failure reason" |
| 87 | + } |
| 88 | + |
| 89 | + val mockCall = mock(Call::class.java) as Call<Void> |
| 90 | + val mockResponse = mock(Response::class.java) as Response<Void> |
| 91 | + val mockDatasetCall = mock(Call::class.java) as Call<Dataset> |
| 92 | + val mockDatasetResponse = mock(Response::class.java) as Response<Dataset> |
| 93 | + |
| 94 | + `when`(modelApi.predictWithModel(anyLong(), any(Dataset::class.java))).thenReturn(mockCall) |
| 95 | + `when`(mockCall.execute()).thenReturn(mockResponse) |
| 96 | + `when`(mockResponse.isSuccessful).thenReturn(true) |
| 97 | + `when`(mockResponse.headers()).thenReturn(mapOf("Location" to "/datasets/1").toHeaders()) |
| 98 | + `when`(datasetApi.getDatasetById(anyLong())).thenReturn(mockDatasetCall) |
| 99 | + `when`(mockDatasetCall.execute()).thenReturn(mockDatasetResponse) |
| 100 | + `when`(mockDatasetResponse.body()).thenReturn(dataset) |
| 101 | + |
| 102 | + val exception = assertThrows<JaqpotSDKException> { |
| 103 | + jaqpotApiClient.predictSync(1L, listOf()) |
| 104 | + } |
| 105 | + |
| 106 | + assertTrue(exception.message!!.contains("Prediction failed: Some failure reason")) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun `predictSync should throw exception on maximum retries reached`() { |
| 111 | + val dataset = Dataset().apply { |
| 112 | + id = 1L |
| 113 | + status = Dataset.StatusEnum.EXECUTING |
| 114 | + } |
| 115 | + |
| 116 | + val mockCall = mock(Call::class.java) as Call<Void> |
| 117 | + val mockResponse = mock(Response::class.java) as Response<Void> |
| 118 | + val mockDatasetCall = mock(Call::class.java) as Call<Dataset> |
| 119 | + val mockDatasetResponse = mock(Response::class.java) as Response<Dataset> |
| 120 | + |
| 121 | + `when`(modelApi.predictWithModel(anyLong(), any(Dataset::class.java))).thenReturn(mockCall) |
| 122 | + `when`(mockCall.execute()).thenReturn(mockResponse) |
| 123 | + `when`(mockResponse.isSuccessful).thenReturn(true) |
| 124 | + `when`(mockResponse.headers()).thenReturn(mapOf("Location" to "/datasets/1").toHeaders()) |
| 125 | + `when`(datasetApi.getDatasetById(anyLong())).thenReturn(mockDatasetCall) |
| 126 | + `when`(mockDatasetCall.execute()).thenReturn(mockDatasetResponse) |
| 127 | + `when`(mockDatasetResponse.body()).thenReturn(dataset) |
| 128 | + |
| 129 | + val exception = assertThrows<JaqpotSDKException> { |
| 130 | + jaqpotApiClient.predictSync(1L, listOf()) |
| 131 | + } |
| 132 | + |
| 133 | + assertTrue(exception.message!!.contains("Maximum amount of retries reached")) |
| 134 | + } |
| 135 | +} |
0 commit comments