6 Dateien geändert
1 Dateien umbenannt
| Datei umbenannt von src/main/java/de/gedoplan/seminar/sbt/di/exercise/SbtDiExerciseApplication.java |
| | |
| | | |
| | | @ConfigurationPropertiesScan |
| | | @SpringBootApplication |
| | | public class SbtDiExerciseApplication { |
| | | public class SbtCoreExerciseApplication { |
| | | |
| | | public static void main(String[] args) { |
| | | SpringApplication.run(SbtDiExerciseApplication.class, args); |
| | | SpringApplication.run(SbtCoreExerciseApplication.class, args); |
| | | } |
| | | |
| | | } |
| | |
| | | package de.gedoplan.seminar.sbt.di.exercise.rest; |
| | | |
| | | import de.gedoplan.seminar.sbt.di.exercise.aop.Counted; |
| | | import de.gedoplan.seminar.sbt.di.exercise.domain.Cocktail; |
| | | import de.gedoplan.seminar.sbt.di.exercise.service.BarInfo; |
| | | import de.gedoplan.seminar.sbt.di.exercise.service.CocktailService; |
| | |
| | | return barInfo.barName(); |
| | | } |
| | | |
| | | @Counted |
| | | @GetMapping(path = "cocktails", produces = MediaType.APPLICATION_JSON_VALUE) |
| | | public List<Cocktail> getCocktails() { |
| | | return cocktails; |
| | |
| | | |
| | | @EventListener |
| | | public void select(Cocktail cocktail) { |
| | | cocktail.getIngredients().entrySet().stream() |
| | | .filter(not(e -> ingredientService.isAvailable(e.getKey().getId(), e.getValue()))) |
| | | .findFirst().ifPresent(beverage -> { |
| | | throw new OutOfStockException(beverage.getKey()); |
| | | ingredientService.beveragesNotAvailable(cocktail) |
| | | .stream().findFirst().ifPresent(beverage -> { |
| | | throw new OutOfStockException(beverage); |
| | | }); |
| | | } |
| | | } |
| | |
| | | private void init() { |
| | | Map<Boolean,List<Cocktail>> cocktails = cocktailService.findAll().stream() |
| | | .collect(Collectors.groupingBy(Cocktail::isAlcoholic)); |
| | | cocktailsAlcoholic = cocktails.get(true); |
| | | cocktailsNonAlcoholic = cocktails.get(false); |
| | | cocktailsAlcoholic = cocktails.getOrDefault(true,List.of()); |
| | | cocktailsNonAlcoholic = cocktails.getOrDefault(false,List.of()); |
| | | } |
| | | |
| | | @Primary |
| | |
| | | package de.gedoplan.seminar.sbt.di.exercise.service; |
| | | |
| | | import de.gedoplan.seminar.sbt.di.exercise.domain.Beverage; |
| | | import de.gedoplan.seminar.sbt.di.exercise.domain.Cocktail; |
| | | import de.gedoplan.seminar.sbt.di.exercise.exception.OutOfStockException; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static java.util.function.Predicate.not; |
| | | |
| | | @Component |
| | | public class IngredientService { |
| | | |
| | | public boolean isAvailable(String id, double amount) { |
| | | public List<Beverage> beveragesNotAvailable(Cocktail cocktail) { |
| | | return cocktail.getIngredients().entrySet().stream() |
| | | .filter(not(e -> isAvailable(e.getKey(), e.getValue()))) |
| | | .map(Map.Entry::getKey) |
| | | .toList(); |
| | | } |
| | | |
| | | public boolean isAvailable(Beverage beverage, double amount) { |
| | | // Dummy check: Anything but rum is available |
| | | return !"RUM".equals(id); |
| | | return !"RUM".equals(beverage.getId()); |
| | | } |
| | | } |
| | |
| | | @Test |
| | | public void testFiltersNonAlcEmpty() throws Exception { |
| | | mockMvc.perform(get("/api/bar/nonalc")) |
| | | .andExpect(content().string("")); |
| | | .andExpect(jsonPath("$.size()",is(0))); |
| | | } |
| | | |
| | | @Test |
| | |
| | | |
| | | |
| | | mockMvc.perform(get("/api/methodCount")) |
| | | .andExpect(jsonPath("$.findAll").value(2)); |
| | | .andExpect(jsonPath("$.getCocktails").value(1)); |
| | | |
| | | mockMvc.perform(get("/api/bar/cocktails")) |
| | | .andExpect(status().isOk()); |
| | | |
| | | mockMvc.perform(get("/api/methodCount")) |
| | | .andExpect(jsonPath("$.findAll").value(4)); |
| | | .andExpect(jsonPath("$.getCocktails").value(2)); |
| | | } |
| | | |
| | | } |