Search in sources :

Example 51 with NotificationPermission

use of org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission in project ORCID-Source by ORCID.

the class NotificationsTest method createPermissionNotificationWithBlankAuthorizationUri.

@Test
public void createPermissionNotificationWithBlankAuthorizationUri() throws JSONException {
    NotificationPermission notification = unmarshallFromPath("/notification_2.1/samples/notification-permission-2.1.xml");
    notification.setPutCode(null);
    AuthorizationUrl authUrl = notification.getAuthorizationUrl();
    authUrl.setUri("");
    String accessToken = oauthHelper.getClientCredentialsAccessToken(client1ClientId, client1ClientSecret, ScopePathType.PREMIUM_NOTIFICATION);
    ClientResponse postResponse = notificationsClient_V2_1.addPermissionNotificationXml(testUser1OrcidId, notification, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
    String locationPath = postResponse.getLocation().getPath();
    assertTrue("Location header path should match pattern, but was " + locationPath, locationPath.matches(".*/v2.1/" + testUser1OrcidId + "/notification-permission/\\d+"));
    String putCodeString = locationPath.substring(locationPath.lastIndexOf('/') + 1);
    Long putCode = Long.valueOf(putCodeString);
    ClientResponse viewResponse = notificationsClient_V2_1.viewPermissionNotificationXml(testUser1OrcidId, putCode, accessToken);
    assertEquals(Response.Status.OK.getStatusCode(), viewResponse.getStatus());
    NotificationPermission retrievedNotification = viewResponse.getEntity(NotificationPermission.class);
    assertNotNull(retrievedNotification);
    assertTrue(retrievedNotification.getAuthorizationUrl().getPath().endsWith(authUrl.getPath()));
    assertFalse(retrievedNotification.getAuthorizationUrl().getPath().startsWith("http"));
    assertTrue(retrievedNotification.getAuthorizationUrl().getUri().startsWith("http"));
}
Also used : AuthorizationUrl(org.orcid.jaxb.model.notification.permission_v2.AuthorizationUrl) ClientResponse(com.sun.jersey.api.client.ClientResponse) NotificationPermission(org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) Test(org.junit.Test)

Example 52 with NotificationPermission

use of org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission in project ORCID-Source by ORCID.

the class MapperFacadeFactory method getObject.

@Override
public MapperFacade getObject() throws Exception {
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
    // Register converters
    ConverterFactory converterFactory = mapperFactory.getConverterFactory();
    converterFactory.registerConverter("singleWorkExternalIdentifierFromJsonConverter", new SingleWorkExternalIdentifierFromJsonConverter());
    converterFactory.registerConverter("externalIdentifierIdConverter", new ExternalIdentifierTypeConverter());
    // Register factories
    mapperFactory.registerObjectFactory(new WorkEntityFactory(workDao), TypeFactory.<NotificationWorkEntity>valueOf(NotificationWorkEntity.class), TypeFactory.<Item>valueOf(Item.class));
    // Custom notification
    ClassMapBuilder<NotificationCustom, NotificationCustomEntity> notificationCustomClassMap = mapperFactory.classMap(NotificationCustom.class, NotificationCustomEntity.class);
    registerSourceConverters(mapperFactory, notificationCustomClassMap);
    mapCommonFields(notificationCustomClassMap).register();
    // Permission notification
    ClassMapBuilder<NotificationPermission, NotificationAddItemsEntity> notificationPermissionClassMap = mapperFactory.classMap(NotificationPermission.class, NotificationAddItemsEntity.class);
    registerSourceConverters(mapperFactory, notificationPermissionClassMap);
    mapCommonFields(notificationPermissionClassMap.field("authorizationUrl.uri", "authorizationUrl").field("items.items", "notificationItems").customize(new CustomMapper<NotificationPermission, NotificationAddItemsEntity>() {

        @Override
        public void mapAtoB(NotificationPermission notification, NotificationAddItemsEntity entity, MappingContext context) {
            if (StringUtils.isBlank(entity.getAuthorizationUrl())) {
                String authUrl = orcidUrlManager.getBaseUrl() + notification.getAuthorizationUrl().getPath();
                // validate
                validateAndConvertToURI(authUrl);
                entity.setAuthorizationUrl(authUrl);
            }
        }

        @Override
        public void mapBtoA(NotificationAddItemsEntity entity, NotificationPermission notification, MappingContext context) {
            AuthorizationUrl authUrl = notification.getAuthorizationUrl();
            if (authUrl != null) {
                authUrl.setPath(extractFullPath(authUrl.getUri()));
                authUrl.setHost(orcidUrlManager.getBaseHost());
            }
        }
    })).register();
    // Institutional sign in notification
    ClassMapBuilder<NotificationInstitutionalConnection, NotificationInstitutionalConnectionEntity> institutionalConnectionNotificationClassMap = mapperFactory.classMap(NotificationInstitutionalConnection.class, NotificationInstitutionalConnectionEntity.class);
    registerSourceConverters(mapperFactory, institutionalConnectionNotificationClassMap);
    mapCommonFields(institutionalConnectionNotificationClassMap.field("authorizationUrl.uri", "authorizationUrl").customize(new CustomMapper<NotificationInstitutionalConnection, NotificationInstitutionalConnectionEntity>() {

        @Override
        public void mapAtoB(NotificationInstitutionalConnection notification, NotificationInstitutionalConnectionEntity entity, MappingContext context) {
            if (StringUtils.isBlank(entity.getAuthorizationUrl())) {
                String authUrl = orcidUrlManager.getBaseUrl() + notification.getAuthorizationUrl().getPath();
                // validate
                validateAndConvertToURI(authUrl);
                entity.setAuthorizationUrl(authUrl);
            }
        }

        @Override
        public void mapBtoA(NotificationInstitutionalConnectionEntity entity, NotificationInstitutionalConnection notification, MappingContext context) {
            AuthorizationUrl authUrl = notification.getAuthorizationUrl();
            if (authUrl != null) {
                authUrl.setPath(extractFullPath(authUrl.getUri()));
                authUrl.setHost(orcidUrlManager.getBaseHost());
            }
            String providerId = entity.getAuthenticationProviderId();
            if (StringUtils.isNotBlank(providerId)) {
                String idpName = identityProviderManager.retrieveIdentitifyProviderName(providerId);
                notification.setIdpName(idpName);
            } else {
                notification.setIdpName(LAST_RESORT_IDENTITY_PROVIDER_NAME);
            }
        }
    })).register();
    // Amend notification
    ClassMapBuilder<NotificationAmended, NotificationAmendedEntity> amendNotificationClassMap = mapperFactory.classMap(NotificationAmended.class, NotificationAmendedEntity.class);
    registerSourceConverters(mapperFactory, amendNotificationClassMap);
    mapCommonFields(amendNotificationClassMap).register();
    mapperFactory.classMap(NotificationItemEntity.class, Item.class).fieldMap("externalIdType", "externalIdentifier.type").converter("externalIdentifierIdConverter").add().field("externalIdValue", "externalIdentifier.value").byDefault().register();
    return mapperFactory.getMapperFacade();
}
Also used : NotificationInstitutionalConnectionEntity(org.orcid.persistence.jpa.entities.NotificationInstitutionalConnectionEntity) ConverterFactory(ma.glasnost.orika.converter.ConverterFactory) NotificationCustom(org.orcid.jaxb.model.notification.custom_v2.NotificationCustom) NotificationAddItemsEntity(org.orcid.persistence.jpa.entities.NotificationAddItemsEntity) MappingContext(ma.glasnost.orika.MappingContext) AuthorizationUrl(org.orcid.jaxb.model.notification.permission_v2.AuthorizationUrl) SingleWorkExternalIdentifierFromJsonConverter(org.orcid.core.adapter.impl.jsonidentifiers.SingleWorkExternalIdentifierFromJsonConverter) Item(org.orcid.jaxb.model.notification.permission_v2.Item) DefaultMapperFactory(ma.glasnost.orika.impl.DefaultMapperFactory) NotificationPermission(org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) NotificationAmendedEntity(org.orcid.persistence.jpa.entities.NotificationAmendedEntity) NotificationInstitutionalConnection(org.orcid.model.notification.institutional_sign_in_v2.NotificationInstitutionalConnection) NotificationCustomEntity(org.orcid.persistence.jpa.entities.NotificationCustomEntity) NotificationWorkEntity(org.orcid.persistence.jpa.entities.NotificationWorkEntity) ExternalIdentifierTypeConverter(org.orcid.core.adapter.impl.jsonidentifiers.ExternalIdentifierTypeConverter) DefaultMapperFactory(ma.glasnost.orika.impl.DefaultMapperFactory) MapperFactory(ma.glasnost.orika.MapperFactory) NotificationAmended(org.orcid.jaxb.model.notification.amended_v2.NotificationAmended)

Example 53 with NotificationPermission

use of org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission in project ORCID-Source by ORCID.

the class NotificationManagerTest method testFindPermissionsByOrcidAndClient.

/**
     * Test independent of spring context, sets up NotificationManager with
     * mocked notifiation dao and notification adapter
     */
@Test
public void testFindPermissionsByOrcidAndClient() {
    List<Notification> notificationPermissions = IntStream.range(0, 10).mapToObj(i -> new NotificationPermission()).collect(Collectors.toList());
    NotificationDao notificationDao = mock(NotificationDaoImpl.class);
    JpaJaxbNotificationAdapter adapter = mock(JpaJaxbNotificationAdapterImpl.class);
    when(notificationDao.findPermissionsByOrcidAndClient(anyString(), anyString(), anyInt(), anyInt())).thenReturn(new ArrayList<NotificationEntity>());
    when(adapter.toNotification(Matchers.<ArrayList<NotificationEntity>>any())).thenReturn(notificationPermissions);
    NotificationManager notificationManager = new NotificationManagerImpl();
    ReflectionTestUtils.setField(notificationManager, "notificationAdapter", adapter);
    ReflectionTestUtils.setField(notificationManager, "notificationDao", notificationDao);
    NotificationPermissions notifications = notificationManager.findPermissionsByOrcidAndClient("some-orcid", "some-client", 0, OrcidApiConstants.MAX_NOTIFICATIONS_AVAILABLE);
    assertEquals(notificationPermissions.size(), notifications.getNotifications().size());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) TargetProxyHelper(org.orcid.test.TargetProxyHelper) OrcidJUnit4ClassRunner(org.orcid.test.OrcidJUnit4ClassRunner) URISyntaxException(java.net.URISyntaxException) ProfileEventEntity(org.orcid.persistence.jpa.entities.ProfileEventEntity) NotificationPermissions(org.orcid.jaxb.model.notification.permission_v2.NotificationPermissions) NotificationPermission(org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) RecordNameEntity(org.orcid.persistence.jpa.entities.RecordNameEntity) DBUnitTest(org.orcid.test.DBUnitTest) Assert.assertThat(org.junit.Assert.assertThat) DelegateSummary(org.orcid.jaxb.model.message.DelegateSummary) MockitoAnnotations(org.mockito.MockitoAnnotations) AmendedSection(org.orcid.jaxb.model.notification.amended_v2.AmendedSection) ProfileDao(org.orcid.persistence.dao.ProfileDao) Source(org.orcid.jaxb.model.common_v2.Source) Locale(org.orcid.jaxb.model.message.Locale) NotificationDao(org.orcid.persistence.dao.NotificationDao) After(org.junit.After) NotificationManagerImpl(org.orcid.core.manager.impl.NotificationManagerImpl) NotificationType(org.orcid.jaxb.model.notification_v2.NotificationType) AfterClass(org.junit.AfterClass) OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) CreditName(org.orcid.jaxb.model.message.CreditName) Resource(javax.annotation.Resource) OrcidOauth2TokenDetailService(org.orcid.core.oauth.OrcidOauth2TokenDetailService) ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) Set(java.util.Set) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) DelegationDetails(org.orcid.jaxb.model.message.DelegationDetails) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) NotificationEntity(org.orcid.persistence.jpa.entities.NotificationEntity) List(java.util.List) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) NotificationDaoImpl(org.orcid.persistence.dao.impl.NotificationDaoImpl) GenericDao(org.orcid.persistence.dao.GenericDao) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Notification(org.orcid.jaxb.model.notification_v2.Notification) NotificationCustomEntity(org.orcid.persistence.jpa.entities.NotificationCustomEntity) CoreMatchers.anyOf(org.hamcrest.CoreMatchers.anyOf) BeforeClass(org.junit.BeforeClass) Matchers(org.mockito.Matchers) Mock(org.mockito.Mock) CoreMatchers.not(org.hamcrest.CoreMatchers.not) RunWith(org.junit.runner.RunWith) MailGunManager(org.orcid.core.manager.impl.MailGunManager) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OrcidIdentifier(org.orcid.jaxb.model.message.OrcidIdentifier) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) JAXBContext(javax.xml.bind.JAXBContext) IntFunction(java.util.function.IntFunction) Before(org.junit.Before) Unmarshaller(javax.xml.bind.Unmarshaller) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) ClientDetailsDao(org.orcid.persistence.dao.ClientDetailsDao) Mockito.when(org.mockito.Mockito.when) JpaJaxbNotificationAdapterImpl(org.orcid.core.adapter.impl.JpaJaxbNotificationAdapterImpl) NotificationInstitutionalConnection(org.orcid.model.notification.institutional_sign_in_v2.NotificationInstitutionalConnection) OrcidApiConstants(org.orcid.core.api.OrcidApiConstants) Mockito.verify(org.mockito.Mockito.verify) NotificationCustom(org.orcid.jaxb.model.notification.custom_v2.NotificationCustom) Mockito(org.mockito.Mockito) Visibility(org.orcid.jaxb.model.common_v2.Visibility) Mockito.never(org.mockito.Mockito.never) JpaJaxbNotificationAdapter(org.orcid.core.adapter.JpaJaxbNotificationAdapter) ContextConfiguration(org.springframework.test.context.ContextConfiguration) SecurityQuestionEntity(org.orcid.persistence.jpa.entities.SecurityQuestionEntity) Email(org.orcid.jaxb.model.record_v2.Email) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Transactional(org.springframework.transaction.annotation.Transactional) NotificationDao(org.orcid.persistence.dao.NotificationDao) JpaJaxbNotificationAdapter(org.orcid.core.adapter.JpaJaxbNotificationAdapter) NotificationPermissions(org.orcid.jaxb.model.notification.permission_v2.NotificationPermissions) NotificationPermission(org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) NotificationEntity(org.orcid.persistence.jpa.entities.NotificationEntity) Notification(org.orcid.jaxb.model.notification_v2.Notification) NotificationManagerImpl(org.orcid.core.manager.impl.NotificationManagerImpl) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 54 with NotificationPermission

use of org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission in project ORCID-Source by ORCID.

the class NotificationsTest method createPermissionNotificationWithAbsentAuthorizationUriElement.

@Test
public void createPermissionNotificationWithAbsentAuthorizationUriElement() throws JSONException {
    NotificationPermission notification = unmarshallFromPath("/notification_2.0/samples/notification-permission-2.0.xml");
    notification.setPutCode(null);
    AuthorizationUrl authUrl = notification.getAuthorizationUrl();
    authUrl.setUri("");
    String accessToken = oauthHelper.getClientCredentialsAccessToken(client1ClientId, client1ClientSecret, ScopePathType.PREMIUM_NOTIFICATION);
    ClientResponse postResponse = notificationsClient.addPermissionNotificationXml(testUser1OrcidId, notification, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
    String locationPath = postResponse.getLocation().getPath();
    assertTrue("Location header path should match pattern, but was " + locationPath, locationPath.matches(".*/v2.0/" + testUser1OrcidId + "/notification-permission/\\d+"));
    String putCodeString = locationPath.substring(locationPath.lastIndexOf('/') + 1);
    Long putCode = Long.valueOf(putCodeString);
    ClientResponse viewResponse = notificationsClient.viewPermissionNotificationXml(testUser1OrcidId, putCode, accessToken);
    assertEquals(Response.Status.OK.getStatusCode(), viewResponse.getStatus());
    NotificationPermission retrievedNotification = viewResponse.getEntity(NotificationPermission.class);
    assertNotNull(retrievedNotification);
    assertTrue(retrievedNotification.getAuthorizationUrl().getPath().endsWith(authUrl.getPath()));
    assertFalse(retrievedNotification.getAuthorizationUrl().getPath().startsWith("http"));
    assertTrue(retrievedNotification.getAuthorizationUrl().getUri().startsWith("http"));
}
Also used : AuthorizationUrl(org.orcid.jaxb.model.notification.permission_v2.AuthorizationUrl) ClientResponse(com.sun.jersey.api.client.ClientResponse) NotificationPermission(org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) Test(org.junit.Test)

Example 55 with NotificationPermission

use of org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission in project ORCID-Source by ORCID.

the class NotificationsTest method createPermissionNotificationWithBlankAuthorizationUri.

@Test
public void createPermissionNotificationWithBlankAuthorizationUri() throws JSONException {
    NotificationPermission notification = unmarshallFromPath("/notification_2.0/samples/notification-permission-2.0.xml");
    notification.setPutCode(null);
    AuthorizationUrl authUrl = notification.getAuthorizationUrl();
    authUrl.setUri("");
    String accessToken = oauthHelper.getClientCredentialsAccessToken(client1ClientId, client1ClientSecret, ScopePathType.PREMIUM_NOTIFICATION);
    ClientResponse postResponse = notificationsClient.addPermissionNotificationXml(testUser1OrcidId, notification, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
    String locationPath = postResponse.getLocation().getPath();
    assertTrue("Location header path should match pattern, but was " + locationPath, locationPath.matches(".*/v2.0/" + testUser1OrcidId + "/notification-permission/\\d+"));
    String putCodeString = locationPath.substring(locationPath.lastIndexOf('/') + 1);
    Long putCode = Long.valueOf(putCodeString);
    ClientResponse viewResponse = notificationsClient.viewPermissionNotificationXml(testUser1OrcidId, putCode, accessToken);
    assertEquals(Response.Status.OK.getStatusCode(), viewResponse.getStatus());
    NotificationPermission retrievedNotification = viewResponse.getEntity(NotificationPermission.class);
    assertNotNull(retrievedNotification);
    assertTrue(retrievedNotification.getAuthorizationUrl().getPath().endsWith(authUrl.getPath()));
    assertFalse(retrievedNotification.getAuthorizationUrl().getPath().startsWith("http"));
    assertTrue(retrievedNotification.getAuthorizationUrl().getUri().startsWith("http"));
}
Also used : AuthorizationUrl(org.orcid.jaxb.model.notification.permission_v2.AuthorizationUrl) ClientResponse(com.sun.jersey.api.client.ClientResponse) NotificationPermission(org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)45 ClientResponse (com.sun.jersey.api.client.ClientResponse)41 NotificationPermission (org.orcid.jaxb.model.notification.permission_v2.NotificationPermission)24 AuthorizationUrl (org.orcid.jaxb.model.notification.permission_v2.AuthorizationUrl)11 NotificationPermission (org.orcid.jaxb.model.notification.permission_rc2.NotificationPermission)8 NotificationPermission (org.orcid.jaxb.model.notification.permission_rc3.NotificationPermission)8 NotificationPermission (org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission)8 JAXBContext (javax.xml.bind.JAXBContext)7 JAXBException (javax.xml.bind.JAXBException)7 Unmarshaller (javax.xml.bind.Unmarshaller)7 NotificationPermission (org.orcid.jaxb.model.notification.permission_rc1.NotificationPermission)7 Notification (org.orcid.jaxb.model.notification_v2.Notification)6 NotificationInstitutionalConnection (org.orcid.model.notification.institutional_sign_in_v2.NotificationInstitutionalConnection)5 NotificationAmended (org.orcid.jaxb.model.notification.amended_v2.NotificationAmended)4 AuthorizationUrl (org.orcid.jaxb.model.notification.permission_rc2.AuthorizationUrl)4 AuthorizationUrl (org.orcid.jaxb.model.notification.permission_rc3.AuthorizationUrl)4 AuthorizationUrl (org.orcid.jaxb.model.notification.permission_rc4.AuthorizationUrl)4 ArrayList (java.util.ArrayList)3 Source (org.orcid.jaxb.model.common_v2.Source)3 SourceClientId (org.orcid.jaxb.model.common_v2.SourceClientId)3