Search in sources :

Example 1 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class LoginHandler method authenticateUser.

public ResponseEntity<String> authenticateUser(final HttpServletRequest request, final HttpServletResponse response, final LoginRestModel loginRestModel) {
    final IntLogger logger = new PrintStreamIntLogger(System.out, LogLevel.INFO);
    try {
        if (loginActions.authenticateUser(loginRestModel, logger)) {
            final CsrfToken token = csrfTokenRepository.generateToken(request);
            csrfTokenRepository.saveToken(token, request, response);
            response.setHeader(token.getHeaderName(), token.getToken());
            return createResponse(HttpStatus.OK, "{\"message\":\"Success\"}");
        }
        return createResponse(HttpStatus.UNAUTHORIZED, "User not administrator");
    } catch (final IntegrationRestException e) {
        logger.error(e.getMessage(), e);
        return createResponse(HttpStatus.valueOf(e.getHttpStatusCode()), e.getHttpStatusMessage() + " : " + e.getMessage());
    } catch (final AlertFieldException e) {
        logger.error(e.getMessage(), e);
        final ResponseBodyBuilder responseBodyBuilder = new ResponseBodyBuilder(0L, e.getMessage());
        responseBodyBuilder.putErrors(e.getFieldErrors());
        final String responseBody = responseBodyBuilder.build();
        return createResponse(HttpStatus.BAD_REQUEST, responseBody);
    } catch (final Exception e) {
        logger.error(e.getMessage(), e);
        return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) PrintStreamIntLogger(com.blackducksoftware.integration.log.PrintStreamIntLogger) IntLogger(com.blackducksoftware.integration.log.IntLogger) PrintStreamIntLogger(com.blackducksoftware.integration.log.PrintStreamIntLogger) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) CsrfToken(org.springframework.security.web.csrf.CsrfToken) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 2 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class GlobalEmailConfigActionsTest method testInvalidConfig.

@Override
public void testInvalidConfig() {
    final MockEmailGlobalRestModel mockUtil = new MockEmailGlobalRestModel();
    mockUtil.setMailSmtpPort("qq");
    mockUtil.setMailSmtpConnectionTimeout("qq");
    mockUtil.setMailSmtpTimeout("qq");
    mockUtil.setMailSmtpEhlo(false);
    mockUtil.setMailSmtpAuth(true);
    mockUtil.setMailSmtpAllow8bitmime(false);
    mockUtil.setMailSmtpSendPartial(false);
    final GlobalEmailConfigRestModel restModel = mockUtil.createGlobalRestModel();
    String result = null;
    try {
        result = configActions.validateConfig(restModel);
        fail();
    } catch (final AlertFieldException e) {
        assertTrue(true);
    }
    assertNull(result);
}
Also used : MockEmailGlobalRestModel(com.blackducksoftware.integration.hub.alert.channel.email.mock.MockEmailGlobalRestModel) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)

Example 3 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class CommonConfigHandlerTest method testConfigWithAlertFieldExceptionTest.

@Test
public void testConfigWithAlertFieldExceptionTest() throws Exception {
    final CommonDistributionConfigActions configActions = Mockito.mock(CommonDistributionConfigActions.class);
    final CommonConfigHandler<CommonDistributionConfigEntity, CommonDistributionConfigRestModel, CommonDistributionRepositoryWrapper> handler = new CommonConfigHandler<>(CommonDistributionConfigEntity.class, CommonDistributionConfigRestModel.class, configActions, objectTransformer);
    Mockito.when(configActions.testConfig(Mockito.any())).thenThrow(new AlertFieldException(Collections.emptyMap()));
    Mockito.when(configActions.getObjectTransformer()).thenReturn(objectTransformer);
    final CommonDistributionConfigRestModel restModel = mockCommonDistributionRestModel.createRestModel();
    final ResponseEntity<String> response = handler.testConfig(restModel);
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Also used : CommonDistributionConfigActions(com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions) CommonDistributionRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper) CommonDistributionConfigRestModel(com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) Test(org.junit.Test)

Example 4 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class LoginHandlerTest method authenticateUserWithAlertFieldExceptionTest.

@Test
public void authenticateUserWithAlertFieldExceptionTest() throws IntegrationException {
    final LoginActions loginActions = Mockito.mock(LoginActions.class);
    final LoginHandler loginHandler = new LoginHandler(objectTransformer, loginActions, csrfTokenRepository);
    Mockito.when(loginActions.authenticateUser(Mockito.any(), Mockito.any())).thenThrow(new AlertFieldException(Collections.emptyMap()));
    final ResponseEntity<String> response = loginHandler.authenticateUser(null, null, null);
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Also used : LoginActions(com.blackducksoftware.integration.hub.alert.web.actions.LoginActions) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) Test(org.junit.Test)

Example 5 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class GlobalHubConfigActionsTest method testInvalidConfig.

@Override
public void testInvalidConfig() {
    final MockGlobalHubRestModel mockUtil = new MockGlobalHubRestModel();
    mockUtil.setHubTimeout("qqq");
    final GlobalHubConfigRestModel restModel = mockUtil.createGlobalRestModel();
    String result = null;
    try {
        result = configActions.validateConfig(restModel);
        fail();
    } catch (final AlertFieldException e) {
        assertTrue(true);
    }
    assertNull(result);
    mockUtil.setHubApiKey(StringUtils.repeat('a', 300));
    final GlobalHubConfigRestModel restModelBigApi = mockUtil.createGlobalRestModel();
    String resultBigApi = null;
    try {
        resultBigApi = configActions.validateConfig(restModelBigApi);
        fail();
    } catch (final AlertFieldException e) {
        assertTrue(true);
    }
    assertNull(resultBigApi);
}
Also used : GlobalHubConfigRestModel(com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigRestModel) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) MockGlobalHubRestModel(com.blackducksoftware.integration.hub.alert.hub.mock.MockGlobalHubRestModel)

Aggregations

AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)17 Test (org.junit.Test)8 CommonDistributionConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity)5 CommonDistributionRepositoryWrapper (com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper)4 CommonDistributionConfigActions (com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions)4 CommonDistributionConfigRestModel (com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel)4 ValidationResults (com.blackducksoftware.integration.validator.ValidationResults)4 HashMap (java.util.HashMap)4 GlobalActionsTest (com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)3 ResponseBodyBuilder (com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)3 AbstractValidator (com.blackducksoftware.integration.validator.AbstractValidator)3 FieldEnum (com.blackducksoftware.integration.validator.FieldEnum)3 ValidationResult (com.blackducksoftware.integration.validator.ValidationResult)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)2 ObjectTransformer (com.blackducksoftware.integration.hub.alert.web.ObjectTransformer)2 HubServerConfigBuilder (com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder)2 IntLogger (com.blackducksoftware.integration.log.IntLogger)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1