Search in sources :

Example 1 with AppValidator

use of org.sagebionetworks.bridge.validators.AppValidator in project BridgeServer2 by Sage-Bionetworks.

the class BridgeExceptionHandlerTest method bridgeValidationExceptionCorrectlyReported.

@Test
public void bridgeValidationExceptionCorrectlyReported() throws Throwable {
    App app = new DynamoApp();
    try {
        Validate.entityThrowingException(new AppValidator(), app);
        fail("Should have thrown exception");
    } catch (InvalidEntityException e) {
        MethodInvocation invocation = mock(MethodInvocation.class);
        when(invocation.proceed()).thenThrow(e);
        ResponseEntity<String> response = handler.handleException(mockRequest, e);
        JsonNode node = new ObjectMapper().readTree(response.getBody());
        assertEquals(node.size(), 5);
        assertEquals(node.get("errors").get("identifier").get(0).textValue(), "identifier is required");
        assertEquals(node.get("type").textValue(), "InvalidEntityException");
        assertNotNull(node.get("entity"));
        assertNotNull(node.get("errors"));
        assertEquals(response.getStatusCodeValue(), 400);
        assertEquals(node.get("statusCode").intValue(), 400);
        // Verify log.
        verify(logger).info(contains(e.getMessage()));
    }
}
Also used : DynamoApp(org.sagebionetworks.bridge.dynamodb.DynamoApp) App(org.sagebionetworks.bridge.models.apps.App) ResponseEntity(org.springframework.http.ResponseEntity) DynamoApp(org.sagebionetworks.bridge.dynamodb.DynamoApp) AppValidator(org.sagebionetworks.bridge.validators.AppValidator) MethodInvocation(org.aopalliance.intercept.MethodInvocation) JsonNode(com.fasterxml.jackson.databind.JsonNode) InvalidEntityException(org.sagebionetworks.bridge.exceptions.InvalidEntityException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test) HttpUtilTest(org.sagebionetworks.bridge.spring.util.HttpUtilTest)

Example 2 with AppValidator

use of org.sagebionetworks.bridge.validators.AppValidator in project BridgeServer2 by Sage-Bionetworks.

the class AppServiceTest method before.

@BeforeMethod
public void before() throws Exception {
    MockitoAnnotations.initMocks(this);
    // Mock config.
    when(mockBridgeConfig.get(AppService.CONFIG_KEY_SUPPORT_EMAIL_PLAIN)).thenReturn(SUPPORT_EMAIL);
    when(mockBridgeConfig.get(AppService.CONFIG_KEY_TEAM_BRIDGE_ADMIN)).thenReturn(String.valueOf(BRIDGE_ADMIN_TEAM_ID));
    when(mockBridgeConfig.get(AppService.CONFIG_KEY_TEAM_BRIDGE_STAFF)).thenReturn(String.valueOf(BRIDGE_STAFF_TEAM_ID));
    when(mockBridgeConfig.getPropertyAsList(AppService.CONFIG_APP_WHITELIST)).thenReturn(ImmutableList.of(API_APP_ID));
    when(mockBridgeConfig.get(AppService.CONFIG_KEY_SYNAPSE_TRACKING_VIEW)).thenReturn(SYNAPSE_TRACKING_VIEW_ID);
    // this has to be set again after being mocked
    service.setBridgeConfig(mockBridgeConfig);
    // Mock templates
    service.setAppEmailVerificationTemplateSubject(mockTemplateAsSpringResource("Verify your app email"));
    service.setAppEmailVerificationTemplate(mockTemplateAsSpringResource("Click here ${appEmailVerificationUrl} ${appEmailVerificationExpirationPeriod}" + " ${studyEmailVerificationUrl} ${studyEmailVerificationExpirationPeriod}"));
    service.setValidator(new AppValidator());
    AppAndUsersValidator appAndUsersValidator = new AppAndUsersValidator();
    appAndUsersValidator.setSynapseClient(mockSynapseClient);
    service.setAppAndUsersValidator(appAndUsersValidator);
    when(service.getNameScopingToken()).thenReturn(TEST_NAME_SCOPING_TOKEN);
    app = getTestApp();
    app.setIdentifier(TEST_APP_ID);
    when(mockAppDao.getApp(TEST_APP_ID)).thenReturn(app);
    GuidVersionHolder keys = new GuidVersionHolder("guid", 1L);
    when(mockTemplateService.createTemplate(any(), any())).thenReturn(keys);
    when(mockAppDao.createApp(any())).thenAnswer(invocation -> {
        // Return the same app, except set version to 1.
        App app = invocation.getArgument(0);
        app.setVersion(1L);
        return app;
    });
    when(mockAppDao.updateApp(any())).thenAnswer(invocation -> {
        // Return the same app, except we increment the version.
        App app = invocation.getArgument(0);
        Long oldVersion = app.getVersion();
        app.setVersion(oldVersion != null ? oldVersion + 1 : 1);
        return app;
    });
    // Spy AppService.createTimeLimitedToken() to create a known token instead of a random one. This makes our
    // tests easier.
    doReturn(VERIFICATION_TOKEN).when(service).createTimeLimitedToken();
    // setup project and team
    team = new Team();
    project = new Project();
    project.setId(TEST_PROJECT_ID);
    team.setId(TEST_TEAM_ID);
    teamMemberInvitation = new MembershipInvitation();
    teamMemberInvitation.setInviteeId(TEST_USER_ID.toString());
    teamMemberInvitation.setTeamId(TEST_TEAM_ID);
}
Also used : DynamoApp(org.sagebionetworks.bridge.dynamodb.DynamoApp) App(org.sagebionetworks.bridge.models.apps.App) AppAndUsersValidator(org.sagebionetworks.bridge.validators.AppAndUsersValidator) Project(org.sagebionetworks.repo.model.Project) AppValidator(org.sagebionetworks.bridge.validators.AppValidator) Team(org.sagebionetworks.repo.model.Team) MembershipInvitation(org.sagebionetworks.repo.model.MembershipInvitation) GuidVersionHolder(org.sagebionetworks.bridge.models.GuidVersionHolder) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

DynamoApp (org.sagebionetworks.bridge.dynamodb.DynamoApp)2 App (org.sagebionetworks.bridge.models.apps.App)2 AppValidator (org.sagebionetworks.bridge.validators.AppValidator)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1 InvalidEntityException (org.sagebionetworks.bridge.exceptions.InvalidEntityException)1 GuidVersionHolder (org.sagebionetworks.bridge.models.GuidVersionHolder)1 HttpUtilTest (org.sagebionetworks.bridge.spring.util.HttpUtilTest)1 AppAndUsersValidator (org.sagebionetworks.bridge.validators.AppAndUsersValidator)1 MembershipInvitation (org.sagebionetworks.repo.model.MembershipInvitation)1 Project (org.sagebionetworks.repo.model.Project)1 Team (org.sagebionetworks.repo.model.Team)1 ResponseEntity (org.springframework.http.ResponseEntity)1 BeforeMethod (org.testng.annotations.BeforeMethod)1 Test (org.testng.annotations.Test)1