Advertisement
Georgi_Benchev

Untitled

Jan 22nd, 2025
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1.  @Test
  2.     public void createBeer_Should_Return_CreatedBeer_When_ValidBeerDto() throws Exception {
  3.         // Arrange
  4.         BeerDto beerDto = createMockBeerDto();
  5.         Beer mockBeer = createMockBeer();
  6.  
  7.         Mockito.when(beerMapper.fromDto(Mockito.any(BeerDto.class))).thenReturn(createMockBeerFromBeerDto());
  8.         Mockito.when(authenticationHelper.tryGetUser(Mockito.any())).thenReturn(createMockUserAdmin());
  9.         Mockito.when(beerService.create(Mockito.any(Beer.class), Mockito.any(User.class))).thenReturn(mockBeer);
  10.  
  11.         // Act, Assert
  12.         mockMvc.perform(MockMvcRequestBuilders.post("/api/beers")
  13.                         .contentType(MediaType.APPLICATION_JSON)
  14.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  15.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  16.                 .andExpect(MockMvcResultMatchers.status().isCreated())
  17.                 .andExpect(MockMvcResultMatchers.jsonPath("$.name").value("MockBeerName"))
  18.                 .andExpect(MockMvcResultMatchers.jsonPath("$.style.name").value("MockStyleName"))  // Corrected path to style name
  19.                 .andExpect(MockMvcResultMatchers.jsonPath("$.abv").value(5.5));
  20.     }
  21.  
  22.     @Test
  23.     public void createBeer_ShouldReturn_NotFound_When_StyleId_IsInvalid() throws Exception {
  24.         // Arrange
  25.         Mockito.when(beerMapper.fromDto(Mockito.any(BeerDto.class))).thenThrow(EntityNotFoundException.class);
  26.  
  27.         // Act, Assert
  28.         mockMvc.perform(MockMvcRequestBuilders.post("/api/beers")
  29.                         .contentType(MediaType.APPLICATION_JSON)
  30.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  31.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  32.                 .andExpect(MockMvcResultMatchers.status().isNotFound());
  33.     }
  34.  
  35.     @Test
  36.     public void createBeer_ShouldReturn_CONFLICT_When_DuplicateName() throws Exception {
  37.         // Arrange
  38.         Mockito.when(beerService.create(Mockito.any(), Mockito.any()))
  39.                 .thenThrow(DuplicateEntityException.class);
  40.  
  41.         // Act, Assert
  42.         mockMvc.perform(MockMvcRequestBuilders.post("/api/beers")
  43.                         .contentType(MediaType.APPLICATION_JSON)
  44.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  45.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  46.                 .andExpect(MockMvcResultMatchers.status().isConflict());
  47.     }
  48.  
  49.     @Test
  50.     public void createBeer_ShouldReturn_Unauthorized_When_UnauthorizedExcThrown() throws Exception {
  51.         // Arrange
  52.         Mockito.when(authenticationHelper.tryGetUser(Mockito.any()))
  53.                 .thenThrow(UnauthorizedAccessException.class);
  54.  
  55.         // Act, Assert
  56.         mockMvc.perform(MockMvcRequestBuilders.post("/api/beers")
  57.                         .contentType(MediaType.APPLICATION_JSON)
  58.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  59.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  60.                 .andExpect(MockMvcResultMatchers.status().isUnauthorized());
  61.     }
  62.  
  63.  
  64.     // UPDATE
  65.  
  66.  
  67.     @Test
  68.     public void updateBeer_ShouldReturn_UpdatedBeer_When_ValidRequest() throws Exception {
  69.         // Arrange
  70.         Beer mockBeer = createMockBeerFromBeerDtoWithId();
  71.  
  72.         Mockito.when(beerMapper.fromDto(Mockito.anyInt(), Mockito.any(BeerDto.class))).thenReturn(mockBeer);
  73.         Mockito.when(authenticationHelper.tryGetUser(Mockito.any())).thenReturn(createMockUserAdmin());
  74.         Mockito.when(beerService.update(Mockito.any(Beer.class), Mockito.any(User.class))).thenReturn(mockBeer);
  75.  
  76.         // Act & Assert
  77.         mockMvc.perform(MockMvcRequestBuilders.put("/api/beers/{id}", 1)
  78.                         .contentType(MediaType.APPLICATION_JSON)
  79.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  80.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  81.                 .andExpect(MockMvcResultMatchers.status().isOk())
  82.                 .andExpect(MockMvcResultMatchers.jsonPath("$.name").value("MockBeerName"))
  83.                 .andExpect(MockMvcResultMatchers.jsonPath("$.style.name").value("MockStyleName"))
  84.                 .andExpect(MockMvcResultMatchers.jsonPath("$.abv").value(5.5));
  85.     }
  86.  
  87.     @Test
  88.     public void updateBeer_ShouldReturn_NotFound_When_StyleId_IsInvalid() throws Exception {
  89.         // Arrange
  90.         Mockito.when(beerMapper.fromDto(Mockito.anyInt(), Mockito.any(BeerDto.class)))
  91.                 .thenThrow(EntityNotFoundException.class);
  92.  
  93.         // Act, Assert
  94.         mockMvc.perform(MockMvcRequestBuilders.put("/api/beers/{id}", 1)
  95.                         .contentType(MediaType.APPLICATION_JSON)
  96.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  97.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  98.                 .andExpect(MockMvcResultMatchers.status().isNotFound());
  99.     }
  100.  
  101.     @Test
  102.     public void updateBeer_ShouldReturn_CONFLICT_When_DuplicateName() throws Exception {
  103.         // Arrange
  104.         Mockito.when(beerService.update(Mockito.any(), Mockito.any()))
  105.                 .thenThrow(DuplicateEntityException.class);
  106.  
  107.         // Act, Assert
  108.         mockMvc.perform(MockMvcRequestBuilders.put("/api/beers/{id}", 1)
  109.                         .contentType(MediaType.APPLICATION_JSON)
  110.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  111.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  112.                 .andExpect(MockMvcResultMatchers.status().isConflict());
  113.     }
  114.  
  115.     @Test
  116.     public void updateBeer_ShouldReturn_Unauthorized_When_UnauthorizedExcThrown() throws Exception {
  117.         // Arrange
  118.         Mockito.when(authenticationHelper.tryGetUser(Mockito.any()))
  119.                 .thenThrow(UnauthorizedAccessException.class);
  120.  
  121.         // Act, Assert
  122.         mockMvc.perform(MockMvcRequestBuilders.put("/api/beers/{id}", 1)
  123.                         .contentType(MediaType.APPLICATION_JSON)
  124.                         .content("{\"name\": \"MockBeerName\", \"abv\": 5.5, \"styleId\": 1}")
  125.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  126.                 .andExpect(MockMvcResultMatchers.status().isUnauthorized());
  127.     }
  128.  
  129.     // DELETE
  130.  
  131.     @Test
  132.     public void deleteBeer_ShouldReturn_StatusOK_When_ValidRequest() throws Exception {
  133.         // Arrange
  134.         Mockito.when(authenticationHelper.tryGetUser(Mockito.any())).thenReturn(createMockUserAdmin());
  135.         Mockito.when(beerService.delete(Mockito.anyInt(), Mockito.any(User.class))).thenReturn(createMockBeer());
  136.  
  137.         // Act & Assert
  138.         mockMvc.perform(MockMvcRequestBuilders.delete("/api/beers/{id}", 1)
  139.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  140.                 .andExpect(MockMvcResultMatchers.status().isOk());
  141.     }
  142.  
  143.     @Test
  144.     public void deleteBeer_ShouldReturn_NotFound_When_BeerId_IsInvalid() throws Exception {
  145.         // Arrange
  146.         Mockito.when(beerService.delete(Mockito.anyInt(), Mockito.any()))
  147.                 .thenThrow(EntityNotFoundException.class);
  148.  
  149.         // Act, Assert
  150.         mockMvc.perform(MockMvcRequestBuilders.delete("/api/beers/{id}", 1)
  151.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  152.                 .andExpect(MockMvcResultMatchers.status().isNotFound());
  153.     }
  154.  
  155.  
  156.     @Test
  157.     public void deleteBeer_ShouldReturn_Unauthorized_When_UnauthorizedExcThrown() throws Exception {
  158.         // Arrange
  159.         Mockito.when(authenticationHelper.tryGetUser(Mockito.any()))
  160.                 .thenThrow(UnauthorizedAccessException.class);
  161.  
  162.         // Act, Assert
  163.         mockMvc.perform(MockMvcRequestBuilders.delete("/api/beers/{id}", 1)
  164.                         .header(HttpHeaders.AUTHORIZATION, "valid"))
  165.                 .andExpect(MockMvcResultMatchers.status().isUnauthorized());
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement