Commit 29bd981c authored by Tika Lestari [SLB]'s avatar Tika Lestari [SLB] Committed by Rostislav Vatolin [SLB]
Browse files

Return 400 on invalid request body for PUT/POST apis and handle CosmosDB 429 error

parent a637c4e0
......@@ -138,25 +138,19 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<version>0.8.6</version>
<executions>
<execution>
<id>prepare-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-agent</id>
<id>report</id>
<phase>test</phase>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
</executions>
</plugin>
......
package org.opengroup.osdu.entitlements.v2.config;
import com.fasterxml.jackson.databind.DeserializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.MapperFeature;
......@@ -14,6 +15,7 @@ public class ConfigMapper {
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
}
\ No newline at end of file
......@@ -44,7 +44,8 @@ public class SpringExceptionMapper extends ResponseEntityExceptionHandler {
.stream()
.map(fieldError -> fieldError.getField() + " " + fieldError.getDefaultMessage())
.collect(Collectors.toList());
return this.getErrorResponse(new AppException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), errorList.toString(), ex));
String errorMessage = String.join(", ", errorList);
return this.getErrorResponse(new AppException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), errorMessage, ex));
}
@ExceptionHandler({ValidationException.class, JsonProcessingException.class, UnrecognizedPropertyException.class})
......
......@@ -7,12 +7,16 @@ import lombok.Data;
import lombok.Generated;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
@Data
@Builder
@Generated
@NoArgsConstructor
@AllArgsConstructor
public class AddMemberDto {
@NotNull
private String email;
@NotNull
private Role role;
}
......@@ -9,6 +9,7 @@ import lombok.Generated;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
@Data
......@@ -16,7 +17,8 @@ import javax.validation.constraints.Pattern;
@NoArgsConstructor
@AllArgsConstructor
public class CreateGroupDto {
@Pattern(regexp ="^[A-Za-z0-9{}_.-]{3,128}$")
@Pattern(regexp = "^[A-Za-z0-9{}_.-]{3,128}$")
@NotNull
private String name;
@Length(min = 0, max = 256)
private String description;
......
......@@ -132,7 +132,7 @@ public class AddMemberApiTest {
}
@Test
public void should_callService_withLowerCasedMemberAndGroupEmail() throws Exception {
public void shouldCallServiceWithLowerCasedMemberAndGroupEmail() throws Exception {
ArgumentCaptor<AddMemberDto> captor1 = ArgumentCaptor.forClass(AddMemberDto.class);
ArgumentCaptor<AddMemberServiceDto> captor2 = ArgumentCaptor.forClass(AddMemberServiceDto.class);
......@@ -158,6 +158,22 @@ public class AddMemberApiTest {
assertThat(captor2.getValue().getPartitionId()).isEqualTo("common");
}
@Test
public void shouldReturnBadRequestGivenInvalidInput() throws Exception {
String body = "{\"test\":\"test\"}";
String group = "service.VIEWERS.users@common.contoso.com";
ResultActions resultActions = mockMvc.perform(post("/groups/{group_email}/members", group)
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("UTF8")
.header(DpsHeaders.AUTHORIZATION, "Bearer ew0KICAidHlwIjogIkpXVCIsDQogICJhbGciOiAiUlMyNTYiLA0KICAia2lkIjogIk1UVTVNRFUxTkRrNU9BPT0iDQp9.ew0KICAic3ViIjogImFAYi5jb20iLA0KICAiaXNzIjogInByZXZpZXcuY29tIiwNCiAgImF1ZCI6ICJ0ZXN0LmNvbSIsDQogICJpYXQiOiAxNTkwNjAwODI0LA0KICAiZXhwIjogMTU5MDY4NzIyNCwNCiAgInByb3ZpZGVyIjogImEuY29tIiwNCiAgImNsaWVudCI6ICJ0ZXN0LmNvbSIsDQogICJ1c2VyaWQiOiAiYUBiLmNvbSIsDQogICJlbWFpbCI6ICJhQGIuY29tIiwNCiAgImF1dGh6IjogIiIsDQogICJsYXN0bmFtZSI6ICJCIiwNCiAgImZpcnN0bmFtZSI6ICJBIiwNCiAgImNvdW50cnkiOiAiIiwNCiAgImNvbXBhbnkiOiAiIiwNCiAgImpvYnRpdGxlIjogIiIsDQogICJzdWJpZCI6ICJ1NUxTTkp1aFVmYUgweFAzdVlUbkl4Vk9BSjR4NkRKdWNXc3BzNWdEb280IiwNCiAgImlkcCI6ICJvMzY1IiwNCiAgImhkIjogInNsYi5jb20iLA0KICAiZGVzaWQiOiAiYUBkZXNpZC5jb20iLA0KICAiY29udGFjdF9lbWFpbCI6ICJhQGIuY29tIiwNCiAgInJ0X2hhc2giOiAieVMxcHY3a0NvaTZHVld2c3c4S3F5QSINCn0.tvE00W8cZZOZZDc83Sn4nKPBlw3boJEjJaTvOyvMXmNSTB4BN7kdLnhXy_CLQ4FZy0Y-PMboMGCH9gfKT1bYcQHllUTLnjtzd0iBJWY-I0ahoMEIa1PkksCytBz5qBGunrwr28PqW_t6GN99pUn0zxFn2022C17fnDHGdS1G2Tlag0Jpadl2PgdN_V9u2BndHgkKCFlmS2ZmX59KWQCOJmwnTd1k8vXCpUgDVjBK5CzCb9aFp8pjdy0mdMeV-7hYE2acyIrTPVZwAMAgHFdA")
.header(DpsHeaders.DATA_PARTITION_ID, "common")
.header(DpsHeaders.USER_ID,"a@b.com")
.content(body));
resultActions.andExpect(status().isBadRequest());
}
private ResultActions performRequest(AddMemberDto dto, String groupEmail) throws Exception {
return mockMvc.perform(post("/groups/{group_email}/members", groupEmail)
.contentType(MediaType.APPLICATION_JSON)
......
......@@ -129,6 +129,21 @@ public class CreateGroupApiTest {
assertThat(captor1.getValue().getName()).isEqualTo("service.viewers");
}
@Test
public void shouldReturnBadRequestGivenInvalidInput() throws Exception {
String body = "{\"test\":\"test\"}";
ResultActions resultActions = mockMvc.perform(post("/groups")
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("UTF8")
.header(DpsHeaders.AUTHORIZATION, "Bearer ew0KICAidHlwIjogIkpXVCIsDQogICJhbGciOiAiUlMyNTYiLA0KICAia2lkIjogIk1UVTVNRFUxTkRrNU9BPT0iDQp9.ew0KICAic3ViIjogImFAYi5jb20iLA0KICAiaXNzIjogInByZXZpZXcuY29tIiwNCiAgImF1ZCI6ICJ0ZXN0LmNvbSIsDQogICJpYXQiOiAxNTkwNjAwODI0LA0KICAiZXhwIjogMTU5MDY4NzIyNCwNCiAgInByb3ZpZGVyIjogImEuY29tIiwNCiAgImNsaWVudCI6ICJ0ZXN0LmNvbSIsDQogICJ1c2VyaWQiOiAiYUBiLmNvbSIsDQogICJlbWFpbCI6ICJhQGIuY29tIiwNCiAgImF1dGh6IjogIiIsDQogICJsYXN0bmFtZSI6ICJCIiwNCiAgImZpcnN0bmFtZSI6ICJBIiwNCiAgImNvdW50cnkiOiAiIiwNCiAgImNvbXBhbnkiOiAiIiwNCiAgImpvYnRpdGxlIjogIiIsDQogICJzdWJpZCI6ICJ1NUxTTkp1aFVmYUgweFAzdVlUbkl4Vk9BSjR4NkRKdWNXc3BzNWdEb280IiwNCiAgImlkcCI6ICJvMzY1IiwNCiAgImhkIjogInNsYi5jb20iLA0KICAiZGVzaWQiOiAiYUBkZXNpZC5jb20iLA0KICAiY29udGFjdF9lbWFpbCI6ICJhQGIuY29tIiwNCiAgInJ0X2hhc2giOiAieVMxcHY3a0NvaTZHVld2c3c4S3F5QSINCn0.tvE00W8cZZOZZDc83Sn4nKPBlw3boJEjJaTvOyvMXmNSTB4BN7kdLnhXy_CLQ4FZy0Y-PMboMGCH9gfKT1bYcQHllUTLnjtzd0iBJWY-I0ahoMEIa1PkksCytBz5qBGunrwr28PqW_t6GN99pUn0zxFn2022C17fnDHGdS1G2Tlag0Jpadl2PgdN_V9u2BndHgkKCFlmS2ZmX59KWQCOJmwnTd1k8vXCpUgDVjBK5CzCb9aFp8pjdy0mdMeV-7hYE2acyIrTPVZwAMAgHFdA")
.header(DpsHeaders.DATA_PARTITION_ID, "common")
.header(DpsHeaders.USER_ID,"a@b.com")
.content(body));
resultActions.andExpect(status().isBadRequest());
}
private ResultActions performCreateGroupRequest(CreateGroupDto dto) throws Exception {
return mockMvc.perform(post("/groups")
.contentType(MediaType.APPLICATION_JSON)
......
......@@ -213,6 +213,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
......@@ -168,6 +168,12 @@ public class ClusterGremlinConnector implements GremlinConnector {
HttpStatus.NOT_FOUND.getReasonPhrase(),
RESOURCE_NOT_FOUND_ERROR_MESSAGE, e);
}
if (e.getMessage().contains("Request rate is large")) {
throw new AppException(
HttpStatus.TOO_MANY_REQUESTS.value(),
HttpStatus.TOO_MANY_REQUESTS.getReasonPhrase(),
RETRIEVING_RESULT_SET_ERROR_MESSAGE, e);
}
throw new AppException(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment