From f29e5c840e9950ac6fb94c78b99a0253c085a286 Mon Sep 17 00:00:00 2001 From: tbilos Date: Sun, 27 Mar 2022 20:54:11 +0200 Subject: [PATCH] implement tests --- .../group7/secretservice/entity/Agent.java | 4 + .../group7/secretservice/AgentTest.java | 146 ++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/test/java/cz/fi/muni/pa165/seminar4/group7/secretservice/AgentTest.java 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 3aed0b7c..f1539c56 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 00000000..fb86f8fe --- /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); + } +} -- GitLab