Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
VasylyshynDmytro committed Jan 3, 2025
1 parent 7d657c2 commit aed8078
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ $(document).ready(function () {
});
}



function validateForm() {
let isValid = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

import greencity.dto.PageableDto;
import greencity.dto.category.CategoryDto;
import greencity.dto.discount.DiscountValueDto;
import greencity.dto.location.LocationAddressAndGeoForUpdateDto;
import greencity.dto.openhours.OpeningHoursDto;
import greencity.dto.place.AdminPlaceDto;
import greencity.dto.place.PlaceUpdateDto;
import greencity.dto.specification.SpecificationNameDto;
import greencity.service.CategoryService;
import greencity.service.PlaceService;
import greencity.service.SpecificationService;

import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.time.DayOfWeek;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -24,19 +31,32 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.multipart.MultipartFile;

import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.xpath;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
Expand Down Expand Up @@ -99,7 +119,7 @@ void savePlace() throws Exception {
String json = """
{
"placeName": "Тестове місце",
"locationName": "смиків, південна 7",
"locationName": "Смиків, південна 7",
"status": "APPROVED",
"categoryName": "Recycling points",
"discountValues": null,
Expand Down Expand Up @@ -130,52 +150,87 @@ void savePlace() throws Exception {
verify(placeService).addPlaceFromUi(any(), any(), any());
}

// @Test
// void updatePlaceTest() throws Exception {
// PlaceUpdateDto placeUpdateDto = getPlaceUpdateDto();
// ObjectMapper objectMapper = new ObjectMapper();
// String json = objectMapper.writeValueAsString(placeUpdateDto);
// mockMvc.perform(put("/management/places/")
// .content(json)
// .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk());
// verify(placeService).update(placeUpdateDto);
//
// }

// @Test
// void updatePlaceWithoutIdTest() throws Exception {
// String json = """
// {
// "name": "test",
// "category": {
// "name": "Food"
// },
// "discountValues": null,
// "location": {
// "address": "address",
// "lat": 111.1,
// "lng": 111.1
// }
// }
// """;
//
// mockMvc.perform(put("/management/places/")
// .content(json)
// .contentType(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk());
//
// verify(placeService, never()).update(any(PlaceUpdateDto.class));
// }
@Test
void updatePlaceTest() throws Exception {
Principal principal = Mockito.mock(Principal.class);
Mockito.when(principal.getName()).thenReturn("testUser");

PlaceUpdateDto placeUpdateDto = getPlaceUpdateDto();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
String json = objectMapper.writeValueAsString(placeUpdateDto);

MockMultipartFile placeUpdateDtoPart = new MockMultipartFile(
"placeUpdateDto",
"",
MediaType.APPLICATION_JSON_VALUE,
json.getBytes(StandardCharsets.UTF_8));

MockMultipartFile imagePart = new MockMultipartFile(
"images",
"test-image.jpg",
MediaType.IMAGE_JPEG_VALUE,
"image-content".getBytes(StandardCharsets.UTF_8));

this.mockMvc.perform(multipart(HttpMethod.PUT, "/management/places/")
.file(placeUpdateDtoPart)
.file(imagePart)
.principal(principal)
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
.characterEncoding("UTF-8"))
.andExpect(status().isOk());

verify(placeService).updateFromUI(eq(placeUpdateDto), any(MultipartFile[].class), anyString());
}

private PlaceUpdateDto getPlaceUpdateDto() {
PlaceUpdateDto placeUpdateDto = new PlaceUpdateDto();
placeUpdateDto.setId(1L);
placeUpdateDto.setName("test");
placeUpdateDto.setCategory(new CategoryDto("Food", "test", null));
placeUpdateDto.setLocation(new LocationAddressAndGeoForUpdateDto("address", 111.1, 111.1, "адреса"));
return PlaceUpdateDto.builder()
.id(1L)
.name("Test Place")
.location(new LocationAddressAndGeoForUpdateDto(
"Test Address",
50.45,
30.52,
"Тестова адреса"))
.category(new CategoryDto("Food", "Їжа", null))
.openingHoursList(Set.of(new OpeningHoursDto(
LocalTime.of(10, 0),
LocalTime.of(22, 0),
DayOfWeek.FRIDAY,
null)))
.discountValues(Set.of(new DiscountValueDto(10, new SpecificationNameDto("kdf"))))
.build();
}

@Test
void updatePlaceWithoutIdTest() throws Exception {
String json = """
{
"name": "test",
"category": {
"name": "Food"
},
"discountValues": [{"value": 10}],
"location": {
"address": "address",
"lat": 111.1,
"lng": 111.1,
"addressUa": "адреса"
},
"openingHoursList": [{"dayOfWeek": "MONDAY", "openTime": "08:00", "closeTime": "22:00"}]
}
""";

mockMvc.perform(multipart("/management/places/")
.file(new MockMultipartFile(
"placeUpdateDto",
"placeUpdateDto.json",
MediaType.APPLICATION_JSON_VALUE,
json.getBytes()))
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE))
.andExpect(status().isBadRequest());

return placeUpdateDto;
verify(placeService, never()).updateFromUI(any(PlaceUpdateDto.class), any(), any());
}

@Test
Expand Down

0 comments on commit aed8078

Please sign in to comment.