diff --git a/src/main/java/cz/fi/muni/pa165/seminar4/group7/secretservice/entity/Agent.java b/src/main/java/cz/fi/muni/pa165/seminar4/group7/secretservice/entity/Agent.java index 3aed0b7c4da523bc0e0fffa17b193b2ee663931d..f1539c56de73f7d0d034c18c6814613aa13c4219 100644 --- a/src/main/java/cz/fi/muni/pa165/seminar4/group7/secretservice/entity/Agent.java +++ b/src/main/java/cz/fi/muni/pa165/seminar4/group7/secretservice/entity/Agent.java @@ -40,6 +40,10 @@ public class Agent { agentAssignments.add(agentAssignment); } + public void removeAssignment(AgentAssignment agentAssignment) { + agentAssignments.remove(agentAssignment); + } + public void addCodeName(CodeName codeName) { codeNames.add(codeName); } diff --git a/src/test/java/cz/fi/muni/pa165/seminar4/group7/secretservice/AgentTest.java b/src/test/java/cz/fi/muni/pa165/seminar4/group7/secretservice/AgentTest.java new file mode 100644 index 0000000000000000000000000000000000000000..fb86f8feaa655cd006934d8b187e846d496748f8 --- /dev/null +++ b/src/test/java/cz/fi/muni/pa165/seminar4/group7/secretservice/AgentTest.java @@ -0,0 +1,146 @@ +package cz.fi.muni.pa165.seminar4.group7.secretservice; + +import cz.fi.muni.pa165.seminar4.group7.secretservice.dao.AgentDao; +import cz.fi.muni.pa165.seminar4.group7.secretservice.entity.*; +import cz.fi.muni.pa165.seminar4.group7.secretservice.enums.LanguageCode; +import cz.fi.muni.pa165.seminar4.group7.secretservice.enums.WeaponCategory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.transaction.Transactional; + +import static org.assertj.core.api.Assertions.*; + +@SpringBootTest +@Transactional +public class AgentTest { + + @Autowired + private AgentDao agentDao; + + @BeforeEach + private void deleteAll() { + agentDao.deleteAll(); + } + + @Test + public void testEmpty() { + assertThat(agentDao.findAll()).hasSize(0); + } + + @Test + public void createAgents() { + var agentAlice = new Agent(); + var agentBob = new Agent(); + var aliceTraining = "Special training of Alice"; + var bobTraining = "Special training of Bob"; + agentAlice.setTraining(aliceTraining); + agentBob.setTraining(bobTraining); + agentDao.save(agentAlice); + agentDao.save(agentBob); + + var agents = agentDao.findAll(); + for (var agent : + agents) { + assertThat(agent.getTraining()).isIn(aliceTraining, bobTraining); + } + + assertThat(agents).hasSize(2); + } + + @Test + public void modifyAgent() { + var agentAlice = new Agent(); + agentAlice.setTraining("Special training of Alice"); + agentDao.save(agentAlice); + + var agent = agentDao.findById(agentAlice.getId()); + assertThat(agent).isPresent(); + var betterTraining = "Better training of Alice"; + agent.get().setTraining(betterTraining); + agentDao.save(agent.get()); + + var modifiedAgent = agentDao.findById(agentAlice.getId()); + assertThat(modifiedAgent).isPresent(); + assertThat(modifiedAgent.get().getTraining()).isEqualTo(betterTraining); + } + + @Test + public void addSkills() { + var agentAlice = new Agent(); + agentAlice.addSkill(new WeaponSkill().setWeaponCategory(WeaponCategory.KNIVES)); + agentAlice.addSkill(new LanguageSkill().setLanguageCode(LanguageCode.en_US)); + agentDao.save(agentAlice); + + var agent = agentDao.findById(agentAlice.getId()); + assertThat(agent).isPresent(); + assertThat(agent.get().getSkills()).hasSize(2); + } + + @Test + public void removeSkills() { + var agentAlice = new Agent(); + agentAlice.addSkill(new WeaponSkill().setWeaponCategory(WeaponCategory.KNIVES)); + agentAlice.addSkill(new LanguageSkill().setLanguageCode(LanguageCode.en_US)); + agentDao.save(agentAlice); + + agentAlice.removeSkill(new WeaponSkill().setWeaponCategory(WeaponCategory.KNIVES)); + agentDao.save(agentAlice); + var agent = agentDao.findById(agentAlice.getId()); + assertThat(agent).isPresent(); + assertThat(agent.get().getSkills()).hasSize(1); + } + + @Test + public void addCodeNames() { + var agentAlice = new Agent(); + agentAlice.addCodeName(new CodeName("Alpha")); + agentAlice.addCodeName(new CodeName("Jednotka")); + agentDao.save(agentAlice); + + var agent = agentDao.findById(agentAlice.getId()); + assertThat(agent).isPresent(); + assertThat(agent.get().getCodeNames()).hasSize(2); + } + + @Test + public void removeCodeName() { + var agentAlice = new Agent(); + agentAlice.addCodeName(new CodeName("Alpha")); + agentAlice.addCodeName(new CodeName("Jednotka")); + agentDao.save(agentAlice); + + agentAlice.removeCodeName(new CodeName("Alpha")); + agentDao.save(agentAlice); + var agent = agentDao.findById(agentAlice.getId()); + assertThat(agent).isPresent(); + assertThat(agent.get().getCodeNames()).hasSize(1); + } + + @Test + public void addAssignment() { + var agentAlice = new Agent(); + agentAlice.addAgentAssignment(new AgentAssignment()); + agentDao.save(agentAlice); + + var agent = agentDao.findById(agentAlice.getId()); + assertThat(agent).isPresent(); + assertThat(agent.get().getAgentAssignments()).hasSize(1); + } + + @Test + public void removeAssignment() { + var agentAlice = new Agent(); + agentAlice.addAgentAssignment(new AgentAssignment()); + agentAlice.addAgentAssignment(new AgentAssignment()); + agentDao.save(agentAlice); + + agentAlice.removeAssignment(new AgentAssignment()); + agentDao.save(agentAlice); + var agent = agentDao.findById(agentAlice.getId()); + assertThat(agent).isPresent(); + assertThat(agent.get().getAgentAssignments()).hasSize(1); + } +}