Skip to content

Commit

Permalink
Added recipe show page and tests for recipe service and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
IusanMihai committed Mar 3, 2021
1 parent 60b0630 commit 07c08e9
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package guru.springframework.controllers;

import guru.springframework.services.RecipeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RecipeController {

private final RecipeService recipeService;

public RecipeController(RecipeService recipeService) {
this.recipeService = recipeService;
}

@RequestMapping("/recipe/show/{id}")
public String showById(@PathVariable String id, Model model){

model.addAttribute("recipe", recipeService.findById(Long.valueOf(id)));

return "recipe/show";
}
}
12 changes: 8 additions & 4 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ <h1 class="panel-title">Recipe</h1>
<tr>
<th>ID</th>
<th>Description</th>
<th>View</th>
</tr>
</thead>
<tr th:remove="all">
<td th:text="${recipe.id}">123</td>
<td th:text="${recipe.description}">Tasty Goodness</td>
<td>123</td>
<td>Tasty Goodness 1</td>
<td><a href="#">View</a></td>
</tr>
<tr th:remove="all">
<td th:text="${recipe.id}">123</td>
<td th:text="${recipe.description}">Tasty Goodness</td>
<td>123</td>
<td>Tasty Goodness 2</td>
<td><a href="#">View</a></td>
</tr>
<tr th:each="recipe : ${recipes}">
<td th:text="${recipe.id}">123</td>
<td th:text="${recipe.description}">Tasty Goodness</td>
<td><a href="#" th:href="@{'/recipe/show/' + ${recipe.id}}">View</a></td>
</tr>
</table>

Expand Down
134 changes: 134 additions & 0 deletions src/main/resources/templates/recipe/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Show Recipe</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"
th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}">

<script src="/webjars/jquery/1.11.1/jquery.min.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous" th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
</head>
<body>
<!--/*@thymesVar id="recipe" type="guru.springframework.domain.Recipe"*/-->
<div class="container-fluid" style="margin-top: 20px">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="pannel-group">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title" th:text="${recipe.description}">Recipe Description Here!</h1>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<h5>Categories:</h5>
</div>
<div class="col-md-9">
<ul>
<li th:remove="all">cat one</li>
<li th:remove="all">cat two</li>
<li th:each="category : ${recipe.categories}" th:text="${category.getDescription()}">cat three</li>
</ul>
</div>
</div>
<div class="row">
<div class="col-md-3">
<h5>Prep Time:</h5>
</div>
<div class="col-md-3">
<p th:text="${(recipe.prepTime) + ' Min'}">30 min</p>
</div>
<div class="col-md-3">
<h5>Difficulty:</h5>
</div>
<div class="col-md-3">
<p th:text="${recipe.difficulty}">Easy</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<h5>Cooktime:</h5>
</div>
<div class="col-md-3">
<p th:text="${(recipe.cookTime + ' Min')}">30 min</p>
</div>
<div class="col-md-3">
<h5>Servings:</h5>
</div>
<div class="col-md-3">
<p th:text="${recipe.servings}">4</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<h5>Source:</h5>
</div>
<div class="col-md-3">
<p th:text="${recipe.source}">30 min</p>
</div>
<div class="col-md-3">
<h5>URL:</h5>
</div>
<div class="col-md-3">
<p><a href="#" th:href="${recipe.url}" >View Original</a></p>
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title" >Ingredients</h1>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<ul>
<li th:remove="all">1 Cup of milk</li>
<li th:remove="all">1 Teaspoon of chocolate</li>
<li th:each="ingredient : ${recipe.ingredients}"
th:text="${(ingredient.getAmount() +
' ' + ingredient.uom.getDescription() +
' - ' + ingredient.getDescription())}">1 Teaspoon of Sugar</li>
</ul>
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title" >Directions</h1>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<p th:text="${recipe.directions}">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.</p>
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title" >Notes</h1>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<p th:text="${recipe.notes.recipeNotes}">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package guru.springframework.controllers;

import guru.springframework.domain.Recipe;
import guru.springframework.services.RecipeService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@Slf4j
class RecipeControllerTest {

@Mock
RecipeService recipeService;
@InjectMocks
RecipeController controller;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void showById() throws Exception {
//given
Long recipeId = 1L;
Recipe givenRecipe = new Recipe();
givenRecipe.setId(recipeId);
when(recipeService.findById(recipeId)).thenReturn(givenRecipe);
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
//when
//Recipe returnedRecipe = recipeService.findById(recipeId);
//then
mockMvc.perform(get("/recipe/show/" + recipeId.toString()))
.andExpect(status().isOk())
.andExpect(view().name("recipe/show"));

verify(recipeService).findById(recipeId);
verify(recipeService, never()).getRecipes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ void getRecipes() {
verify(recipeRepository, times(1)).findAll();
}

@Test
void getRecipeByIdTest() {
//given
Long recipeId = 1L;
Recipe givenRecipe = new Recipe();
givenRecipe.setId(recipeId);
Optional<Recipe> optionalRecipe = Optional.of(givenRecipe);
when(recipeRepository.findById(recipeId)).thenReturn(optionalRecipe);
//when
Recipe returnedRecipe = recipeService.findById(recipeId);
//then
assertNotNull(returnedRecipe, "Null Recipe Returned");
assertEquals(givenRecipe, returnedRecipe);
verify(recipeRepository, times(1)).findById(recipeId);
verify(recipeRepository, never()).findAll();
}

@Test
void findById() {
//given
Expand Down

0 comments on commit 07c08e9

Please sign in to comment.