Skip to content

Commit

Permalink
fix : sonar vulnerabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 2, 2023
1 parent 556be82 commit 5f5d4e3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
13 changes: 0 additions & 13 deletions boot-opensearch-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>21</java.version>
<spring-cloud.version>2022.0.4</spring-cloud.version>
<springdoc-openapi.version>2.2.0</springdoc-openapi.version>

<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
Expand Down Expand Up @@ -108,18 +107,6 @@
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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> grades) {

public Restaurant toRestaurant() {
return new Restaurant(null, name, borough, cuisine, address, grades);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,8 +24,6 @@
@RequestMapping("/api/restaurants")
public class RestaurantController {

private final Logger log = LoggerFactory.getLogger(this.getClass());

private final RestaurantService restaurantService;

@Autowired
Expand Down Expand Up @@ -69,17 +66,19 @@ public ResponseEntity<Restaurant> 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<Restaurant> 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));
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 5f5d4e3

Please sign in to comment.