Fix reports mapping (#41)

* Fix mapping

* Add tests
This commit is contained in:
Egor Cherniak 2022-05-04 17:03:34 +03:00 committed by GitHub
parent 671fc5d471
commit 79f71ae7fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 135 additions and 29 deletions

View File

@ -9,8 +9,6 @@ import dev.vality.anapi.v2.security.AccessService;
import dev.vality.anapi.v2.service.ReporterService;
import dev.vality.geck.common.util.TypeUtil;
import dev.vality.anapi.v2.util.DeadlineUtil;
import dev.vality.reporter.ReportRequest;
import dev.vality.reporter.ReportTimeRange;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@ -67,7 +65,7 @@ public class ReportsApiDelegateService implements ReportsApiDelegate {
.partyId(partyID)
.shopIds(shops)
.build());
var request = getReportRequest(partyID, shopID, fromTime, toTime);
var request = statReportRequestConverter.mapToReportRequest(partyID, shopID, fromTime, toTime);
var reportId = reporterService.createReport(request, reportType);
var response = reporterService.getReport(reportId);
log.info("<- Res [201]: xRequestID={}", xRequestID);
@ -135,13 +133,4 @@ public class ReportsApiDelegateService implements ReportsApiDelegate {
return ResponseEntity.ok(response);
}
private ReportRequest getReportRequest(String partyId, String shopId, OffsetDateTime fromTime,
OffsetDateTime toTime) {
return new ReportRequest()
.setPartyId(partyId)
.setShopId(shopId)
.setTimeRange(new ReportTimeRange()
.setFromTime(fromTime.toString())
.setToTime(toTime.toString()));
}
}

View File

@ -7,6 +7,7 @@ import dev.vality.reporter.StatReportRequest;
import org.springframework.stereotype.Component;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
@ -17,28 +18,34 @@ public class ParamsToStatReportRequestConverter {
OffsetDateTime toTime, Integer limit,
List<String> reportTypes, String continuationToken) {
return new StatReportRequest()
.setRequest(
new ReportRequest()
.setPartyId(partyId)
.setShopId(shopId)
.setTimeRange(new ReportTimeRange()
.setFromTime(fromTime.toString())
.setToTime(toTime.toString())))
.setRequest(mapToReportRequest(partyId, shopId, fromTime, toTime))
.setReportTypes(mapReportTypes(reportTypes))
.setLimit(limit)
.setContinuationToken(continuationToken);
}
public ReportRequest mapToReportRequest(String partyId, String shopId, OffsetDateTime fromTime,
OffsetDateTime toTime) {
return new ReportRequest()
.setPartyId(partyId)
.setShopId(shopId)
.setTimeRange(new ReportTimeRange()
.setFromTime(fromTime.format(DateTimeFormatter.ISO_INSTANT))
.setToTime(toTime.format(DateTimeFormatter.ISO_INSTANT)));
}
public String mapReportType(String requestReportType) {
Report.ReportTypeEnum inputType = Report.ReportTypeEnum.fromValue(requestReportType);
return switch (inputType) {
case PAYMENTREGISTRY -> "payment_registry";
case PROVISIONOFSERVICE -> "provision_of_service";
case PAYMENTREGISTRYBYPAYOUT -> "payment_registry_by_payout";
default -> throw new IllegalArgumentException("Unknown report type: " + inputType.getValue());
};
}
private List<String> mapReportTypes(List<String> requestReportTypes) {
return requestReportTypes.stream().map(input -> {
Report.ReportTypeEnum inputType = Report.ReportTypeEnum.fromValue(input);
return switch (inputType) {
case PAYMENTREGISTRY -> "payment_registry";
case PROVISIONOFSERVICE -> "provision_of_service";
case PAYMENTREGISTRYBYPAYOUT -> "payment_registry_by_payout";
default -> throw new IllegalArgumentException("Unknown report type: " + inputType.getValue());
};
}).collect(Collectors.toList());
return requestReportTypes.stream().map(this::mapReportType).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,86 @@
package dev.vality.anapi.v2.converter.reporter.request;
import dev.vality.anapi.v2.model.Report;
import dev.vality.anapi.v2.testutil.RandomUtil;
import dev.vality.geck.common.util.TypeUtil;
import dev.vality.reporter.ReportRequest;
import dev.vality.reporter.StatReportRequest;
import org.junit.jupiter.api.Test;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
class ParamsToStatReportRequestConverterTest {
private static final ParamsToStatReportRequestConverter converter =
new ParamsToStatReportRequestConverter();
@Test
void convert() {
String partyId = RandomUtil.randomString(2);
String shopId = RandomUtil.randomString(2);
int limit = RandomUtil.randomInt(1, 10);
StatReportRequest request = converter.convert(
partyId,
shopId,
OffsetDateTime.MIN,
OffsetDateTime.now(),
limit,
Arrays.stream(Report.ReportTypeEnum.values()).map(Report.ReportTypeEnum::getValue).collect(
Collectors.toList()),
RandomUtil.randomString(5)
);
assertNotNull(request);
assertNotNull(request.getRequest());
assertEquals(partyId, request.getRequest().getPartyId());
assertEquals(shopId, request.getRequest().getShopId());
assertNotNull(request.getRequest().getTimeRange().getFromTime());
assertNotNull(request.getRequest().getTimeRange().getToTime());
String fromTime = request.getRequest().getTimeRange().getFromTime();
String toTime = request.getRequest().getTimeRange().getToTime();
assertDoesNotThrow(() -> TypeUtil.stringToInstant(fromTime));
assertDoesNotThrow(() -> TypeUtil.stringToInstant(toTime));
assertEquals(limit, request.getLimit());
assertEquals(Report.ReportTypeEnum.values().length, request.getReportTypes().size());
assertNotNull(request.getContinuationToken());
}
@Test
void mapToReportRequest() {
String partyId = RandomUtil.randomString(2);
String shopId = RandomUtil.randomString(2);
ReportRequest request = converter.mapToReportRequest(
partyId,
shopId,
OffsetDateTime.MIN,
OffsetDateTime.now()
);
assertNotNull(request);
assertEquals(partyId, request.getPartyId());
assertEquals(shopId, request.getShopId());
assertNotNull(request.getTimeRange().getFromTime());
assertNotNull(request.getTimeRange().getToTime());
String fromTime = request.getTimeRange().getFromTime();
String toTime = request.getTimeRange().getToTime();
assertDoesNotThrow(() -> TypeUtil.stringToInstant(fromTime));
assertDoesNotThrow(() -> TypeUtil.stringToInstant(toTime));
}
@Test
void mapReportType() {
for (Report.ReportTypeEnum type : Report.ReportTypeEnum.values()) {
assertDoesNotThrow(() -> converter.mapReportType(type.getValue()));
}
}
}

View File

@ -0,0 +1,20 @@
package dev.vality.anapi.v2.converter.reporter.response;
import dev.vality.anapi.v2.model.Report;
import dev.vality.anapi.v2.testutil.ReporterUtil;
import dev.vality.reporter.StatReportResponse;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ReporterResponseToReportConverterTest {
private static final ReporterResponseToReportConverter converter = new ReporterResponseToReportConverter();
@Test
void convert() {
StatReportResponse response = ReporterUtil.createSearchReportsResponse();
Report report = converter.convert(response.getReports().get(0));
assertNotNull(report);
}
}

View File

@ -4,6 +4,8 @@ import dev.vality.reporter.Report;
import dev.vality.reporter.StatReportResponse;
import lombok.experimental.UtilityClass;
import java.util.List;
import static dev.vality.anapi.v2.testutil.DamselUtil.fillRequiredTBaseObject;
@UtilityClass
@ -16,6 +18,8 @@ public class ReporterUtil {
}
public static StatReportResponse createSearchReportsResponse() {
return fillRequiredTBaseObject(new StatReportResponse(), StatReportResponse.class);
var statReportResponse = fillRequiredTBaseObject(new StatReportResponse(), StatReportResponse.class);
statReportResponse.setReports(List.of(createReport(RandomUtil.randomInt(1, 100))));
return statReportResponse;
}
}