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