From f763089e385b09d3bb6d428ae59241396fde7c56 Mon Sep 17 00:00:00 2001 From: Martin Rojas Miguel Angel Date: Thu, 21 Jun 2018 16:31:07 +0200 Subject: [PATCH] TSK-531 Access id validation failing refinement --- .../AccessIdValidationControllerTest.java | 30 +++++++++++++++++-- .../rest/GenenalExceptionHandlingTest.java | 13 ++++---- .../pro/taskana/rest/AccessIdController.java | 6 ++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/AccessIdValidationControllerTest.java b/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/AccessIdValidationControllerTest.java index 9a14b591f..204127abc 100644 --- a/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/AccessIdValidationControllerTest.java +++ b/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/AccessIdValidationControllerTest.java @@ -1,5 +1,6 @@ package pro.taskana.rest; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -19,21 +20,25 @@ import org.springframework.hateoas.hal.Jackson2HalModule; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import pro.taskana.exceptions.InvalidArgumentException; import pro.taskana.ldap.LdapCacheTestImpl; import pro.taskana.rest.resource.AccessIdResource; @RunWith(SpringRunner.class) -@SpringBootTest(classes = RestConfiguration.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {"devMode=true"}) +@SpringBootTest(classes = RestConfiguration.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = { + "devMode=true"}) public class AccessIdValidationControllerTest { @LocalServerPort @@ -49,6 +54,7 @@ public class AccessIdValidationControllerTest { ResponseEntity> response = template.exchange( "http://127.0.0.1:" + port + "/v1/access-ids?searchFor=ali", HttpMethod.GET, request, new ParameterizedTypeReference>() { + }); List body = response.getBody(); assertNotNull(body); @@ -59,6 +65,26 @@ public class AccessIdValidationControllerTest { } } + @Test + public void testBadRequestWhenSearchForIsTooShort() { + AccessIdController.setLdapCache(new LdapCacheTestImpl()); + RestTemplate template = getRestTemplate(); + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); + HttpEntity request = new HttpEntity(headers); + try { + template.exchange( + "http://127.0.0.1:" + port + "/v1/access-ids?searchFor=al", HttpMethod.GET, request, + new ParameterizedTypeReference>() { + + }); + } catch (HttpClientErrorException e) { + assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode()); + assertTrue(e.getResponseBodyAsString().contains("Minimum searchFor length =")); + } + + } + /** * Return a REST template which is capable of dealing with responses in HAL format * @@ -73,7 +99,7 @@ public class AccessIdValidationControllerTest { converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json")); converter.setObjectMapper(mapper); - RestTemplate template = new RestTemplate(Collections.> singletonList(converter)); + RestTemplate template = new RestTemplate(Collections.>singletonList(converter)); return template; } diff --git a/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/GenenalExceptionHandlingTest.java b/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/GenenalExceptionHandlingTest.java index 71b8e95f0..a8079fd3f 100644 --- a/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/GenenalExceptionHandlingTest.java +++ b/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/GenenalExceptionHandlingTest.java @@ -89,28 +89,31 @@ public class GenenalExceptionHandlingTest { try { AccessIdController.setLdapCache(new LdapCacheTestImpl()); - ResponseEntity> response = template.exchange( + template.exchange( server + port + "/v1/access-ids?searchFor=al", HttpMethod.GET, request, new ParameterizedTypeReference>() { }); } catch (Exception ex) { verify(mockAppender).doAppend(captorLoggingEvent.capture()); - assertTrue(captorLoggingEvent.getValue().getMessage().contains("is too short. Minimum Length =")); + assertTrue( + captorLoggingEvent.getValue().getMessage().contains("is too short. Minimum searchFor length = ")); } } @Test public void testDeleteNonExisitingClassificationExceptionIsLogged() { try { - ResponseEntity> response = template.exchange( + template.exchange( server + port + "/v1/classifications/non-existing-id", HttpMethod.DELETE, request, new ParameterizedTypeReference>() { }); - } catch (Exception ex){ + } catch (Exception ex) { verify(mockAppender).doAppend(captorLoggingEvent.capture()); - assertTrue(captorLoggingEvent.getValue().getMessage().contains("The classification \"non-existing-id\" wasn't found")); + assertTrue(captorLoggingEvent.getValue() + .getMessage() + .contains("The classification \"non-existing-id\" wasn't found")); } } diff --git a/rest/taskana-rest-spring/src/main/java/pro/taskana/rest/AccessIdController.java b/rest/taskana-rest-spring/src/main/java/pro/taskana/rest/AccessIdController.java index e8a0147f5..9dcbaf03d 100644 --- a/rest/taskana-rest-spring/src/main/java/pro/taskana/rest/AccessIdController.java +++ b/rest/taskana-rest-spring/src/main/java/pro/taskana/rest/AccessIdController.java @@ -35,9 +35,9 @@ public class AccessIdController { @GetMapping public ResponseEntity> validateAccessIds( - @RequestParam(required = false) String searchFor) throws InvalidArgumentException { - if (searchFor == null || searchFor.length() < ldapClient.getMinSearchForLength()) { - throw new InvalidArgumentException("searchFor string " + searchFor + " is too short. Minimum Length = " + @RequestParam String searchFor) throws InvalidArgumentException { + if (searchFor.length() < ldapClient.getMinSearchForLength()) { + throw new InvalidArgumentException("searchFor string '" + searchFor + "' is too short. Minimum searchFor length = " + ldapClient.getMinSearchForLength()); } if (ldapClient.useLdap()) {