Commit 7d502481 authored by dietricf's avatar dietricf
Browse files

SI-154 Better Unittesting

parent 485711ec
......@@ -53,7 +53,7 @@ public class JobManagerService {
private void doImport(ImportDataDto importDataDto, ForeignFailureDataDto foreignFailureDataDto) {
private FailureInformationDto doImport(ImportDataDto importDataDto, ForeignFailureDataDto foreignFailureDataDto) {
FailureInformationDto existingDto = failureInformationService.findByObjectReferenceExternalSystem(importDataDto.getAssembledRefId());
FailureInformationDto failureInformationDto;
......@@ -68,8 +68,9 @@ public class JobManagerService {
gfiProcessState = GfiProcessState.NEW;
}
failureInformationService.insertFailureInfo(failureInformationDto, gfiProcessState);
FailureInformationDto retVal = failureInformationService.insertFailureInfo(failureInformationDto, gfiProcessState);
log.info("External failure information [" + importDataDto.getMetaId() + "] from " + importDataDto.getSource() + " imported (inserted).");
return retVal;
}
else {
FailureInformationDto updatedDto = setUpdateFromForeignDto(importDataDto, foreignFailureDataDto, existingDto);
......@@ -81,8 +82,9 @@ public class JobManagerService {
statusService.getStatusFromId(GfiProcessState.UPDATED.getStatusValue()).getUuid()
);
}
failureInformationService.updateFailureInfo(updatedDto);
FailureInformationDto retVal = failureInformationService.updateFailureInfo(updatedDto);
log.info("External failure information [" + importDataDto.getMetaId() + "] from " + importDataDto.getSource() + " imported (updated).");
return retVal;
}
}
......
......@@ -21,6 +21,7 @@ import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.util.ImportDataValidator;
import org.eclipse.openk.gridfailureinformation.viewmodel.*;
import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer;
import org.powermock.reflect.Whitebox;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
......@@ -28,8 +29,11 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
......@@ -218,23 +222,23 @@ public class JobManagerServiceTest {
@Test
public void shouldImportUpdateMessageLeavingStatus() {
ImportDataDto dto = MockDataHelper.mockImportDataDto();
dto.setMessageContent("{\n" +
public void shouldImportUpdateMessageLeavingStatus() throws Exception {
ImportDataDto importDataDto = MockDataHelper.mockImportDataDto();
importDataDto.setMessageContent("{\n" +
" \"branch\": \"S\",\n" + // invalid branch
" \"city\": \"Belfast\",\n" +
" \"description\": \"Schlimmer Fehler im System\",\n" +
" \"district\": \"\",\n" +
" \"failureBegin\": \"2020-11-19T14:13:15.666Z\",\n" +
" \"district\": \"InDaHood\",\n" +
" \"failureBegin\": \"2019-11-19T14:13:15.666Z\",\n" +
" \"housenumber\": \"10b\",\n" +
" \"latitude\": 12.345,\n" +
" \"longitude\": 0,\n" +
" \"longitude\": 44,\n" +
" \"planned\": false,\n" +
" \"postcode\": \"3456\",\n" +
" \"pressureLevel\": null,\n" +
" \"pressureLevel\": \"ND\",\n" +
" \"radiusInMeters\": 678,\n" +
" \"stationDescription\": null,\n" +
" \"stationId\": null,\n" +
" \"stationDescription\": \"Haferbox\",\n" +
" \"stationId\": \"34500\",\n" +
" \"street\": \"Oxfordstreet\",\n" +
" \"voltageLevel\": \"HS\"\n" +
" }");
......@@ -243,17 +247,52 @@ public class JobManagerServiceTest {
branchStromDto.setName("S");
when( branchService.findByName(eq("S"))).thenReturn(branchStromDto);
when( statusService.getStatusFromId( anyLong()) ).thenReturn(MockDataHelper.mockStatusDto());
when( radiusService.getRadii() ).thenReturn(getRadiusDtoList());
when( failureInformationService.findByObjectReferenceExternalSystem(anyString())).thenReturn(MockDataHelper.mockFailureInformationDto());
jobManagerService.validateAndImport(dto);
FailureInformationDto dtoFromDB = MockDataHelper.mockFailureInformationDto();
UUID condensedId = UUID.randomUUID();
dtoFromDB.setFailureInformationCondensedId(condensedId);
dtoFromDB.setCondensedCount(77L);
dtoFromDB.setCondensed(true);
dtoFromDB.setObjectReferenceExternalSystem("666777888");
verify(failureInformationService, times(0))
.insertFailureInfo(any( FailureInformationDto.class), eq(GfiProcessState.NEW));
when( failureInformationService.findByObjectReferenceExternalSystem(anyString())).thenReturn(dtoFromDB);
when(failureInformationService.updateFailureInfo(any(FailureInformationDto.class)))
.then((Answer<FailureInformationDto>) invocation -> {
Object[] args = invocation.getArguments();
return (FailureInformationDto) args[0];
});
ForeignFailureDataDto dtoFromBus = importDataValidator.readSafeForeignFailureInfo(importDataDto);
FailureInformationDto dtoResult = Whitebox.invokeMethod(jobManagerService, "doImport", importDataDto, dtoFromBus);
verify(failureInformationService, times(1))
.updateFailureInfo(any( FailureInformationDto.class));
assertEquals(branchStromDto.getUuid(), dtoResult.getBranchId());
assertEquals("Belfast", dtoResult.getCity());
assertEquals("Schlimmer Fehler im System", dtoResult.getInternalRemark());
assertEquals("InDaHood", dtoResult.getDistrict());
Calendar cal = Calendar.getInstance();
cal.setTime(dtoResult.getFailureBegin());
assertEquals( 2019, cal.get(Calendar.YEAR));
assertEquals( "10b", dtoResult.getHousenumber());
assertTrue( BigDecimal.valueOf(12.345d).equals(dtoResult.getLatitude()) );
assertTrue( BigDecimal.valueOf(44).equals(dtoResult.getLongitude()) );
assertTrue( dtoFromDB.getStatusInternId().equals(dtoResult.getStatusInternId()));
assertEquals( "3456", dtoResult.getPostcode());
assertEquals( "ND", dtoResult.getPressureLevel());
assertEquals( "Haferbox", dtoResult.getStationDescription());
assertEquals( "34500", dtoResult.getStationId());
assertEquals( "Oxfordstreet", dtoResult.getStreet());
assertEquals( "HS", dtoResult.getVoltageLevel());
assertTrue( condensedId.equals(dtoResult.getFailureInformationCondensedId()));
assertEquals( 77L, dtoResult.getCondensedCount() );
assertTrue( dtoResult.getCondensed());
assertEquals( importDataDto.getAssembledRefId(), dtoResult.getObjectReferenceExternalSystem());
assertTrue( dtoFromDB.getStatusExternId().equals(dtoResult.getStatusExternId()));
assertTrue( dtoFromDB.getFailureClassificationId().equals(dtoResult.getFailureClassificationId()));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment