diff --git a/boot-opensearch-sample/pom.xml b/boot-opensearch-sample/pom.xml index bc98e6348..fc446b13c 100644 --- a/boot-opensearch-sample/pom.xml +++ b/boot-opensearch-sample/pom.xml @@ -21,7 +21,6 @@ UTF-8 21 - 2022.0.4 2.2.0 ${project.build.directory}/test-results @@ -108,18 +107,6 @@ - - - - org.springframework.cloud - spring-cloud-dependencies - ${spring-cloud.version} - pom - import - - - - diff --git a/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java b/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java index 3358e29b3..2f03d90c2 100644 --- a/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java +++ b/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java @@ -1,7 +1,5 @@ package com.example.opensearch.entities; -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotEmpty; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -17,14 +15,11 @@ public class Restaurant { @Field(fielddata = true, type = FieldType.Text) private String id; - @NotEmpty(message = "Name cannot be empty") private String name; - @NotBlank(message = "Borough Can't be Blank") @Field(fielddata = true, type = FieldType.Text) private String borough; - @NotBlank(message = "Cuisine Can't be Blank") @Field(fielddata = true, type = FieldType.Text) private String cuisine; diff --git a/boot-opensearch-sample/src/main/java/com/example/opensearch/model/request/RestaurantRequest.java b/boot-opensearch-sample/src/main/java/com/example/opensearch/model/request/RestaurantRequest.java new file mode 100644 index 000000000..28d0c5b3d --- /dev/null +++ b/boot-opensearch-sample/src/main/java/com/example/opensearch/model/request/RestaurantRequest.java @@ -0,0 +1,20 @@ +package com.example.opensearch.model.request; + +import com.example.opensearch.entities.Address; +import com.example.opensearch.entities.Grades; +import com.example.opensearch.entities.Restaurant; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import java.util.List; + +public record RestaurantRequest( + @NotEmpty(message = "Name cannot be empty") String name, + @NotBlank(message = "Borough Can't be Blank") String borough, + @NotBlank(message = "Cuisine Can't be Blank") String cuisine, + Address address, + List grades) { + + public Restaurant toRestaurant() { + return new Restaurant(null, name, borough, cuisine, address, grades); + } +} diff --git a/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java b/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java index 3fb665efc..162c13274 100644 --- a/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java +++ b/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java @@ -1,11 +1,10 @@ package com.example.opensearch.web.controllers; import com.example.opensearch.entities.Restaurant; +import com.example.opensearch.model.request.RestaurantRequest; import com.example.opensearch.model.response.PagedResult; import com.example.opensearch.services.RestaurantService; import com.example.opensearch.utils.AppConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -25,8 +24,6 @@ @RequestMapping("/api/restaurants") public class RestaurantController { - private final Logger log = LoggerFactory.getLogger(this.getClass()); - private final RestaurantService restaurantService; @Autowired @@ -69,17 +66,19 @@ public ResponseEntity getRestaurantById(@PathVariable String id) { @PostMapping @ResponseStatus(HttpStatus.CREATED) - public Restaurant createRestaurant(@RequestBody @Validated Restaurant restaurant) { - return restaurantService.saveRestaurant(restaurant); + public Restaurant createRestaurant( + @RequestBody @Validated RestaurantRequest restaurantRequest) { + return restaurantService.saveRestaurant(restaurantRequest.toRestaurant()); } @PutMapping("/{id}") public ResponseEntity updateRestaurant( - @PathVariable String id, @RequestBody Restaurant restaurant) { + @PathVariable String id, @RequestBody RestaurantRequest restaurantRequest) { return restaurantService .findRestaurantById(id) .map( restaurantObj -> { + Restaurant restaurant = restaurantRequest.toRestaurant(); restaurant.setId(id); return ResponseEntity.ok(restaurantService.saveRestaurant(restaurant)); }) diff --git a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java index d9635f71c..b51219611 100644 --- a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java +++ b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java @@ -15,6 +15,7 @@ import com.example.opensearch.entities.Address; import com.example.opensearch.entities.Grades; import com.example.opensearch.entities.Restaurant; +import com.example.opensearch.model.request.RestaurantRequest; import com.example.opensearch.repositories.RestaurantRepository; import java.time.LocalDateTime; import java.util.ArrayList; @@ -87,22 +88,22 @@ void shouldCreateNewRestaurant() throws Exception { address.setLocation(new Point(-73.8, 40.8)); address.setStreet("street1"); Grades grade = new Grades("1", LocalDateTime.now(), 5); - Restaurant restaurant = - new Restaurant( - null, "New Restaurant", "borough1", "cuisine1", address, List.of(grade)); + RestaurantRequest restaurantRequest = + new RestaurantRequest( + "New Restaurant", "borough1", "cuisine1", address, List.of(grade)); this.mockMvc .perform( post("/api/restaurants") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(restaurant))) + .content(objectMapper.writeValueAsString(restaurantRequest))) .andExpect(status().isCreated()) .andExpect(jsonPath("$.id", notNullValue())) - .andExpect(jsonPath("$.name", is(restaurant.getName()))); + .andExpect(jsonPath("$.name", is(restaurantRequest.name()))); } @Test void shouldReturn400WhenCreateNewRestaurantWithoutText() throws Exception { - Restaurant restaurant = new Restaurant(null, null, null, null, null, null); + RestaurantRequest restaurant = new RestaurantRequest(null, null, null, null, null); this.mockMvc .perform( diff --git a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java index f8c35183d..ab8250807 100644 --- a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java +++ b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java @@ -2,7 +2,6 @@ import static com.example.opensearch.utils.AppConstants.PROFILE_TEST; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.Matchers.hasSize; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; @@ -18,6 +17,7 @@ import com.example.opensearch.entities.Address; import com.example.opensearch.entities.Grades; import com.example.opensearch.entities.Restaurant; +import com.example.opensearch.model.request.RestaurantRequest; import com.example.opensearch.model.response.PagedResult; import com.example.opensearch.services.RestaurantService; import com.fasterxml.jackson.databind.ObjectMapper; @@ -131,13 +131,13 @@ void shouldCreateNewRestaurant() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(restaurant))) .andExpect(status().isCreated()) - .andExpect(jsonPath("$.id", notNullValue())) .andExpect(jsonPath("$.name", is(restaurant.getName()))); } @Test - void shouldReturn400WhenCreateNewRestaurantWithoutText() throws Exception { - Restaurant restaurant = new Restaurant(null, null, null, null, null, null); + void shouldReturn400WhenCreateNewRestaurantWithoutRequiredFields() throws Exception { + RestaurantRequest restaurant = + new RestaurantRequest(null, null, null, null, new ArrayList<>()); this.mockMvc .perform(