add mapper tests

This commit is contained in:
akshat-sonic
2023-02-23 20:24:40 +05:30
parent 4981f6a777
commit 59f30637c7
3 changed files with 39 additions and 6 deletions

View File

@@ -12,19 +12,15 @@ import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class MetricMapper {
private final ExperimentRepository experimentRepository;
private final JacksonUtils jacksonUtils;
public MetricResponse mapMetricEntityToMetricResponse(MetricEntity metricEntity){
long numberOfExperimentsForMetrics = experimentRepository.countByPrimaryMetric(metricEntity.getMetricName());
return MetricResponse.builder()
.metricId(metricEntity.getMetricId())
.createdBy(metricEntity.getCreatedBy())
.metricName(metricEntity.getMetricName())
.metricType(StringUtils.isNotBlank(metricEntity.getMetricType())
? jacksonUtils.stringToObject(metricEntity.getMetricType(), MetricType.class)
? MetricType.valueOf(metricEntity.getMetricType())
: null)
.updatedAt(metricEntity.getUpdatedAt())
.numberOfExperiments(numberOfExperimentsForMetrics)
.build();
}
}

View File

@@ -10,6 +10,7 @@ import com.navi.medici.validator.MetricValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Objects;
import java.util.UUID;
@Slf4j
@@ -26,7 +27,9 @@ public record MetricServiceImpl(MetricRepository metricRepository,
MetricEntity metric = MetricEntity.builder()
.metricId(UUID.randomUUID().toString())
.metricName(createMetricRequest.getMetricName())
.metricType(jacksonUtils.objectToString(createMetricRequest.getMetricType()))
.metricType(Objects.nonNull(createMetricRequest.getMetricType())
? createMetricRequest.getMetricType().toString()
: null)
.createdBy(emailId)
.athenaQuery(createMetricRequest.getAthenaQuery())
.build();

View File

@@ -0,0 +1,34 @@
package com.navi.medici.mapper;
import com.navi.medici.entity.MetricEntity;
import com.navi.medici.enums.MetricType;
import com.navi.medici.response.MetricResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(MockitoExtension.class)
class MetricMapperTest {
@InjectMocks
private MetricMapper metricMapper;
@Test
void mapMetricEntityToMetricResponse() {
MetricEntity metricEntity = MetricEntity.builder()
.metricId("uuid")
.metricType("BUSINESS")
.createdBy("user")
.athenaQuery("athena-query")
.metricName("metric-name")
.build();
MetricResponse response = metricMapper.mapMetricEntityToMetricResponse(metricEntity);
Assertions.assertEquals("uuid", response.getMetricId());
Assertions.assertEquals("BUSINESS", response.getMetricType().toString());
Assertions.assertEquals("user", response.getCreatedBy());
Assertions.assertEquals("metric-name", response.getMetricName());
}
}