-
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.
Added recipe show page and tests for recipe service and controller
- Loading branch information
1 parent
60b0630
commit 07c08e9
Showing
5 changed files
with
234 additions
and
4 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/guru/springframework/controllers/RecipeController.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,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"; | ||
} | ||
} |
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 |
---|---|---|
@@ -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> |
50 changes: 50 additions & 0 deletions
50
src/test/java/guru/springframework/controllers/RecipeControllerTest.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,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(); | ||
} | ||
} |
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