Added curtains

This commit is contained in:
Jacob Salvi 2020-04-09 13:41:58 +02:00
parent 8179e0ff19
commit bdf0f87d77
6 changed files with 209 additions and 2 deletions

View file

@ -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

View file

@ -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<Curtains> 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);
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}
}

View file

@ -0,0 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface CurtainsRepository extends DeviceRepository<Curtains> {}

View file

@ -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());
}
}