@Test public void text_error() { BadRequestException exception = BadRequestException.create("error"); assertThat(exception.getMessage()).isEqualTo("error"); }
@Test public void getMessage_return_first_error() { BadRequestException underTest = BadRequestException.create(asList("error1", "error2")); assertThat(underTest.getMessage()).isEqualTo("error1"); }
private void expectFailure(String expectedMessage, Runnable runnable) { try { runnable.run(); fail(); } catch (BadRequestException e) { assertThat(e.getMessage()).isEqualTo(expectedMessage); } verifyNoActiveRules(); }
@Test public void fail_on_unknown_type() { TypeValidation fakeTypeValidation = mock(TypeValidation.class); when(fakeTypeValidation.key()).thenReturn("Fake"); try { TypeValidations typeValidations = new TypeValidations(newArrayList(fakeTypeValidation)); typeValidations.validate("10", "Unknown", null); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); BadRequestException badRequestException = (BadRequestException) e; assertThat(badRequestException.getMessage()).isEqualTo("Type 'Unknown' is not valid."); } }
public Optional<UserDto> authenticate(HttpServletRequest request, HttpServletResponse response) { try { return doAuthenticate(request, response); } catch (BadRequestException e) { throw AuthenticationException.newBuilder() .setSource(Source.sso()) .setMessage(e.getMessage()) .build(); } }
@Test public void activate_rule_diff_languages() { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule("php", "toto"); ruleIndexer.commitAndIndex(dbSession, rule.getId()); // 0. Assert No Active Rule for profile assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); try { // 1. Activate Rule wsActivateRule.newRequest().setMethod("POST") .setParam(PARAM_KEY, profile.getKee()) .setParam(PARAM_RULE, rule.getKey().toString()) .execute(); dbSession.clearCache(); fail(); } catch (BadRequestException e) { assertThat(e.getMessage()).isEqualTo("php rule blah:toto cannot be activated on java profile Pjava"); } }
@Test public void execute_fails_with_BadRequestException_if_specified_component_is_neither_a_project_a_portfolio_nor_an_application() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = randomPublicOrPrivateProject(); ComponentDto module = ComponentTesting.newModuleDto(project); ComponentDto dir = ComponentTesting.newDirectory(project, "path"); ComponentDto file = ComponentTesting.newFileDto(project); dbTester.components().insertComponents(module, dir, file); ComponentDto application = dbTester.components().insertApplication(organization); ComponentDto portfolio = dbTester.components().insertView(organization); ComponentDto subView = ComponentTesting.newSubView(portfolio); ComponentDto projectCopy = newProjectCopy("foo", project, subView); dbTester.components().insertComponents(subView, projectCopy); userSessionRule.addProjectPermission(UserRole.ADMIN, project, portfolio, application); Stream.of(project, portfolio, application).forEach(c -> request .setParam(PARAM_PROJECT, c.getDbKey()) .setParam(PARAM_VISIBILITY, randomVisibility) .execute()); Stream.of(module, dir, file, subView, projectCopy) .forEach(nonRootComponent -> { request.setParam(PARAM_PROJECT, nonRootComponent.getDbKey()) .setParam(PARAM_VISIBILITY, randomVisibility); try { request.execute(); fail("a BadRequestException should have been raised"); } catch (BadRequestException e) { assertThat(e.getMessage()).isEqualTo("Component must be a project, a portfolio or an application"); } }); }
public Optional<UserDto> authenticate(HttpServletRequest request, HttpServletResponse response) { try { return doAuthenticate(request, response); } catch (BadRequestException e) { throw AuthenticationException.newBuilder() .setSource(Source.sso()) .setMessage(e.getMessage()) .build(); } }