Commit bc543518 authored by tbilos's avatar tbilos
Browse files

Merge branch 'main' into 14-agent-dao-tests

parents f29e5c84 bf92755f
Pipeline #123130 waiting for manual action with stage
......@@ -31,6 +31,7 @@ public class AgentAssignment {
@OneToOne
@JoinColumn(name = "report_id")
@Setter(AccessLevel.NONE)
private Report report;
@ManyToOne
......@@ -44,6 +45,11 @@ public class AgentAssignment {
@Setter(AccessLevel.NONE)
private Set<PerformanceEvaluation> performanceEvaluations = new HashSet<>();
public void setReport(Report report) {
this.report = report;
report.setAgentAssignment(this);
}
public void addPerformanceEvaluation(PerformanceEvaluation performanceEvaluation) {
performanceEvaluations.add(performanceEvaluation);
}
......
......@@ -3,6 +3,7 @@ package cz.fi.muni.pa165.seminar4.group7.secretservice.entity;
import com.sun.istack.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import javax.persistence.*;
import java.sql.Date;
......@@ -41,7 +42,7 @@ public class Mission {
@OneToMany(mappedBy = "mission")
private List<Resource> resources;
@ManyToOne
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "country_id")
@Getter
@Setter
......
package cz.fi.muni.pa165.seminar4.group7.secretservice;
import cz.fi.muni.pa165.seminar4.group7.secretservice.dao.MissionDao;
import cz.fi.muni.pa165.seminar4.group7.secretservice.dao.ResourceDao;
import cz.fi.muni.pa165.seminar4.group7.secretservice.entity.Country;
import cz.fi.muni.pa165.seminar4.group7.secretservice.entity.Mission;
import cz.fi.muni.pa165.seminar4.group7.secretservice.entity.Resource;
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 java.sql.Date;
import java.util.Calendar;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
class MissionTest {
@Autowired
private MissionDao missionDao;
@BeforeEach
private void reset() {
missionDao.deleteAll();
}
Mission createMission() {
var country = new Country().setCode("US");
country.setCommunications("Everything.")
.setDemographics("Americans.")
.setEconomy("Big.")
.setGovernment("Blue or red.")
.setMilitary("Even bigger.")
.setName("America, hell yeah!")
.setGeography("Smaller than widely believed.");
var mission = new Mission();
var today = new Date(Calendar.getInstance().getTime().getTime());
mission.setCountry(country);
mission.setDuration(10);
mission.setObjective("Retrieve bomb plans. Failure is not an option.");
mission.setStart(today);
return mission;
}
@Test
void testCreate() {
var mission = createMission();
missionDao.save(mission);
assertThat(missionDao.findAll()).hasSize(1);
}
@Test
@Transactional
void testFindAll() {
var mission = createMission();
missionDao.save(mission);
assertThat(missionDao.findAll().iterator().next()).isEqualTo(mission);
}
@Test
void testFindById() {
var mission = createMission();
missionDao.save(mission);
var mission2 = missionDao.findById(mission.getId());
assertThat(mission2).isPresent();
assertThat(mission2.get().getId()).isEqualTo(mission.getId());
}
@Test
void testSave() {
var mission = createMission();
missionDao.save(mission);
mission.setObjective("New objective, get out of there ASAP.");
missionDao.save(mission);
var savedMission = missionDao.findById(mission.getId());
assertThat(savedMission).isNotEmpty();
assertThat(savedMission.get().getObjective()).isEqualTo(mission.getObjective());
}
@Test
void testFindEmpty() {
assertThat(missionDao.findById(1L)).isEmpty();
}
@Test
void testCreateDuplicate() {
var mission = createMission();
missionDao.save(mission);
var mission2 = missionDao.findById(mission.getId());
assertThat(mission2).isPresent();
missionDao.save(mission2.get());
assertThat(missionDao.findAll()).hasSize(1);
}
@Test
void testDelete() {
var mission = createMission();
missionDao.save(mission);
missionDao.delete(mission);
assertThat(missionDao.findAll()).hasSize(0);
}
}
package cz.fi.muni.pa165.seminar4.group7.secretservice.dao;
import cz.fi.muni.pa165.seminar4.group7.secretservice.entity.AgentAssignment;
import cz.fi.muni.pa165.seminar4.group7.secretservice.entity.Report;
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.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Milan Mozolak
*/
@SpringBootTest
@Transactional
class ReportDaoTests {
@Autowired
private ReportDao reportDao;
@BeforeEach
private void reset() {
reportDao.deleteAll();
}
private Report createNewReport() {
return new Report().setReport("Test");
}
@Test
void create() {
assertThat(reportDao.findAll()).isEmpty();
var report = createNewReport();
reportDao.save(report);
assertThat(reportDao.findAll()).hasSize(1);
}
@Test
void findByIdExisting() {
var report = createNewReport();
reportDao.save(report);
var find = reportDao.findById(report.getId());
assertThat(find).isPresent();
}
@Test
void findByIdNonExisting() {
assertThat(reportDao.findAll()).isEmpty();
var find = reportDao.findById(1L);
assertThat(find).isEmpty();
}
@Test
void findAll() {
for (int i = 1; i < 10; i++) {
reportDao.save(createNewReport());
assertThat(reportDao.findAll()).hasSize(i);
}
}
@Test
void update() {
var r1 = createNewReport();
reportDao.save(r1);
var r2 = reportDao.findById(r1.getId());
assertThat(r2).isNotEmpty();
assertEquals(r1.getReport(), r2.get().getReport());
r1.setReport("Report 2");
reportDao.save(r1);
var r3 = reportDao.findById(r1.getId());
assertThat(r3).isNotEmpty();
assertEquals(r1.getReport(), r3.get().getReport());
assertEquals(r1.getReport(), r2.get().getReport());
}
@Test
void delete() {
var report = createNewReport();
reportDao.save(report);
assertThat(reportDao.findAll()).hasSize(1);
reportDao.delete(report);
assertThat(reportDao.findAll()).isEmpty();
}
@Test
void createDuplicate() {
var report = createNewReport();
reportDao.save(report);
assertThat(reportDao.findAll()).hasSize(1);
var report2 = reportDao.findById(report.getId());
assertThat(report2).isPresent();
reportDao.save(report2.get());
assertEquals(report.getId(), report2.get().getId());
assertThat(reportDao.findAll()).hasSize(1);
}
@Test
void agentAssignment() {
var assignment = new AgentAssignment();
var report = createNewReport();
assignment.setReport(report);
reportDao.save(report);
var report2 = reportDao.findById(report.getId());
assertThat(report2).isPresent();
assertEquals(assignment, report2.get().getAgentAssignment());
}
}
Supports Markdown
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