From bdf0f87d772e234cd51a173367c4016f2f7472aa Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Thu, 9 Apr 2020 13:41:58 +0200 Subject: [PATCH] Added curtains --- .gitlab-ci.yml | 9 ++- .../controller/CurtainsController.java | 56 +++++++++++++++++++ .../smarthut/dto/CurtainsSaveRequest.java | 52 +++++++++++++++++ .../sanmarinoes/smarthut/models/Curtains.java | 46 +++++++++++++++ .../smarthut/models/CurtainsRepository.java | 3 + .../sanmarinoes/smarthut/CurtainsTests.java | 45 +++++++++++++++ 6 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4ea8d59..75fbcfc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,6 +6,7 @@ stages: - test - code_quality - deploy + - hopeItWorks #Sets up the docker smarthut_deploy: @@ -42,9 +43,8 @@ test: stage: test script: - gradle test - - npm install junit-viewer -g - - npm junit-viewer --results=/home/git/gitlab/shared/artifacts/build/test-results/test --port=3000 artifacts: + when: always paths: - build/test-results/test/TEST-*.xml reports: @@ -63,3 +63,8 @@ code_quality: #create a report on the quality of the code expose_as: 'Code Quality Report' allow_failure: true + +Robe: + stage: hopeItWorks + script: + - curl -L \ No newline at end of file diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java new file mode 100644 index 0000000..11f7aee --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java @@ -0,0 +1,56 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.CurtainsSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Curtains; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.CurtainsRepository; +import java.util.List; +import javax.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.*; + +@RestController +@EnableAutoConfiguration +@RequestMapping("/curtains") +public class CurtainsController { + @Autowired private CurtainsRepository curtainsService; + + @GetMapping + List findAll() { + return toList(curtainsService.findAll()); + } + + @GetMapping("/{id}") + public Curtains findById(@PathVariable("id") long id) throws NotFoundException { + return curtainsService.findById(id).orElseThrow(NotFoundException::new); + } + + private Curtains save(Curtains newRL, CurtainsSaveRequest s) { + newRL.setName(s.getName()); + newRL.setRoomId(s.getRoomId()); + newRL.setOpening(s.getOpening()); + + return curtainsService.save(newRL); + } + + @PostMapping + public Curtains create(@Valid @RequestBody CurtainsSaveRequest curtain) { + return save(new Curtains(), curtain); + } + + @PutMapping + public Curtains update(@Valid @RequestBody CurtainsSaveRequest curtain) + throws NotFoundException { + return save( + curtainsService.findById(curtain.getId()).orElseThrow(NotFoundException::new), + curtain); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable("id") long id) { + curtainsService.deleteById(id); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java new file mode 100644 index 0000000..876d770 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java @@ -0,0 +1,52 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.NotNull; + +public class CurtainsSaveRequest { + /** The state of this switch */ + private int openedAmount; + + /** Device identifier */ + private long id; + + /** + * The room this device belongs in, as a foreign key id. To use when updating and inserting from + * a REST call. + */ + @NotNull private Long roomId; + + /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ + @NotNull private String name; + + public void setRoomId(Long roomId) { + this.roomId = roomId; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public Long getRoomId() { + return roomId; + } + + public String getName() { + return name; + } + + public int getOpening() { + return this.openedAmount; + } + + public void setOpening(int newOpening) { + this.openedAmount = newOpening; + } + + public void setId(long id) { + this.id = id; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java new file mode 100644 index 0000000..35c0ab9 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java @@ -0,0 +1,46 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; + +@Entity +public class Curtains extends OutputDevice { + + @Column(name = "curtains_id") + private Long curtainsId; + + public void setCurtainsId(Long curtainsId) { + this.curtainsId = curtainsId; + } + + @NotNull + @Column(nullable = false) + @Min(0) + @Max(100) + /*it represents how much the curtains are opened, 0 is completely closed 100 is completely open*/ + private int openedAmount; + + public Curtains() { + super("curtains"); + this.openedAmount = 0; + } + + /*return the current state of the curtain*/ + public int getOpening() { + return this.openedAmount; + } + + /*sets the state to a desired one*/ + public void setOpening(int newOpening) { + if (newOpening < 0) { + this.openedAmount = 0; + } else if (newOpening > 100) { + this.openedAmount = 100; + } else { + this.openedAmount = newOpening; + } + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java new file mode 100644 index 0000000..83f4d16 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java @@ -0,0 +1,3 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +public interface CurtainsRepository extends DeviceRepository {} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java new file mode 100644 index 0000000..c7ceeee --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java @@ -0,0 +1,45 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Curtains; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("Curtains tests") +public class CurtainsTests { + public Curtains curtains; + + @BeforeEach + public void createCurtains() { + this.curtains = new Curtains(); + } + + @Test + @DisplayName("State when just created") + public void initialState() { + assertEquals(0, this.curtains.getOpening()); + } + + @Test + @DisplayName("Check wether setting the opening works") + public void normalSet() { + this.curtains.setOpening(42); + assertEquals(42, this.curtains.getOpening()); + } + + @Test + @DisplayName("Set setting a negative number") + public void setNeg() { + this.curtains.setOpening(-1); + assertEquals(0, this.curtains.getOpening()); + } + + @Test + @DisplayName("Setting state to a number greater than 100") + public void setLarge() { + this.curtains.setOpening(32768); + assertEquals(100, this.curtains.getOpening()); + } +}