Package com.streamreduce.rest.dto.response

Examples of com.streamreduce.rest.dto.response.ConstraintViolationExceptionResponseDTO


            userService.createUser(user);

        } catch (UserNotFoundException e) {
            return error("Invalid Invite " + inviteKey + ":" + accountId, Response.status(Response.Status.NOT_FOUND));
        } catch (InvalidUserAliasException e) {
            ConstraintViolationExceptionResponseDTO dto = new ConstraintViolationExceptionResponseDTO();
            dto.setViolations(ImmutableMap.of("alias", e.getMessage()));
            return Response.status(Response.Status.BAD_REQUEST).entity(dto).build();
        }

        // user created, they can now login
        return Response
View Full Code Here


            userService.createUser(user);

        } catch (UserNotFoundException e) {
            return error("Invalid Signup " + signupKey + ":" + userId, Response.status(Response.Status.NOT_FOUND));
        } catch (InvalidUserAliasException e) {
            ConstraintViolationExceptionResponseDTO dto = new ConstraintViolationExceptionResponseDTO();
            dto.setViolations(ImmutableMap.of("alias", e.getMessage()));
            return Response.status(Response.Status.BAD_REQUEST).entity(dto).build();
        }

        return Response
                .status(Response.Status.CREATED)
View Full Code Here

    public Response toResponse(ConstraintViolationException exception) {
        return validationError(exception.getConstraintViolations());
    }

    public Response validationError(Set<ConstraintViolation<?>> violations) {
        ConstraintViolationExceptionResponseDTO dto = new ConstraintViolationExceptionResponseDTO();
        Map<String, String> violationsMap = new HashMap<>();

        // Not sure if it's possible to have multiple messages per property but if so, testing should uncover
        // it and the fix is quick/simple.
        for (ConstraintViolation<?> violation : violations) {
            String propertyPath = violation.getPropertyPath().toString();
            String violationMessage = violation.getMessage();

            // The property name is what is important, not the full path since most consumers will be RESTful
            if (propertyPath.endsWith(".") || propertyPath.length() == 0) {
                // We have a few @ScriptAssert validations which do not allow you to specify the property path.
                // Based on convention, the message will always start with the property name so we can parse it here.
                propertyPath = violationMessage.substring(0, violationMessage.indexOf(' '));
            } else if (propertyPath.indexOf('.') > -1) {
                propertyPath = propertyPath.substring(propertyPath.lastIndexOf('.'));
            }

            violationsMap.put(propertyPath, violationMessage);
        }

        dto.setViolations(violationsMap);

        return Response.status(Response.Status.BAD_REQUEST).entity(dto).build();
    }
View Full Code Here

    @Test
    @Ignore
    public void testValidation() throws Exception {
        JSONObject json = new JSONObject();
        ConstraintViolationExceptionResponseDTO exceptionDTO;

        String req = makeRequest(connectionsBaseUrl, "POST", null, authnToken);
        exceptionDTO = jsonToObject(req,
                TypeFactory.defaultInstance().constructType(ConstraintViolationExceptionResponseDTO.class));

        assertEquals(3, exceptionDTO.getViolations().keySet().size());

        // Test invalid name
        json.put("alias", "a");

        exceptionDTO = jsonToObject(makeRequest(connectionsBaseUrl, "POST", json, authnToken),
                TypeFactory.defaultInstance().constructType(ConstraintViolationExceptionResponseDTO.class));

        assertEquals(3, exceptionDTO.getViolations().keySet().size());

        json.put("alias", "GitHub Feed");

        exceptionDTO = jsonToObject(makeRequest(connectionsBaseUrl, "POST", json, authnToken),
                TypeFactory.defaultInstance().constructType(ConstraintViolationExceptionResponseDTO.class));

        assertEquals(2, exceptionDTO.getViolations().keySet().size());

        json.put("type", FeedProvider.TYPE);

        exceptionDTO = jsonToObject(makeRequest(connectionsBaseUrl, "POST", json, authnToken),
                TypeFactory.defaultInstance().constructType(ConstraintViolationExceptionResponseDTO.class));

        assertEquals(1, exceptionDTO.getViolations().keySet().size());

        // Test invalid url
        json.put("url", "h:/feeds.feedburner.com/github");

        exceptionDTO = jsonToObject(makeRequest(connectionsBaseUrl, "POST", json, authnToken),
                TypeFactory.defaultInstance().constructType(ConstraintViolationExceptionResponseDTO.class));

        assertEquals(1, exceptionDTO.getViolations().keySet().size());

        json.put("url", sampleFeedUri);

        // Test invalid description
        int maxLength = 256;
        StringBuilder desc = new StringBuilder();

        for (int i = 0; i <= maxLength; i++) {
            desc.append("X");
        }

        json.put("description", desc.toString());
        json.put("providerId", FeedType.RSS.toString());
       
        exceptionDTO = jsonToObject(makeRequest(connectionsBaseUrl, "POST", json, authnToken),
                TypeFactory.defaultInstance().constructType(ConstraintViolationExceptionResponseDTO.class));

        assertEquals(1, exceptionDTO.getViolations().keySet().size());

        json.put("description", "Valid description.");

        try {
            ConnectionResponseDTO connectionResponseDTO = jsonToObject(makeRequest(connectionsBaseUrl, "POST", json, authnToken),
View Full Code Here

                        .add("alias", "maynard@toolband.com")
                        .add("fullname", "MJK")
                        .add("accountName", "TOOLBAND").build());

        Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),response.getStatus());
        ConstraintViolationExceptionResponseDTO dto = (ConstraintViolationExceptionResponseDTO) response.getEntity();
        Assert.assertTrue(StringUtils.isNotBlank(dto.getViolations().get("alias")));
    }
View Full Code Here

                        .add("alias", "maynard@toolband.com")
                        .add("fullname", "MJK")
                        .add("accountName", "TOOLBAND").build());

        Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),response.getStatus());
        ConstraintViolationExceptionResponseDTO dto = (ConstraintViolationExceptionResponseDTO) response.getEntity();
        Assert.assertTrue(StringUtils.isNotBlank(dto.getViolations().get("alias")));
    }
View Full Code Here

        JSONObject json = new JSONObject();

        json.put("visibility", "PUBLIC");

        ConstraintViolationExceptionResponseDTO response =
                jsonToObject(makeRequest(connectionsBaseUrl + "/" + gitHub.getId(), "PUT", json, authnToken),
                        TypeFactory.defaultInstance().constructType(ConstraintViolationExceptionResponseDTO.class));
        Map<String, String> violations = response.getViolations();
        boolean violationFound = false;

        for (String violationKey : violations.keySet()) {
            if (violationKey.equals("visibility")) {
                violationFound = true;
View Full Code Here

        updateUserJson.put("fullname", "justinc");
        updateUserJson.put("alias", "maynard"); //Justin is tired of playing bass and wants to do vocals

        Response actualResponse = userResource.changeUserProfile(updateUserJson);
        Assert.assertEquals(Response.Status.CONFLICT.getStatusCode(), actualResponse.getStatus());
        ConstraintViolationExceptionResponseDTO resp = (ConstraintViolationExceptionResponseDTO) actualResponse.getEntity();
        Assert.assertEquals("maynard is already in use", resp.getViolations().get("alias"));
    }
View Full Code Here

        updateUserJson.put("fullname", "testUser");
        updateUserJson.put("alias", "@maynard");

        Response actualResponse = userResource.changeUserProfile(updateUserJson);
        Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), actualResponse.getStatus());
        ConstraintViolationExceptionResponseDTO resp = (ConstraintViolationExceptionResponseDTO) actualResponse.getEntity();
        Assert.assertEquals("User alias contains characters that aren't alphanumeric, dashes, or underscores",
                resp.getViolations().get("alias"));
    }
View Full Code Here

        if (json.containsKey("alias")) {
            String alias = json.getString("alias");

            if (!alias.equalsIgnoreCase(user.getAlias())) {
                if (!(userService.isAliasAvailable(user.getAccount(), alias))) {
                    ConstraintViolationExceptionResponseDTO dto = new ConstraintViolationExceptionResponseDTO();
                    dto.setViolations(ImmutableMap.of("alias", json.getString("alias") + " is already in use"));
                    return Response.status(Response.Status.CONFLICT).entity(dto).build();
                }
            }
        }

        user.mergeWithJSON(json);

        try {
            userService.updateUser(user);
        } catch (InvalidUserAliasException e) {
            ConstraintViolationExceptionResponseDTO dto = new ConstraintViolationExceptionResponseDTO();
            dto.setViolations(ImmutableMap.of("alias", e.getMessage()));
            return Response.status(Response.Status.BAD_REQUEST).entity(dto).build();
        } catch (Exception e) {
            //Something unexpected, so return a 500.
            return error(e.getMessage(), Response.status(Response.Status.INTERNAL_SERVER_ERROR));
        }
View Full Code Here

TOP

Related Classes of com.streamreduce.rest.dto.response.ConstraintViolationExceptionResponseDTO

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.