MemberService { public void syncMembers() { log.info("> Sync members"); syncLocalMembers(); syncRemoteMembers(); log.info("< Synced members"); }  void syncMembers(); List<Member> groupMembers(String groupId); List<Member> groupMembers(String groupId, LocalDate form, LocalDate to); List<GroupLightModel> memberGroups(String memberId); List<GroupLightModel> memberGroups(String memberId, LocalDate form, LocalDate to); Map<String, Long> getIntegrationsSummary();  }
@Test public void shouldAddLocalUsersToDb() { memberService.syncMembers(); List<MemberEntryEntity> entries = memberRepository.findAll(); assertThat(entries).hasSize(1); assertThat(entries.get(0).getId()).isEqualTo("member1"); }
@Test public void shouldSetAttachedForNewEntries() { memberService.syncMembers(); List<MembershipEntryEntity> entries = membershipRepository.findAll(); assertThat(entries).hasSize(1); assertThat(entries.get(0).getEvent()).isEqualTo(MembershipEntryEntity.Event.ATTACHED); assertThat(entries.get(0).getGroupId()).isEqualTo("group1"); assertThat(entries.get(0).getMember().getId()).isEqualTo("member1"); }
@Test public void shouldChangeStatusIfDetached() { MemberEntryEntity member = memberRepository.save(MemberEntryEntity.builder().id("member1").build()); membershipRepository.save(MembershipEntryEntity.builder() .membershipId(1L) .groupId("group1") .event(MembershipEntryEntity.Event.DETACHED) .date(LocalDate.now().minusDays(3)) .member(member) .build()); memberService.syncMembers(); Optional<MembershipEntryEntity> entry = membershipRepository.findFirstByMemberIdAndGroupIdOrderByDateDesc("member1", "group1"); assertThat(entry).isPresent(); assertThat(entry.get().getEvent()).isEqualTo(MembershipEntryEntity.Event.ATTACHED); assertThat(entry.get().getDate()).isEqualTo(LocalDate.now()); assertThat(entry.get().getGroupId()).isEqualTo("group1"); assertThat(entry.get().getMember().getId()).isEqualTo("member1"); }
@Test public void shouldSetAttachedOnExistingEntryIfLocalDateIsEqual() { MemberEntryEntity member = memberRepository.save(MemberEntryEntity.builder().id("member1").build()); membershipRepository.save(MembershipEntryEntity.builder() .groupId("group1") .event(MembershipEntryEntity.Event.DETACHED) .date(LocalDate.now()) .member(member) .build()); memberService.syncMembers(); List<MembershipEntryEntity> entries = membershipRepository.findAll(); assertThat(entries).hasSize(1); assertThat(entries.get(0).getEvent()).isEqualTo(MembershipEntryEntity.Event.ATTACHED); assertThat(entries.get(0).getDate()).isEqualTo(LocalDate.now()); assertThat(entries.get(0).getGroupId()).isEqualTo("group1"); assertThat(entries.get(0).getMember().getId()).isEqualTo("member1"); }
@Test public void shouldNotCreateNewEntryIfAlreadyAttached() { MemberEntryEntity member = memberRepository.save(MemberEntryEntity.builder().id("member1").build()); membershipRepository.save(MembershipEntryEntity.builder() .groupId("group1") .event(MembershipEntryEntity.Event.ATTACHED) .date(LocalDate.now().minusDays(3)) .member(member) .build()); memberService.syncMembers(); List<MembershipEntryEntity> entries = membershipRepository.findAll(); assertThat(entries).hasSize(1); assertThat(entries.get(0).getEvent()).isEqualTo(MembershipEntryEntity.Event.ATTACHED); assertThat(entries.get(0).getDate()).isEqualTo(LocalDate.now().minusDays(3)); assertThat(entries.get(0).getGroupId()).isEqualTo("group1"); assertThat(entries.get(0).getMember().getId()).isEqualTo("member1"); }
@Test public void shouldNotSetDetachedForNewEntries() { memberService.syncMembers(); Optional<MembershipEntryEntity> entry = membershipRepository.findFirstByMemberIdAndGroupIdOrderByDateDesc("member1", "group1"); assertThat(entry).isPresent(); assertThat(entry.get().getEvent()).isEqualTo(MembershipEntryEntity.Event.ATTACHED); assertThat(entry.get().getDate()).isEqualTo(LocalDate.now()); }
@Test public void shouldChangeStatusIfAttached() { MemberEntryEntity member = memberRepository.save(MemberEntryEntity.builder().id("member1").build()); membershipRepository.save(MembershipEntryEntity.builder() .groupId("group2") .event(MembershipEntryEntity.Event.ATTACHED) .date(LocalDate.now().minusDays(3)) .member(member) .build()); memberService.syncMembers(); Optional<MembershipEntryEntity> entry = membershipRepository.findFirstByMemberIdAndGroupIdOrderByDateDesc("member1", "group2"); assertThat(entry).isPresent(); assertThat(entry.get().getEvent()).isEqualTo(MembershipEntryEntity.Event.DETACHED); assertThat(entry.get().getDate()).isEqualTo(LocalDate.now()); }
@Test public void shouldSetDetachedOnExistingEntryIfLocalDateIsEqual() { MemberEntryEntity member = memberRepository.save(MemberEntryEntity.builder().id("member1").build()); membershipRepository.save(MembershipEntryEntity.builder() .groupId("group2") .event(MembershipEntryEntity.Event.ATTACHED) .date(LocalDate.now()) .member(member) .build()); memberService.syncMembers(); List<MembershipEntryEntity> entries = membershipRepository.findAll().stream().filter(e -> "group2".equals(e.getGroupId())).collect(Collectors.toList()); assertThat(entries).hasSize(1); assertThat(entries.get(0).getEvent()).isEqualTo(MembershipEntryEntity.Event.DETACHED); assertThat(entries.get(0).getDate()).isEqualTo(LocalDate.now()); assertThat(entries.get(0).getGroupId()).isEqualTo("group2"); assertThat(entries.get(0).getMember().getId()).isEqualTo("member1"); }
@Test public void shouldNotCreateNewEntryIfAlreadyDetached() { MemberEntryEntity member = memberRepository.save(MemberEntryEntity.builder().id("member1").build()); membershipRepository.save(MembershipEntryEntity.builder() .groupId("group2") .event(MembershipEntryEntity.Event.DETACHED) .date(LocalDate.now().minusDays(3)) .member(member) .build()); memberService.syncMembers(); List<MembershipEntryEntity> entries = membershipRepository.findAll().stream().filter(e -> "group2".equals(e.getGroupId())).collect(Collectors.toList()); assertThat(entries).hasSize(1); assertThat(entries.get(0).getEvent()).isEqualTo(MembershipEntryEntity.Event.DETACHED); assertThat(entries.get(0).getDate()).isEqualTo(LocalDate.now().minusDays(3)); assertThat(entries.get(0).getGroupId()).isEqualTo("group2"); assertThat(entries.get(0).getMember().getId()).isEqualTo("member1"); }