Skip to content

Commit

Permalink
providing Box Restful Service
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanSST committed Mar 24, 2024
1 parent 5bdf632 commit 2d790f7
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 10 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ This code will run on a Raspberry PI in a chicken barn. There are scales under t

### Epic v1: Chicken scale recognizes weight change and sends message about state (which chicken, an egg) via Messenger

* BUG: wenn Ei gelegt und erkannt, tare-it oder wenn nicht, die letzten Hühner in Message packen
* correct shutdown of mqtt user
* implement calibrate
* write tests and mocks for mqtt client
* ping message to check scale
* endpoint to display state and messages
* endpoint to set current weight, box-state and chicken
* understand mqtt, qos, retain and create tasks to adopt
* (1) correct shutdown of mqtt user
* (1) understand mqtt, qos, retain and create tasks to adopt
* (1) endpoint to display messured weights
* (3) BUG: wenn Ei gelegt und erkannt, tare-it oder wenn nicht, die letzten Hühner in Message packen
* (3) implement calibrate
* (2) write tests and mocks for mqtt client
* (2) ping message to check scale


## TODO List Arduino
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/ch/stephan/chickenfarm/dto/BoxState.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package ch.stephan.chickenfarm.dto;

import java.util.Arrays;

public enum BoxState {
EMPTY, CHICKEN_IN, EGG_IN
EMPTY, CHICKEN_IN, EGG_IN;

public static BoxState findByName(String name) {
return Arrays.asList(values()).stream()//
.filter(bs -> bs.name().equalsIgnoreCase(name))//
.findFirst()//
.orElse(null);
}
}
9 changes: 9 additions & 0 deletions src/main/java/ch/stephan/chickenfarm/dto/Chicken.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.stephan.chickenfarm.dto;

import java.util.Arrays;

public enum Chicken {
HEIDI("Heidi", 2200), //
KLARA("Klara", 2850), //
Expand All @@ -22,4 +24,11 @@ public int getWeight() {
return weight;
}

public static Chicken findByName(String name) {
return Arrays.asList(values()).stream()//
.filter(c -> c.name().equalsIgnoreCase(name))//
.findFirst()//
.orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MessengerService {

public String sendNotification(String message) {
if (!enabled) {
return "Messaging was disabled, not message sent.";
return "Messaging was disabled, no message sent.";
}

try {
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/ch/stephan/chickenfarm/services/BoxController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ch.stephan.chickenfarm.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ch.stephan.chickenfarm.dto.Box;
import ch.stephan.chickenfarm.dto.BoxState;
import ch.stephan.chickenfarm.dto.Chicken;
import ch.stephan.chickenfarm.registry.BoxService;

@RestController
public class BoxController {

@Autowired
private BoxService boxService;

@GetMapping("/boxes")
public List<Box> get() {
return boxService.getBoxes();
}

@GetMapping("/boxes/{id}")
public Box changeWeightOfBox(@PathVariable String id, //
@RequestParam(value = "weight", required = false) String weight, //
@RequestParam(value = "state", required = false) String state, //
@RequestParam(value = "chicken", required = false) String chicken //
) {
Box box = boxService.getBox(id);

if (weight != null) {
box.setWeight(Integer.valueOf(weight));
}

BoxState boxState = BoxState.findByName(state);
if (boxState != null) {
box.setBoxState(boxState);
}

Chicken matchedChicken = Chicken.findByName(chicken);
if (matchedChicken != null || "null".equalsIgnoreCase(chicken)) {
box.setChicken(matchedChicken);
}

return box;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ch.stephan.chickenfarm.services;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import ch.stephan.chickenfarm.dto.Box;
import ch.stephan.chickenfarm.dto.Chicken;
import ch.stephan.chickenfarm.dto.Measure;

@WebMvcTest(value = BoxController.class)
@ActiveProfiles("maven")
@Disabled("Duplicate mock definition for BoxService, reason unknown")
class BoxControllerTest {

private static final Box BOX1 = Box.HINTEN;
private static final Box BOX2 = Box.VORNE;
private static final Chicken CHICKEN1 = Chicken.HEIDI;
private static final Chicken CHICKEN2 = Chicken.KLARA;

@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

// @Autowired
// private BoxService boxService;
//
@BeforeEach
void setUp() {
// MockitoAnnotations.openMocks(this);
}

@Test
void testGet() throws Exception {
String mockMvcResult = mockMvc.perform(get("/boxes").contentType(MediaType.APPLICATION_JSON))//
.andExpect(status().isOk())//
.andReturn()//
.getResponse()//
.getContentAsString();

List<Measure> boxes = objectMapper.readValue(mockMvcResult, new TypeReference<>() {
});

assertNotNull(boxes);
assertThat(boxes).hasSize(2);
assertThat(boxes.get(0).boxId()).isEqualTo(BOX1.getId());
assertThat(boxes.get(0).boxDescription()).isEqualTo(BOX1.getDescription());
assertThat(boxes.get(0).currentWeight()).isEqualTo(CHICKEN1.getWeight());
assertThat(boxes.get(0).currentChicken()).isEqualTo(CHICKEN1.name());
assertThat(boxes.get(1).boxId()).isEqualTo(BOX2.getId());
assertThat(boxes.get(1).boxDescription()).isEqualTo(BOX2.getDescription());
assertThat(boxes.get(1).currentWeight()).isEqualTo(CHICKEN2.getWeight());
assertThat(boxes.get(1).currentChicken()).isEqualTo(CHICKEN2.name());
}

}

0 comments on commit 2d790f7

Please sign in to comment.