Skip to content
Snippets Groups Projects
Commit eef57ccc authored by Anders Austlid's avatar Anders Austlid
Browse files

Added Waste endpoint/service/repo, and initial tests for Service

parent 453ee57b
Branches
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.controller.group;public class WasteController { package ntnu.idatt2016.v233.SmartMat.controller.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import ntnu.idatt2016.v233.SmartMat.service.group.WasteService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@AllArgsConstructor
@RestController
@RequestMapping("/api/waste")
public class WasteController {
private final WasteService wasteService;
/**
* Saves a new waste
*
* @param waste the waste to save
* @return a ResponseEntity containing the saved waste if it was saved successfully, or a 400 if it wasn't
*/
@PostMapping("/waste")
public ResponseEntity<Waste> createWaste(@RequestBody Waste waste) {
if(wasteService.getWasteById(waste.getWasteId()).isPresent()) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(wasteService.createWaste(waste));
}
/**
* Gets a waste by its id
*
* @param wasteId the id of the waste
* @return a ResponseEntity containing the waste if it exists, or a 404 if it doesn't
*/
@GetMapping("/waste/{wasteId}")
public ResponseEntity<Waste> getWasteById(@PathVariable("wasteId") long wasteId) {
return wasteService.getWasteById(wasteId)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
} }
...@@ -26,10 +26,10 @@ public class Waste { ...@@ -26,10 +26,10 @@ public class Waste {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "waste_id") @Column(name = "waste_id")
long wasteID; long wasteId;
@Column(name = "group_id") @Column(name = "group_id")
long groupID; long groupId;
@Column(name = "ean") @Column(name = "ean")
long ean; long ean;
......
package ntnu.idatt2016.v233.SmartMat.repository.group;public interface WastesRepository { package ntnu.idatt2016.v233.SmartMat.repository.group;
import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import org.springframework.data.jpa.repository.JpaRepository;
public interface WasteRepository extends JpaRepository<Waste, Long> {
} }
package ntnu.idatt2016.v233.SmartMat.service.group; package ntnu.idatt2016.v233.SmartMat.service.group;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import ntnu.idatt2016.v233.SmartMat.repository.group.WasteRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
public class WasteRepository { public class WasteService {
private final WasteRepository wasteRepository; private final WasteRepository wasteRepository;
/**
* Creates a new waste
*
* @param waste the waste to create
* @return the created waste
*/
public Waste createWaste(Waste waste) {
return wasteRepository.save(waste);
}
/**
* Gets a waste by its id
* @param id the id of the waste
* @return an optional containing the waste if it exists
*/
public Optional<Waste> getWasteById(long id) {
return wasteRepository.findById(id);
}
} }
package ntnu.idatt2016.v233.SmartMat.service.group;
import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import ntnu.idatt2016.v233.SmartMat.repository.group.WasteRepository;
import ntnu.idatt2016.v233.SmartMat.service.group.WasteService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.sql.Timestamp;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
public class WasteServiceTest {
private WasteService wasteService;
@Mock
private WasteRepository wasteRepository;
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
wasteService = new WasteService(wasteRepository);
}
@Test
public void testCreateWaste() {
Waste waste = Waste.builder()
.groupId(1L)
.ean(1234567890123L)
.timestamp(new Timestamp(System.currentTimeMillis()))
.amount(1.0)
.unit("kg")
.build();
Waste createdWaste = Waste.builder()
.wasteId(1L)
.groupId(1L)
.ean(1234567890123L)
.timestamp(waste.getTimestamp())
.amount(1.0)
.unit("kg")
.build();
when(wasteRepository.save(waste)).thenReturn(createdWaste);
Waste result = wasteService.createWaste(waste);
assertEquals(createdWaste, result);
}
@Test
public void testGetWasteById() {
Waste waste = Waste.builder()
.wasteId(1L)
.groupId(1L)
.ean(1234567890123L)
.timestamp(new Timestamp(System.currentTimeMillis()))
.amount(1.0)
.unit("kg")
.build();
when(wasteRepository.findById(1L)).thenReturn(Optional.of(waste));
Optional<Waste> result = wasteService.getWasteById(1L);
assertTrue(result.isPresent());
assertEquals(waste, result.get());
}
}
...@@ -14,7 +14,8 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialec ...@@ -14,7 +14,8 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialec
# Hibernate ddl auto (create, create-drop, validate, update) # Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.ddl-auto = update
# Set CORS configuration origin address
domain.domain=https://smartmat.app
## info ## info
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment