some done
This commit is contained in:
parent
13a162f744
commit
e620af3165
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,6 +5,7 @@ build/
|
|||||||
!**/src/main/**/build/
|
!**/src/main/**/build/
|
||||||
!**/src/test/**/build/
|
!**/src/test/**/build/
|
||||||
data.mv.db
|
data.mv.db
|
||||||
|
data.trace.db
|
||||||
|
|
||||||
### STS ###
|
### STS ###
|
||||||
.apt_generated
|
.apt_generated
|
||||||
|
@ -8,15 +8,13 @@ import java.util.List;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@Table(name = "cabinet")
|
||||||
public class Cabinet {
|
public class Cabinet {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Long id;
|
||||||
private String number;
|
private String number;
|
||||||
@ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.EAGER)
|
@OneToMany(cascade = {CascadeType.MERGE})
|
||||||
@JoinTable(name = "cabinets_computers",
|
|
||||||
joinColumns = @JoinColumn(name = "cabinet_fk"),
|
|
||||||
inverseJoinColumns = @JoinColumn(name = "computer_fk"))
|
|
||||||
private List<Computer> computers;
|
private List<Computer> computers;
|
||||||
|
|
||||||
public Cabinet() {
|
public Cabinet() {
|
||||||
@ -36,21 +34,18 @@ public class Cabinet {
|
|||||||
public List<Computer> getComputers() {
|
public List<Computer> getComputers() {
|
||||||
return computers;
|
return computers;
|
||||||
}
|
}
|
||||||
|
public void addComputer(Computer computer){
|
||||||
public void addComputer(Computer computer) {
|
|
||||||
if (computers == null){
|
if (computers == null){
|
||||||
this.computers = new ArrayList<>();
|
this.computers = new ArrayList<>();
|
||||||
}
|
}
|
||||||
if (!computers.contains(computer)) {
|
if (!computers.contains(computer)) {
|
||||||
this.computers.add(computer);
|
this.computers.add(computer);
|
||||||
computer.addCabinet(this);
|
computer.setCabinet(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void removeComputer(Computer computer) {
|
public void removeComputer(Computer computer){
|
||||||
if (computers.contains(computer)) {
|
if (computers.contains(computer))
|
||||||
this.computers.remove(computer);
|
this.computers.remove(computer);
|
||||||
computer.removeCabinet(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -57,7 +57,7 @@ public class CabinetService {
|
|||||||
int size = currentCabinet.getComputers().size();
|
int size = currentCabinet.getComputers().size();
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
Computer temp = currentCabinet.getComputers().get(i);
|
Computer temp = currentCabinet.getComputers().get(i);
|
||||||
temp.removeCabinet(currentCabinet);
|
temp.setCabinet(null);
|
||||||
currentCabinet.removeComputer(temp);
|
currentCabinet.removeComputer(temp);
|
||||||
em.remove(temp);
|
em.remove(temp);
|
||||||
}
|
}
|
||||||
|
@ -34,38 +34,35 @@ public class ComputerController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}/computers")
|
@GetMapping("/{id}/computers")
|
||||||
public List<Cabinet> getCabinetComputers(@PathVariable Long id) {
|
public Cabinet getComputersCabinet(@PathVariable Long id) {
|
||||||
return computerService.listCabinets(id);
|
return computerService.getCabinet(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/")
|
@PostMapping("/")
|
||||||
public Computer createComputer(@RequestParam("modelName") String modelName,
|
public Computer createComputer(@RequestParam("modelName") String modelName,
|
||||||
@RequestParam("serialNum") String serialNum,
|
@RequestParam("serialNum") String serialNum,
|
||||||
@RequestParam("monitorId") Long monitorId) {
|
@RequestParam("monitorId") Long monitorId,
|
||||||
return computerService.addComputer(modelName, serialNum, monitorId);
|
@RequestParam("cabinetId") Long cabinetId) {
|
||||||
|
return computerService.addComputer(modelName, serialNum, monitorId, cabinetId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/cabinet")
|
@PostMapping("/cabinet")
|
||||||
public Cabinet createCabinetComputer(@RequestParam("computerId") Long computerId,
|
public Cabinet setCabinetComputer(@RequestParam("computerId") Long computerId,
|
||||||
@RequestParam("cabinetId") Long cabinetId) {
|
@RequestParam("cabinetId") Long cabinetId) {
|
||||||
return computerService.addCabinet(cabinetId, computerId);
|
return computerService.setCabinet(cabinetId, computerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/{id}")
|
@PatchMapping("/{id}")
|
||||||
public Computer updateComputer(@PathVariable Long id,
|
public Computer updateComputer(@PathVariable Long id,
|
||||||
@RequestParam("modelName") String modelName,
|
@RequestParam("modelName") String modelName,
|
||||||
@RequestParam("serialNum") String serialNum) {
|
@RequestParam("serialNum") String serialNum,
|
||||||
return computerService.updateComputer(id, modelName, serialNum);
|
@RequestParam("monitorId") Long monitorId,
|
||||||
|
@RequestParam("cabinetId") Long cabinetId) {
|
||||||
|
return computerService.updateComputer(id, modelName, serialNum, monitorId, cabinetId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public Computer deleteComputer(@PathVariable Long id) {
|
public Computer deleteComputer(@PathVariable Long id) {
|
||||||
return computerService.deleteComputer(id);
|
return computerService.deleteComputer(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{computerId}/cabinet")
|
|
||||||
public Cabinet deleteCabinetComputer(@PathVariable Long computerId,
|
|
||||||
@RequestParam("cabinetId") Long cabinetId) {
|
|
||||||
return computerService.deleteCabinet(cabinetId, computerId);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -16,9 +16,10 @@ public class Computer {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private String modelName;
|
private String modelName;
|
||||||
private String serialNum;
|
private String serialNum;
|
||||||
@ManyToMany(mappedBy = "computers", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
|
@ManyToOne( cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
|
||||||
private List<Cabinet> cabinets;
|
@JoinColumn(name = "cabinets", nullable = true)
|
||||||
@OneToOne(cascade = {CascadeType.ALL})
|
private Cabinet cabinet;
|
||||||
|
@OneToOne()
|
||||||
@JoinColumn(name = "monitor_id")
|
@JoinColumn(name = "monitor_id")
|
||||||
private Monitor monitor;
|
private Monitor monitor;
|
||||||
|
|
||||||
@ -42,23 +43,8 @@ public class Computer {
|
|||||||
return serialNum;
|
return serialNum;
|
||||||
}
|
}
|
||||||
public void setserialNum(String serialNum) { this.serialNum = serialNum; }
|
public void setserialNum(String serialNum) { this.serialNum = serialNum; }
|
||||||
public List<Cabinet> getCabinets() { return cabinets; }
|
public Cabinet getCabinet() { return cabinet; }
|
||||||
|
public void setCabinet(Cabinet cabinet) { this.cabinet = cabinet; }
|
||||||
public void addCabinet(Cabinet cabinet) {
|
|
||||||
if (cabinets == null){
|
|
||||||
this.cabinets = new ArrayList<>();
|
|
||||||
}
|
|
||||||
if (!cabinets.contains(cabinet)) {
|
|
||||||
this.cabinets.add(cabinet);
|
|
||||||
cabinet.addComputer(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void removeCabinet(Cabinet cabinet) {
|
|
||||||
if (cabinets.contains(cabinet)) {
|
|
||||||
this.cabinets.remove(cabinet);
|
|
||||||
cabinet.removeComputer(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Monitor getMonitor() {
|
public Monitor getMonitor() {
|
||||||
return monitor;
|
return monitor;
|
||||||
|
@ -21,22 +21,28 @@ public class ComputerService {
|
|||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Computer addComputer(String modelName, String serialNum, Long monitorId) {
|
public Computer addComputer(String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||||
if (!StringUtils.hasText(modelName)) {
|
if (!StringUtils.hasText(modelName)) {
|
||||||
throw new IllegalArgumentException("Computer model name is null or empty");
|
throw new IllegalArgumentException("Computer model name is null or empty");
|
||||||
}
|
}
|
||||||
if (!StringUtils.hasText(serialNum)) {
|
if (!StringUtils.hasText(serialNum)) {
|
||||||
throw new IllegalArgumentException("Computer serial number is null or empty");
|
throw new IllegalArgumentException("Computer serial number is null or empty");
|
||||||
}
|
}
|
||||||
if ((Object)monitorId == null) {
|
final Computer computer = new Computer(modelName, serialNum);
|
||||||
throw new IllegalArgumentException("Monitor id is null or empty");
|
if (monitorId != null) {
|
||||||
}
|
|
||||||
final Monitor monitor = em.find(Monitor.class, monitorId);
|
final Monitor monitor = em.find(Monitor.class, monitorId);
|
||||||
if (monitor == null) {
|
if (monitor == null) {
|
||||||
throw new IllegalArgumentException(String.format("Monitor with id [%s] not found", monitorId));
|
throw new IllegalArgumentException(String.format("Monitor with id [%s] not found", monitorId));
|
||||||
}
|
}
|
||||||
final Computer computer = new Computer(modelName, serialNum);
|
|
||||||
computer.setMonitor(monitor);
|
computer.setMonitor(monitor);
|
||||||
|
}
|
||||||
|
if (cabinetId != null) {
|
||||||
|
final Cabinet cabinet = em.find(Cabinet.class, cabinetId);
|
||||||
|
if (cabinet == null) {
|
||||||
|
throw new IllegalArgumentException(String.format("Cabinet with id [%s] not found", cabinetId));
|
||||||
|
}
|
||||||
|
computer.setCabinet(cabinet);
|
||||||
|
}
|
||||||
em.persist(computer);
|
em.persist(computer);
|
||||||
return computer;
|
return computer;
|
||||||
}
|
}
|
||||||
@ -57,7 +63,7 @@ public class ComputerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Computer updateComputer(Long id, String modelName, String serialNum) {
|
public Computer updateComputer(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||||
if (!(StringUtils.hasText(modelName) || StringUtils.hasText(serialNum))) {
|
if (!(StringUtils.hasText(modelName) || StringUtils.hasText(serialNum))) {
|
||||||
throw new IllegalArgumentException("Need at least one argument");
|
throw new IllegalArgumentException("Need at least one argument");
|
||||||
}
|
}
|
||||||
@ -68,17 +74,23 @@ public class ComputerService {
|
|||||||
if (serialNum != null) {
|
if (serialNum != null) {
|
||||||
currentComputer.setserialNum(serialNum);
|
currentComputer.setserialNum(serialNum);
|
||||||
}
|
}
|
||||||
|
if (monitorId != null) {
|
||||||
|
Monitor monitor = em.find(Monitor.class, monitorId);
|
||||||
|
currentComputer.setMonitor(monitor);
|
||||||
|
}
|
||||||
|
if (cabinetId != null) {
|
||||||
|
Cabinet cabinet = em.find(Cabinet.class, cabinetId);
|
||||||
|
currentComputer.setCabinet(cabinet);
|
||||||
|
}
|
||||||
return em.merge(currentComputer);
|
return em.merge(currentComputer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Computer deleteComputer(Long id) {
|
public Computer deleteComputer(Long id) {
|
||||||
final Computer currentComputer = findComputer(id);
|
final Computer currentComputer = findComputer(id);
|
||||||
int size = currentComputer.getCabinets().size();
|
Cabinet currentComputerCabinet = currentComputer.getCabinet();
|
||||||
for (int i = 0; i < size; i++) {
|
if (currentComputerCabinet != null) {
|
||||||
Cabinet temp = currentComputer.getCabinets().get(i);
|
currentComputerCabinet.removeComputer(currentComputer);
|
||||||
temp.removeComputer(currentComputer);
|
|
||||||
currentComputer.removeCabinet(temp);
|
|
||||||
}
|
}
|
||||||
em.remove(currentComputer);
|
em.remove(currentComputer);
|
||||||
return currentComputer;
|
return currentComputer;
|
||||||
@ -86,23 +98,20 @@ public class ComputerService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllComputers() {
|
public void deleteAllComputers() {
|
||||||
List<Computer> list = em.createQuery("select s from Computer s", Computer.class).getResultList();
|
em.createQuery("delete from Computer").executeUpdate();
|
||||||
for(int i = 0; i < list.size(); i++){
|
|
||||||
deleteComputer(list.get(i).getId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<Cabinet> listCabinets(Long id) {
|
public Cabinet getCabinet(Long id) {
|
||||||
if ((Object)id == null) {
|
if ((Object)id == null) {
|
||||||
throw new IllegalArgumentException("Computer id is null or empty");
|
throw new IllegalArgumentException("Computer id is null or empty");
|
||||||
}
|
}
|
||||||
final Computer computer = em.find(Computer.class, id);
|
final Computer computer = em.find(Computer.class, id);
|
||||||
return computer.getCabinets();
|
return computer.getCabinet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Cabinet addCabinet(Long cabinetId, Long computerId) {
|
public Cabinet setCabinet(Long cabinetId, Long computerId) {
|
||||||
if ((Object)computerId == null) {
|
if ((Object)computerId == null) {
|
||||||
throw new IllegalArgumentException("Computer id is null or empty");
|
throw new IllegalArgumentException("Computer id is null or empty");
|
||||||
}
|
}
|
||||||
@ -111,21 +120,7 @@ public class ComputerService {
|
|||||||
}
|
}
|
||||||
final Computer computer = em.find(Computer.class, computerId);
|
final Computer computer = em.find(Computer.class, computerId);
|
||||||
final Cabinet cabinet = em.find(Cabinet.class, cabinetId);
|
final Cabinet cabinet = em.find(Cabinet.class, cabinetId);
|
||||||
computer.addCabinet(cabinet);
|
computer.setCabinet(cabinet);
|
||||||
return cabinet;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Cabinet deleteCabinet(Long cabinetId, Long computerId) {
|
|
||||||
if ((Object)computerId == null) {
|
|
||||||
throw new IllegalArgumentException("Computer id is null or empty");
|
|
||||||
}
|
|
||||||
if ((Object)cabinetId == null) {
|
|
||||||
throw new IllegalArgumentException("Cabinet id is null or empty");
|
|
||||||
}
|
|
||||||
final Computer computer = em.find(Computer.class, computerId);
|
|
||||||
final Cabinet cabinet = em.find(Cabinet.class, cabinetId);
|
|
||||||
computer.removeCabinet(cabinet);
|
|
||||||
return cabinet;
|
return cabinet;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -13,8 +13,6 @@ public class Monitor {
|
|||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Long id;
|
||||||
private String modelName;
|
private String modelName;
|
||||||
@OneToOne(mappedBy = "monitor")
|
|
||||||
private Computer computer;
|
|
||||||
|
|
||||||
public Monitor() {
|
public Monitor() {
|
||||||
}
|
}
|
||||||
@ -31,8 +29,6 @@ public class Monitor {
|
|||||||
public void setmodelName(String modelName) {
|
public void setmodelName(String modelName) {
|
||||||
this.modelName = modelName;
|
this.modelName = modelName;
|
||||||
}
|
}
|
||||||
public Computer getComputer() { return computer; }
|
|
||||||
public void setComputer(Computer computer) { this.computer = computer; }
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.kalyshev.yan.monitor.service;
|
package com.kalyshev.yan.monitor.service;
|
||||||
|
|
||||||
|
import com.kalyshev.yan.computer.model.Computer;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
@ -53,16 +54,30 @@ public class MonitorService {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public Monitor deleteMonitor(Long id) {
|
public Monitor deleteMonitor(Long id) {
|
||||||
final Monitor currentMonitor = findMonitor(id);
|
final Monitor currentMonitor = findMonitor(id);
|
||||||
if(currentMonitor.getComputer() != null) currentMonitor.getComputer().removeMonitor();
|
|
||||||
em.remove(currentMonitor);
|
em.remove(currentMonitor);
|
||||||
return currentMonitor;
|
return currentMonitor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllMonitors() {
|
public void deleteAllMonitors() {
|
||||||
List<Monitor> list = em.createQuery("select s from Monitor s", Monitor.class).getResultList();
|
List<Computer> listComps = em.createQuery("select s from Computer s", Computer.class).getResultList();
|
||||||
for(int i = 0; i < list.size(); i++){
|
for (int i = 0; i < listComps.size(); i++){
|
||||||
deleteMonitor(list.get(i).getId());
|
listComps.get(i).removeMonitor();
|
||||||
}
|
}
|
||||||
|
List<Monitor> listMons = em.createQuery("select s from Monitor s", Monitor.class).getResultList();
|
||||||
|
for (int i = 0; i < listMons.size(); i++){
|
||||||
|
deleteMonitor(listMons.get(i).getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Computer getComputer(Long id) {
|
||||||
|
List<Computer> computers = em.createQuery("select s from Computer").getResultList();
|
||||||
|
for (int i = 0; i < computers.size(); i++){
|
||||||
|
if (computers.get(i).getMonitor().getId() == id) {
|
||||||
|
return computers.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -24,13 +24,13 @@ public class JpaComputerCabinetTests {
|
|||||||
private CabinetService cabinetService;
|
private CabinetService cabinetService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCabinetAddManyToMany() {
|
void testCabinetAddOneToMany() {
|
||||||
cabinetService.deleteAllCabinets();
|
cabinetService.deleteAllCabinets();
|
||||||
computerService.deleteAllComputers();
|
computerService.deleteAllComputers();
|
||||||
monitorService.deleteAllMonitors();
|
monitorService.deleteAllMonitors();
|
||||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||||
cabinet.addComputer(computer);
|
cabinet.addComputer(computer);
|
||||||
log.info(cabinet.toString());
|
log.info(cabinet.toString());
|
||||||
log.info(computer.toString());
|
log.info(computer.toString());
|
||||||
@ -38,13 +38,13 @@ public class JpaComputerCabinetTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCabinetDeleteManyToMany() {
|
void testCabinetDeleteOneToMany() {
|
||||||
cabinetService.deleteAllCabinets();
|
cabinetService.deleteAllCabinets();
|
||||||
computerService.deleteAllComputers();
|
computerService.deleteAllComputers();
|
||||||
monitorService.deleteAllMonitors();
|
monitorService.deleteAllMonitors();
|
||||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||||
cabinet.addComputer(computer);
|
cabinet.addComputer(computer);
|
||||||
log.info(cabinet.toString());
|
log.info(cabinet.toString());
|
||||||
log.info(computer.toString());
|
log.info(computer.toString());
|
||||||
@ -60,11 +60,11 @@ public class JpaComputerCabinetTests {
|
|||||||
monitorService.deleteAllMonitors();
|
monitorService.deleteAllMonitors();
|
||||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||||
computer.addCabinet(cabinet);
|
computer.setCabinet(cabinet);
|
||||||
log.info(cabinet.toString());
|
log.info(cabinet.toString());
|
||||||
log.info(computer.toString());
|
log.info(computer.toString());
|
||||||
Assertions.assertEquals(computer.getCabinets().get(0).getnumber(), cabinet.getnumber());
|
Assertions.assertEquals(computer.getCabinet().getnumber(), cabinet.getnumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -74,12 +74,12 @@ public class JpaComputerCabinetTests {
|
|||||||
monitorService.deleteAllMonitors();
|
monitorService.deleteAllMonitors();
|
||||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||||
computer.addCabinet(cabinet);
|
computer.setCabinet(cabinet);
|
||||||
log.info(cabinet.toString());
|
log.info(cabinet.toString());
|
||||||
log.info(computer.toString());
|
log.info(computer.toString());
|
||||||
Assertions.assertEquals(computer.getCabinets().get(0).getnumber(), cabinet.getnumber());
|
Assertions.assertEquals(computer.getCabinet().getnumber(), cabinet.getnumber());
|
||||||
computer.removeCabinet(cabinet);
|
computer.setCabinet(null);
|
||||||
Assertions.assertEquals(computer.getCabinets().size(), 0);
|
Assertions.assertNull(computer.getCabinet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.kalyshev.yan;
|
package com.kalyshev.yan;
|
||||||
|
|
||||||
|
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||||
import com.kalyshev.yan.computer.model.Computer;
|
import com.kalyshev.yan.computer.model.Computer;
|
||||||
import com.kalyshev.yan.computer.service.ComputerService;
|
import com.kalyshev.yan.computer.service.ComputerService;
|
||||||
@ -22,20 +23,41 @@ public class JpaComputerTests {
|
|||||||
private ComputerService computerService;
|
private ComputerService computerService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private MonitorService monitorService;
|
private MonitorService monitorService;
|
||||||
|
@Autowired
|
||||||
|
private CabinetService cabinetService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testComputerCreate() {
|
void testComputerPartialCreate() {
|
||||||
computerService.deleteAllComputers();
|
computerService.deleteAllComputers();
|
||||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
|
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId(), null);
|
||||||
log.info(computer.toString());
|
log.info(computer.toString());
|
||||||
Assertions.assertNotNull(computer.getId());
|
Assertions.assertNotNull(computer.getId());
|
||||||
|
Assertions.assertNull(computer.getCabinet());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testComputerFullCreate() {
|
||||||
|
computerService.deleteAllComputers();
|
||||||
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
|
final Cabinet cabinet = cabinetService.addCabinet("18a");
|
||||||
|
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId(), cabinet.getId());
|
||||||
|
log.info(computer.toString());
|
||||||
|
Assertions.assertNotNull(computer.getId());
|
||||||
|
Assertions.assertNull(computer.getCabinet());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testComputerDelete() {
|
||||||
|
computerService.deleteAllComputers();
|
||||||
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
|
final Computer computer = computerService.addComputer("Computer", "w7894572", null, null);
|
||||||
|
log.info(computer.toString());
|
||||||
|
computerService.deleteComputer(computer.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> computerService.findComputer(computer.getId()));
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
void testComputerRead() {
|
void testComputerRead() {
|
||||||
computerService.deleteAllComputers();
|
computerService.deleteAllComputers();
|
||||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
final Computer computer = computerService.addComputer("Computer", "w7894572", null, null);
|
||||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
|
|
||||||
log.info(computer.toString());
|
log.info(computer.toString());
|
||||||
final Computer findComputer = computerService.findComputer(computer.getId());
|
final Computer findComputer = computerService.findComputer(computer.getId());
|
||||||
log.info(findComputer.toString());
|
log.info(findComputer.toString());
|
||||||
@ -50,8 +72,8 @@ public class JpaComputerTests {
|
|||||||
void testComputerReadAll() {
|
void testComputerReadAll() {
|
||||||
computerService.deleteAllComputers();
|
computerService.deleteAllComputers();
|
||||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
computerService.addComputer("Computer", "w7894572", monitor.getId());
|
computerService.addComputer("Computer", "w7894572", null, null);
|
||||||
computerService.addComputer("Another comp", "3453s", monitor.getId());
|
computerService.addComputer("Another comp", "3453s", null, null);
|
||||||
final List<Computer> computers = computerService.findAllComputers();
|
final List<Computer> computers = computerService.findAllComputers();
|
||||||
log.info(computers.toString());
|
log.info(computers.toString());
|
||||||
Assertions.assertEquals(computers.size(), 2);
|
Assertions.assertEquals(computers.size(), 2);
|
||||||
|
@ -8,6 +8,7 @@ import com.kalyshev.yan.monitor.service.MonitorService;
|
|||||||
import jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.function.Executable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -19,6 +20,8 @@ public class JpaMonitorTests {
|
|||||||
private static final Logger log = LoggerFactory.getLogger(JpaMonitorTests.class);
|
private static final Logger log = LoggerFactory.getLogger(JpaMonitorTests.class);
|
||||||
@Autowired
|
@Autowired
|
||||||
private MonitorService monitorService;
|
private MonitorService monitorService;
|
||||||
|
@Autowired
|
||||||
|
private ComputerService computerService;
|
||||||
@Test
|
@Test
|
||||||
void testMonitorCreate() {
|
void testMonitorCreate() {
|
||||||
monitorService.deleteAllMonitors();
|
monitorService.deleteAllMonitors();
|
||||||
@ -36,6 +39,14 @@ public class JpaMonitorTests {
|
|||||||
Assertions.assertEquals(monitor, findMonitor);
|
Assertions.assertEquals(monitor, findMonitor);
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
|
void testMonitorDelete() {
|
||||||
|
monitorService.deleteAllMonitors();
|
||||||
|
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
|
log.info(monitor.toString());
|
||||||
|
monitorService.deleteMonitor(monitor.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> monitorService.findMonitor(monitor.getId()));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
void testMonitorReadNotFound() {
|
void testMonitorReadNotFound() {
|
||||||
monitorService.deleteAllMonitors();
|
monitorService.deleteAllMonitors();
|
||||||
Assertions.assertThrows(EntityNotFoundException.class, () -> monitorService.findMonitor(-1L));
|
Assertions.assertThrows(EntityNotFoundException.class, () -> monitorService.findMonitor(-1L));
|
||||||
@ -56,4 +67,13 @@ public class JpaMonitorTests {
|
|||||||
log.info(monitors.toString());
|
log.info(monitors.toString());
|
||||||
Assertions.assertEquals(monitors.size(), 0);
|
Assertions.assertEquals(monitors.size(), 0);
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
void testMonitorGetComputer() {
|
||||||
|
monitorService.deleteAllMonitors();
|
||||||
|
computerService.deleteAllComputers();
|
||||||
|
Monitor monitor = monitorService.addMonitor("Asus");
|
||||||
|
Computer computer = computerService.addComputer("Model", "6sfv4", monitor.getId(), null);
|
||||||
|
Computer fetchedComputer = monitorService.getComputer(monitor.getId());
|
||||||
|
Assertions.assertEquals(computer, fetchedComputer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user