|
| 1 | +package com.exadel.frs.core.trainservice.service; |
| 2 | + |
| 3 | +import static com.exadel.frs.core.trainservice.system.global.Constants.PREDICTION_COUNT; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | +import com.exadel.frs.commonservice.exception.IncorrectPredictionCountException; |
| 10 | +import com.exadel.frs.commonservice.repository.EmbeddingRepository; |
| 11 | +import com.exadel.frs.commonservice.sdk.faces.FacesApiClient; |
| 12 | +import com.exadel.frs.core.trainservice.DbHelper; |
| 13 | +import com.exadel.frs.core.trainservice.EmbeddedPostgreSQLTest; |
| 14 | +import com.exadel.frs.core.trainservice.component.FaceClassifierPredictor; |
| 15 | +import com.exadel.frs.core.trainservice.dto.ProcessEmbeddingsParams; |
| 16 | +import com.exadel.frs.core.trainservice.mapper.FacesMapper; |
| 17 | +import com.exadel.frs.core.trainservice.repository.AppRepository; |
| 18 | +import com.exadel.frs.core.trainservice.validation.ImageExtensionValidator; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import org.apache.commons.lang3.tuple.Pair; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.ValueSource; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 28 | +import org.springframework.transaction.annotation.Transactional; |
| 29 | + |
| 30 | +@Transactional |
| 31 | +class EmbeddingsRecognizeProcessServiceImplTest extends EmbeddedPostgreSQLTest { |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private DbHelper dbHelper; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private ImageExtensionValidator imageExtensionValidator; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private FacesMapper facesMapper; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private AppRepository appRepository; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private EmbeddingRepository embeddingRepository; |
| 47 | + |
| 48 | + @MockBean |
| 49 | + private FacesApiClient facesApiClient; |
| 50 | + |
| 51 | + @MockBean |
| 52 | + private FaceClassifierPredictor predictor; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private EmbeddingsRecognizeProcessServiceImpl recognizeProcessService; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void cleanUp() { |
| 59 | + appRepository.deleteAll(); |
| 60 | + appRepository.flush(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void processEmbeddings_TheInputEmbeddingExistsInTheDatabase_ShouldReturnCompleteSimilarity() { |
| 65 | + var model = dbHelper.insertModel(); |
| 66 | + var subject = dbHelper.insertSubject(model, "subject"); |
| 67 | + var embedding = dbHelper.insertEmbeddingNoImg(subject); |
| 68 | + |
| 69 | + var params = ProcessEmbeddingsParams.builder() |
| 70 | + .apiKey(model.getApiKey()) |
| 71 | + .embeddings(new double[][]{embedding.getEmbedding()}) |
| 72 | + .additionalParams(Collections.singletonMap(PREDICTION_COUNT, 1)) |
| 73 | + .build(); |
| 74 | + |
| 75 | + when(predictor.predict(any(), any(), anyInt())).thenReturn(List.of(Pair.of(1.0, "subject"))); |
| 76 | + assertThat(embeddingRepository.findAll()).containsOnly(embedding); |
| 77 | + |
| 78 | + var results = recognizeProcessService.processEmbeddings(params).getResults(); |
| 79 | + |
| 80 | + assertThat(embeddingRepository.findAll()).containsOnly(embedding); |
| 81 | + assertThat(results).isNotEmpty().hasSize(1); |
| 82 | + |
| 83 | + var result = results.get(0); |
| 84 | + |
| 85 | + assertThat(result.getEmbedding()).isEqualTo(embedding.getEmbedding()); |
| 86 | + assertThat(result.getResults()).isNotEmpty().hasSize(1); |
| 87 | + assertThat(result.getResults().get(0).getSimilarity()).isEqualTo(1.0F); |
| 88 | + assertThat(result.getResults().get(0).getSubject()).isEqualTo("subject"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void processEmbeddings_TheInputEmbeddingDoesNotExistInTheDatabase_ShouldNotReturnCompleteSimilarity() { |
| 93 | + var model = dbHelper.insertModel(); |
| 94 | + var subject = dbHelper.insertSubject(model, "subject"); |
| 95 | + var embedding = dbHelper.insertEmbeddingNoImg(subject); |
| 96 | + |
| 97 | + var params = ProcessEmbeddingsParams.builder() |
| 98 | + .apiKey(model.getApiKey()) |
| 99 | + .embeddings(new double[][]{new double[]{7.3, 8.4, 9.5}}) |
| 100 | + .additionalParams(Collections.singletonMap(PREDICTION_COUNT, 1)) |
| 101 | + .build(); |
| 102 | + |
| 103 | + when(predictor.predict(any(), any(), anyInt())).thenReturn(List.of(Pair.of(0.0, "subject"))); |
| 104 | + assertThat(embeddingRepository.findAll()).containsOnly(embedding); |
| 105 | + |
| 106 | + var results = recognizeProcessService.processEmbeddings(params).getResults(); |
| 107 | + |
| 108 | + assertThat(embeddingRepository.findAll()).containsOnly(embedding); |
| 109 | + assertThat(results).isNotEmpty().hasSize(1); |
| 110 | + |
| 111 | + var result = results.get(0); |
| 112 | + |
| 113 | + assertThat(result.getEmbedding()).isNotEqualTo(embedding.getEmbedding()); |
| 114 | + assertThat(result.getResults()).isNotEmpty().hasSize(1); |
| 115 | + assertThat(result.getResults().get(0).getSimilarity()).isEqualTo(0.0F); |
| 116 | + assertThat(result.getResults().get(0).getSubject()).isEqualTo("subject"); |
| 117 | + } |
| 118 | + |
| 119 | + @ParameterizedTest |
| 120 | + @ValueSource(ints = {0, -2}) |
| 121 | + void processEmbeddings_PredictionCountIsIncorrect_ShouldThrowIncorrectPredictionCountException(int predictionCount) { |
| 122 | + var params = ProcessEmbeddingsParams.builder() |
| 123 | + .additionalParams(Collections.singletonMap(PREDICTION_COUNT, predictionCount)) |
| 124 | + .build(); |
| 125 | + |
| 126 | + assertThatThrownBy(() -> recognizeProcessService.processEmbeddings(params)) |
| 127 | + .isInstanceOf(IncorrectPredictionCountException.class); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void processEmbeddings_PredictionCountIsNull_ShouldThrowIncorrectPredictionCountException() { |
| 132 | + var params = ProcessEmbeddingsParams.builder() |
| 133 | + .additionalParams(Collections.singletonMap(PREDICTION_COUNT, null)) |
| 134 | + .build(); |
| 135 | + |
| 136 | + assertThatThrownBy(() -> recognizeProcessService.processEmbeddings(params)) |
| 137 | + .isInstanceOf(IncorrectPredictionCountException.class); |
| 138 | + } |
| 139 | +} |
0 commit comments