Commit a454d60b authored by dietricf's avatar dietricf
Browse files

SI-731 correction with plz list mapping. Asserted by unit tests.

parent 9bbc8cea
...@@ -45,6 +45,7 @@ public interface DistributionGroupMemberMapper { ...@@ -45,6 +45,7 @@ public interface DistributionGroupMemberMapper {
.split(",")); .split(","));
targetDto.setPostcodeList(result.stream() targetDto.setPostcodeList(result.stream()
.filter( x -> !x.isEmpty() )
.sorted( String::compareTo ) .sorted( String::compareTo )
.collect(Collectors.toList()) .collect(Collectors.toList())
); );
...@@ -61,6 +62,7 @@ public interface DistributionGroupMemberMapper { ...@@ -61,6 +62,7 @@ public interface DistributionGroupMemberMapper {
default void mapPostcodesFromList(DistributionGroupMemberDto srcDto, @MappingTarget TblDistributionGroupMember targetTbl) { default void mapPostcodesFromList(DistributionGroupMemberDto srcDto, @MappingTarget TblDistributionGroupMember targetTbl) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Optional.ofNullable(srcDto.getPostcodeList()).orElse( new ArrayList<>()).stream() Optional.ofNullable(srcDto.getPostcodeList()).orElse( new ArrayList<>()).stream()
.filter( x -> x!=null && !x.isEmpty() )
.sorted(String::compareTo) .sorted(String::compareTo)
.forEach(x -> sb.append("," + x)); .forEach(x -> sb.append("," + x));
targetTbl.setPostcodes( targetTbl.setPostcodes(
......
...@@ -18,6 +18,7 @@ import org.eclipse.openk.gridfailureinformation.api.ContactApi; ...@@ -18,6 +18,7 @@ import org.eclipse.openk.gridfailureinformation.api.ContactApi;
import org.eclipse.openk.gridfailureinformation.config.TestConfiguration; import org.eclipse.openk.gridfailureinformation.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException; import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.exceptions.OperationDeniedException; import org.eclipse.openk.gridfailureinformation.exceptions.OperationDeniedException;
import org.eclipse.openk.gridfailureinformation.mapper.DistributionGroupMemberMapper;
import org.eclipse.openk.gridfailureinformation.model.TblDistributionGroup; import org.eclipse.openk.gridfailureinformation.model.TblDistributionGroup;
import org.eclipse.openk.gridfailureinformation.model.TblDistributionGroupMember; import org.eclipse.openk.gridfailureinformation.model.TblDistributionGroupMember;
import org.eclipse.openk.gridfailureinformation.repository.DistributionGroupMemberRepository; import org.eclipse.openk.gridfailureinformation.repository.DistributionGroupMemberRepository;
...@@ -36,6 +37,7 @@ import org.springframework.http.ResponseEntity; ...@@ -36,6 +37,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
...@@ -54,11 +56,14 @@ import static org.mockito.Mockito.when; ...@@ -54,11 +56,14 @@ import static org.mockito.Mockito.when;
@DataJpaTest @DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class}) @ContextConfiguration(classes = {TestConfiguration.class})
public class DistributionGroupServiceMemberTest { class DistributionGroupServiceMemberTest {
@Qualifier("myDistributionGroupMemberService") @Qualifier("myDistributionGroupMemberService")
@Autowired @Autowired
private DistributionGroupMemberService distributionGroupMemberService; private DistributionGroupMemberService distributionGroupMemberService;
@Autowired
private DistributionGroupMemberMapper distributionGroupMemberMapper;
@MockBean @MockBean
private DistributionGroupMemberRepository distributionGroupMemberRepository; private DistributionGroupMemberRepository distributionGroupMemberRepository;
...@@ -69,7 +74,7 @@ public class DistributionGroupServiceMemberTest { ...@@ -69,7 +74,7 @@ public class DistributionGroupServiceMemberTest {
private ContactApi contactApi; private ContactApi contactApi;
@Test @Test
public void shouldGetAllDistributionGroupMembers() { void shouldGetAllDistributionGroupMembers() {
List<TblDistributionGroupMember> groupMembers = MockDataHelper.mockDistributionGroupMemberList(); List<TblDistributionGroupMember> groupMembers = MockDataHelper.mockDistributionGroupMemberList();
when(distributionGroupMemberRepository.findAll()).thenReturn(groupMembers); when(distributionGroupMemberRepository.findAll()).thenReturn(groupMembers);
List<DistributionGroupMemberDto> groupMemberDtoList = distributionGroupMemberService.getDistributionGroupMembers(); List<DistributionGroupMemberDto> groupMemberDtoList = distributionGroupMemberService.getDistributionGroupMembers();
...@@ -81,7 +86,7 @@ public class DistributionGroupServiceMemberTest { ...@@ -81,7 +86,7 @@ public class DistributionGroupServiceMemberTest {
@Test @Test
@WithMockUser(username = "admin", authorities = { "ADMIN", "USER" }) @WithMockUser(username = "admin", authorities = { "ADMIN", "USER" })
public void shouldGetAllMembersOfASingleDistributionGroup() { void shouldGetAllMembersOfASingleDistributionGroup() {
List<TblDistributionGroupMember> groupMembers = MockDataHelper.mockDistributionGroupMemberList(); List<TblDistributionGroupMember> groupMembers = MockDataHelper.mockDistributionGroupMemberList();
when(distributionGroupMemberRepository.findByTblDistributionGroupUuid(any(UUID.class))).thenReturn(groupMembers); when(distributionGroupMemberRepository.findByTblDistributionGroupUuid(any(UUID.class))).thenReturn(groupMembers);
List<DistributionGroupMemberDto> groupMemberDtoList = distributionGroupMemberService.getMembersByGroupId(groupMembers.get(1).getTblDistributionGroup().getUuid()); List<DistributionGroupMemberDto> groupMemberDtoList = distributionGroupMemberService.getMembersByGroupId(groupMembers.get(1).getTblDistributionGroup().getUuid());
...@@ -91,9 +96,37 @@ public class DistributionGroupServiceMemberTest { ...@@ -91,9 +96,37 @@ public class DistributionGroupServiceMemberTest {
assertEquals(groupMemberDtoList.get(1).getUuid(), groupMembers.get(1).getUuid()); assertEquals(groupMemberDtoList.get(1).getUuid(), groupMembers.get(1).getUuid());
} }
@Test
void shouldMapEmptyPlzListCorrectly() {
TblDistributionGroupMember tbl1 = MockDataHelper.mockTblDistributionGroupMember();
tbl1.setPostcodes(null);
assertTrue(distributionGroupMemberMapper.toDistributionGroupMemberDto(tbl1).getPostcodeList().isEmpty());
TblDistributionGroupMember tbl2 = MockDataHelper.mockTblDistributionGroupMember();
tbl2.setPostcodes("");
assertTrue(distributionGroupMemberMapper.toDistributionGroupMemberDto(tbl2).getPostcodeList().isEmpty());
TblDistributionGroupMember tbl3 = MockDataHelper.mockTblDistributionGroupMember();
tbl3.setPostcodes("5,3,1");
DistributionGroupMemberDto dto3 = distributionGroupMemberMapper.toDistributionGroupMemberDto(tbl3);
assertEquals("1", dto3.getPostcodeList().get(0));
assertEquals("3", dto3.getPostcodeList().get(1));
assertEquals("5", dto3.getPostcodeList().get(2));
List<String> testList = new LinkedList<>();
testList.add(null);
testList.add("");
testList.add("32");
testList.add("23");
dto3.setPostcodeList(testList);
TblDistributionGroupMember tblResult = distributionGroupMemberMapper.toTblDistributionGroupMember(dto3);
assertEquals( "23,32", tblResult.getPostcodes());
}
@Test @Test
@WithMockUser(username = "admin", authorities = { "ADMIN", "USER" }) @WithMockUser(username = "admin", authorities = { "ADMIN", "USER" })
public void shouldFindASingleDistributionGroupMember() { void shouldFindASingleDistributionGroupMember() {
TblDistributionGroupMember groupMember = MockDataHelper.mockTblDistributionGroupMember(); TblDistributionGroupMember groupMember = MockDataHelper.mockTblDistributionGroupMember();
when(distributionGroupMemberRepository.findByUuid(any(UUID.class))).thenReturn(Optional.of(groupMember)); when(distributionGroupMemberRepository.findByUuid(any(UUID.class))).thenReturn(Optional.of(groupMember));
DistributionGroupMemberDto dto = distributionGroupMemberService.getMemberByUuid(groupMember.getTblDistributionGroup().getUuid(), UUID.randomUUID()); DistributionGroupMemberDto dto = distributionGroupMemberService.getMemberByUuid(groupMember.getTblDistributionGroup().getUuid(), UUID.randomUUID());
...@@ -106,7 +139,8 @@ public class DistributionGroupServiceMemberTest { ...@@ -106,7 +139,8 @@ public class DistributionGroupServiceMemberTest {
@Test @Test
void shouldHandleLoadFileWithNotFoundException() { void shouldHandleLoadFileWithNotFoundException() {
when(distributionGroupRepository.findByUuid(any(UUID.class))).thenReturn(Optional.empty()); when(distributionGroupRepository.findByUuid(any(UUID.class))).thenReturn(Optional.empty());
assertThrows( NotFoundException.class, () -> distributionGroupMemberService.handleLoadFile(UUID.randomUUID())); UUID uuid = UUID.randomUUID();
assertThrows( NotFoundException.class, () -> distributionGroupMemberService.handleLoadFile(uuid));
} }
@Test @Test
...@@ -126,15 +160,17 @@ public class DistributionGroupServiceMemberTest { ...@@ -126,15 +160,17 @@ public class DistributionGroupServiceMemberTest {
} }
@Test @Test
public void shouldThrowExceptionWhenFailureNotFound() { void shouldThrowExceptionWhenFailureNotFound() {
TblDistributionGroupMember groupMember = MockDataHelper.mockTblDistributionGroupMember(); TblDistributionGroupMember groupMember = MockDataHelper.mockTblDistributionGroupMember();
when(distributionGroupMemberRepository.findByUuid(any(UUID.class))).thenReturn(Optional.empty()); when(distributionGroupMemberRepository.findByUuid(any(UUID.class))).thenReturn(Optional.empty());
UUID uuid = UUID.randomUUID();
UUID dgMemberUuid = groupMember.getTblDistributionGroup().getUuid();
assertThrows(NotFoundException.class, assertThrows(NotFoundException.class,
() -> distributionGroupMemberService.getMemberByUuid(groupMember.getTblDistributionGroup().getUuid(), UUID.randomUUID())); () -> distributionGroupMemberService.getMemberByUuid(dgMemberUuid, uuid));
} }
@Test @Test
public void shouldInsertADistributionGroupMember() { void shouldInsertADistributionGroupMember() {
DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto(); DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto();
TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember(); TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember();
...@@ -152,7 +188,7 @@ public class DistributionGroupServiceMemberTest { ...@@ -152,7 +188,7 @@ public class DistributionGroupServiceMemberTest {
} }
@Test @Test
public void shouldNotInsertMember_Exception_Member_Exists() { void shouldNotInsertMember_Exception_Member_Exists() {
DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto(); DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto();
TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember(); TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember();
...@@ -161,12 +197,12 @@ public class DistributionGroupServiceMemberTest { ...@@ -161,12 +197,12 @@ public class DistributionGroupServiceMemberTest {
when(distributionGroupRepository.findByUuid(any(UUID.class))).thenReturn(Optional.of(tblDistributionGroup)); when(distributionGroupRepository.findByUuid(any(UUID.class))).thenReturn(Optional.of(tblDistributionGroup));
when(distributionGroupMemberRepository.countByDistributionGroupIdAndContactId(anyLong(),any(UUID.class))).thenReturn(4L); when(distributionGroupMemberRepository.countByDistributionGroupIdAndContactId(anyLong(),any(UUID.class))).thenReturn(4L);
when(distributionGroupMemberRepository.save(any(TblDistributionGroupMember.class))).thenReturn(tblDistributionGroupMember); when(distributionGroupMemberRepository.save(any(TblDistributionGroupMember.class))).thenReturn(tblDistributionGroupMember);
UUID uuid = UUID.randomUUID();
assertThrows(OperationDeniedException.class, () -> distributionGroupMemberService.insertDistributionGroupMember(UUID.randomUUID(), memberDto)); assertThrows(OperationDeniedException.class, () -> distributionGroupMemberService.insertDistributionGroupMember(uuid, memberDto));
} }
@Test @Test
public void shouldDeleteDistributionGroupMember() { void shouldDeleteDistributionGroupMember() {
TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember(); TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember();
TblDistributionGroup tblDistributionGroup = MockDataHelper.mockTblDistributionGroup(); TblDistributionGroup tblDistributionGroup = MockDataHelper.mockTblDistributionGroup();
...@@ -181,7 +217,7 @@ public class DistributionGroupServiceMemberTest { ...@@ -181,7 +217,7 @@ public class DistributionGroupServiceMemberTest {
@Test @Test
public void shouldUpdateDistributionGroupMember() { void shouldUpdateDistributionGroupMember() {
DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto(); DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto();
TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember(); TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember();
TblDistributionGroup tblDistributionGroup = MockDataHelper.mockTblDistributionGroup(); TblDistributionGroup tblDistributionGroup = MockDataHelper.mockTblDistributionGroup();
...@@ -195,7 +231,7 @@ public class DistributionGroupServiceMemberTest { ...@@ -195,7 +231,7 @@ public class DistributionGroupServiceMemberTest {
} }
@Test @Test
public void shouldNotUpdateMember_Exception_GroupNotFound() { void shouldNotUpdateMember_Exception_GroupNotFound() {
DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto(); DistributionGroupMemberDto memberDto = MockDataHelper.mockDistributionGroupMemberDto();
TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember(); TblDistributionGroupMember tblDistributionGroupMember = MockDataHelper.mockTblDistributionGroupMember();
TblDistributionGroup tblDistributionGroup = MockDataHelper.mockTblDistributionGroup(); TblDistributionGroup tblDistributionGroup = MockDataHelper.mockTblDistributionGroup();
...@@ -209,7 +245,8 @@ public class DistributionGroupServiceMemberTest { ...@@ -209,7 +245,8 @@ public class DistributionGroupServiceMemberTest {
Object[] args = invocation.getArguments(); Object[] args = invocation.getArguments();
return (TblDistributionGroupMember) args[0]; return (TblDistributionGroupMember) args[0];
}); });
assertThrows(NotFoundException.class, () -> distributionGroupMemberService.updateGroupMember(UUID.randomUUID(), memberDto)); UUID uuid = UUID.randomUUID();
assertThrows(NotFoundException.class, () -> distributionGroupMemberService.updateGroupMember(uuid, memberDto));
} }
......
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