-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bdf632
commit 2d790f7
Showing
6 changed files
with
152 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/ch/stephan/chickenfarm/services/BoxController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/test/java/ch/stephan/chickenfarm/services/BoxControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |