Skip to content

Commit

Permalink
16/12: test in CategoryControllerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan committed Dec 16, 2023
1 parent 01aa06d commit 368bfdb
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/test/java/com/spotify/app/album/AlbumControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.spotify.app.controller.AlbumController;
import com.spotify.app.dto.AlbumDTO;
import com.spotify.app.dto.request.AlbumRequest;
import com.spotify.app.dto.response.SongResponse;
import com.spotify.app.service.AlbumService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -18,8 +17,6 @@
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand All @@ -44,8 +41,7 @@ public class AlbumControllerTest {
@MockBean
private AlbumService albumService;

// @MockBean
// private AuthenticationProvider authenticationProvider;
private final String prefixUrl = "/api/v1/album";


private AlbumDTO albumDTO ;
Expand All @@ -60,7 +56,7 @@ void canGetAlbumById () throws Exception {
when(albumService.findById(1L)).thenReturn(albumDTO);

// then
this.mockMvc.perform(get("/api/v1/album/1"))
this.mockMvc.perform(get(prefixUrl+"/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.notNullValue(AlbumDTO.class)))
.andExpect(jsonPath("$.name", Matchers.is(albumDTO.name())))
Expand Down
114 changes: 114 additions & 0 deletions src/test/java/com/spotify/app/category/CategoryControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,118 @@
package com.spotify.app.category;


import com.spotify.app.controller.CategoryController;
import com.spotify.app.dto.AlbumDTO;
import com.spotify.app.dto.CategoryDTO;
import com.spotify.app.exception.ResourceNotFoundException;
import com.spotify.app.service.CategoryService;
import com.spotify.app.service.SearchService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import java.util.HashSet;
import java.util.Set;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(
controllers = CategoryController.class,
excludeAutoConfiguration = {
UserDetailsServiceAutoConfiguration.class, SecurityAutoConfiguration.class
})
public class CategoryControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CategoryService categoryService;

@MockBean
private SearchService searchService;

private CategoryDTO categoryParentDTO ;
private CategoryDTO categoryChild1DTO ;
private CategoryDTO categoryChild2DTO ;

private String prefixUrl = "/api/v1/category";


@BeforeEach
public void setUp () {
categoryParentDTO = new CategoryDTO(1, "category parent", true, "image.png","thumb.png",null);
categoryChild1DTO = new CategoryDTO(2, "category child1", true, "image.png","thumb.png",null);
categoryChild2DTO = new CategoryDTO(3, "category child2", true, "image.png","thumb.png",null);
}

// find all child by parent
@Test
public void canFillAllChildByParent () throws Exception {
// given
Set<CategoryDTO> categoryDTOSet = new HashSet<>();
categoryDTOSet.add(categoryChild1DTO);
categoryDTOSet.add(categoryChild2DTO);
Integer categoryParentId = 1 ;
// when
when(categoryService.listByParentId(categoryParentId)).thenReturn(categoryDTOSet);

// then
String url = String.format(prefixUrl+"/getChildBy/%d", categoryParentId);
this.mockMvc.
perform(get(url))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].title", Matchers.is(categoryChild1DTO.title())));
}

// get by id
@Test
public void canGetById () throws Exception {

// given
Integer categoryId = 1;
String url = String.format(prefixUrl.concat("/%d"), categoryId);

// when
when(categoryService.findByIdCustom(categoryId)).thenReturn(categoryParentDTO);

// then
this.mockMvc
.perform(get(url))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title", Matchers.is(categoryParentDTO.title())));


}
// get by id fail
@Test
public void cannotGetById () throws Exception {
// given
Integer categoryId = 0;
String url = String.format(prefixUrl.concat("/%d"), categoryId);

// when
when(categoryService.findByIdCustom(categoryId)).thenThrow(new ResourceNotFoundException("category not found"));

// then
String expectMessage = "category not found";
this.mockMvc
.perform(get(url))
.andExpect(status().isNotFound())
.andDo(print())
.andExpect(jsonPath("$.errors[0]", Matchers.is(expectMessage)));


}
}

0 comments on commit 368bfdb

Please sign in to comment.