Search in sources :

Example 1 with Association

use of org.wso2.carbon.registry.core.Association in project carbon-apimgt by wso2.

the class RegistryPersistenceUtil method notifyAPIStateChangeToAssociatedDocuments.

/**
 * Notify document artifacts if an api state change occured. This change is required to re-trigger the document
 * indexer so that the documnet indexes will be updated with the new associated api status.
 *
 * @param apiArtifact
 * @param registry
 * @throws RegistryException
 * @throws APIManagementException
 */
public static void notifyAPIStateChangeToAssociatedDocuments(GenericArtifact apiArtifact, Registry registry) throws RegistryException, APIManagementException {
    Association[] docAssociations = registry.getAssociations(apiArtifact.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);
    for (Association association : docAssociations) {
        String documentResourcePath = association.getDestinationPath();
        Resource docResource = registry.get(documentResourcePath);
        String oldStateChangeIndicatorStatus = docResource.getProperty(APIConstants.API_STATE_CHANGE_INDICATOR);
        String newStateChangeIndicatorStatus = "false";
        if (oldStateChangeIndicatorStatus != null) {
            newStateChangeIndicatorStatus = String.valueOf(!Boolean.parseBoolean(oldStateChangeIndicatorStatus));
        }
        docResource.setProperty(APIConstants.API_STATE_CHANGE_INDICATOR, "false");
        registry.put(documentResourcePath, docResource);
    }
}
Also used : Association(org.wso2.carbon.registry.core.Association) Resource(org.wso2.carbon.registry.core.Resource)

Example 2 with Association

use of org.wso2.carbon.registry.core.Association in project carbon-apimgt by wso2.

the class APIProviderImplTest method prepareForGetAPIsByProvider.

/**
 * This method can be used when invoking getAPIsByProvider()
 */
private void prepareForGetAPIsByProvider(GenericArtifactManager artifactManager, APIProviderImplWrapper apiProvider, String providerId, API api1, API api2) throws APIManagementException, RegistryException {
    String providerPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + providerId;
    // Mocking API1 association
    Association association1 = Mockito.mock(Association.class);
    String apiPath1 = "/API1/1.0.0";
    Resource resource1 = Mockito.mock(Resource.class);
    String apiArtifactId1 = "76897689";
    Mockito.when(apiProvider.registry.resourceExists(apiPath1)).thenReturn(true);
    Mockito.when(association1.getDestinationPath()).thenReturn(apiPath1);
    Mockito.when(apiProvider.registry.get(apiPath1)).thenReturn(resource1);
    Mockito.when(resource1.getUUID()).thenReturn(apiArtifactId1);
    GenericArtifact apiArtifact1 = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact(apiArtifactId1)).thenReturn(apiArtifact1);
    Mockito.when(APIUtil.getAPI(apiArtifact1, apiProvider.registry)).thenReturn(api1);
    // Mocking API2 association
    Association association2 = Mockito.mock(Association.class);
    String apiPath2 = "/API2/1.0.0";
    Resource resource2 = Mockito.mock(Resource.class);
    String apiArtifactId2 = "76897622";
    Mockito.when(apiProvider.registry.resourceExists(apiPath2)).thenReturn(true);
    Mockito.when(association2.getDestinationPath()).thenReturn(apiPath2);
    Mockito.when(apiProvider.registry.get(apiPath2)).thenReturn(resource2);
    Mockito.when(resource2.getUUID()).thenReturn(apiArtifactId2);
    GenericArtifact apiArtifact2 = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact(apiArtifactId2)).thenReturn(apiArtifact2);
    Mockito.when(APIUtil.getAPI(apiArtifact2, apiProvider.registry)).thenReturn(api2);
    Association[] associactions = { association1, association2 };
    Mockito.when(apiProvider.registry.getAssociations(providerPath, APIConstants.PROVIDER_ASSOCIATION)).thenReturn(associactions);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Association(org.wso2.carbon.registry.core.Association) Resource(org.wso2.carbon.registry.core.Resource)

Example 3 with Association

use of org.wso2.carbon.registry.core.Association in project carbon-apimgt by wso2.

the class APIUtil method notifyAPIStateChangeToAssociatedDocuments.

/**
 * Notify document artifacts if an api state change occured. This change is required to re-trigger the document
 * indexer so that the documnet indexes will be updated with the new associated api status.
 *
 * @param apiArtifact
 * @param registry
 * @throws RegistryException
 * @throws APIManagementException
 */
public static void notifyAPIStateChangeToAssociatedDocuments(GenericArtifact apiArtifact, Registry registry) throws RegistryException, APIManagementException {
    Association[] docAssociations = registry.getAssociations(apiArtifact.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);
    for (Association association : docAssociations) {
        String documentResourcePath = association.getDestinationPath();
        Resource docResource = registry.get(documentResourcePath);
        String oldStateChangeIndicatorStatus = docResource.getProperty(APIConstants.API_STATE_CHANGE_INDICATOR);
        String newStateChangeIndicatorStatus = "false";
        if (oldStateChangeIndicatorStatus != null) {
            newStateChangeIndicatorStatus = String.valueOf(!Boolean.parseBoolean(oldStateChangeIndicatorStatus));
        }
        docResource.setProperty(APIConstants.API_STATE_CHANGE_INDICATOR, "false");
        registry.put(documentResourcePath, docResource);
    }
}
Also used : Association(org.wso2.carbon.registry.core.Association) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource)

Example 4 with Association

use of org.wso2.carbon.registry.core.Association in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetPublishedAPIsByProvider.

@Test
public void testGetPublishedAPIsByProvider() throws APIManagementException, RegistryException {
    Registry userRegistry = Mockito.mock(Registry.class);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(true, false);
    PowerMockito.when(APIUtil.isAllowDisplayAPIsWithMultipleStatus()).thenReturn(true, false);
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    Association association = Mockito.mock(Association.class);
    Association[] associations = new Association[] { association };
    Mockito.when(userRegistry.getAssociations(Mockito.anyString(), Mockito.anyString())).thenReturn(associations);
    Mockito.when(association.getDestinationPath()).thenReturn("testPath");
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(userRegistry.get("testPath")).thenReturn(resource);
    Mockito.when(resource.getUUID()).thenReturn("testID");
    GenericArtifact genericArtifact = Mockito.mock(GenericArtifact.class);
    Mockito.when(artifactManager.getGenericArtifact(Mockito.anyString())).thenReturn(genericArtifact);
    Mockito.when(genericArtifact.getAttribute("overview_status")).thenReturn("PUBLISHED");
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId1);
    Mockito.when(APIUtil.getAPI(genericArtifact)).thenReturn(api);
    assertNotNull(apiConsumer.getPublishedAPIsByProvider("testID", 10));
    assertNotNull(apiConsumer.getPublishedAPIsByProvider("testID", 10));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Association(org.wso2.carbon.registry.core.Association) Resource(org.wso2.carbon.registry.core.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with Association

use of org.wso2.carbon.registry.core.Association in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetPublishedAPIsByProvider2.

@Test
public void testGetPublishedAPIsByProvider2() throws APIManagementException, RegistryException, org.wso2.carbon.user.core.UserStoreException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    String providerId = "2";
    API api = new API(new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION));
    API api1 = new API(new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, "2.0.0"));
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(true, false);
    PowerMockito.when(APIUtil.isAllowDisplayAPIsWithMultipleStatus()).thenReturn(true, false);
    PowerMockito.when(APIUtil.getArtifactManager(userRegistry, APIConstants.API_KEY)).thenReturn(genericArtifactManager);
    PowerMockito.when(APIUtil.getMountedPath(Mockito.any(), Mockito.anyString())).thenReturn("system/governance");
    PowerMockito.when(APIUtil.getAPI(Mockito.any())).thenReturn(api);
    PowerMockito.when(APIUtil.replaceEmailDomainBack(Mockito.anyString())).thenReturn(providerId);
    PowerMockito.when(APIUtil.getLcStateFromArtifact((GovernanceArtifact) Mockito.any())).thenReturn(APIConstants.PUBLISHED);
    GenericArtifact genericArtifact1 = new GenericArtifactImpl(new QName("local"), "artifact1");
    GenericArtifact genericArtifact2 = new GenericArtifactImpl(new QName("local"), "artifact2");
    GenericArtifact[] genericArtifacts = new GenericArtifact[] { genericArtifact1, genericArtifact2 };
    Mockito.when(genericArtifactManager.findGenericArtifacts((Map<String, List<String>>) Mockito.any())).thenReturn(genericArtifacts);
    PowerMockito.mockStatic(GovernanceUtils.class);
    PowerMockito.when(GovernanceUtils.getArtifactPath(Mockito.any(), Mockito.anyString())).thenReturn("/path1");
    PowerMockito.when(RegistryUtils.getAbsolutePath(Mockito.any(), Mockito.anyString())).thenReturn("/path1");
    Association association = new Association();
    association.setDestinationPath("/destPath1");
    Association association2 = new Association();
    association2.setDestinationPath("/destPath2");
    Association[] associations = new Association[] { association, association2 };
    Mockito.when(userRegistry.getAssociations(Mockito.anyString(), Mockito.anyString())).thenThrow(RegistryException.class).thenReturn(associations);
    try {
        apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get Published APIs for provider :"));
    }
    Assert.assertEquals(0, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 2, API_PROVIDER, "").size());
    Mockito.when(authorizationManager.isUserAuthorized(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenThrow(UserStoreException.class).thenThrow(org.wso2.carbon.user.core.UserStoreException.class).thenReturn(true);
    try {
        apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get Published APIs for provider :"));
    }
    try {
        apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get Published APIs for provider :"));
    }
    Resource resource = new ResourceImpl();
    resource.setUUID(UUID.randomUUID().toString());
    Mockito.when(userRegistry.get(Mockito.anyString())).thenReturn(resource);
    GenericArtifact genericArtifact = Mockito.mock(GenericArtifactImpl.class);
    Mockito.when(genericArtifact.getLifecycleState(Mockito.anyString())).thenReturn(APIConstants.PUBLISHED);
    Mockito.when(genericArtifactManager.getGenericArtifact(Mockito.anyString())).thenReturn(genericArtifact);
    Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 1, API_PROVIDER, "").size());
    PowerMockito.when(APIUtil.getAPI((GenericArtifact) Mockito.any())).thenReturn(api, api1);
    Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "").size());
    PowerMockito.when(APIUtil.isAllowDisplayAPIsWithMultipleStatus()).thenReturn(true);
    Set<API> apiSet = apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
    Assert.assertEquals(1, apiSet.size());
    Assert.assertTrue(apiSet.contains(api1));
    String apiOwner = "Smith";
    PowerMockito.when(APIUtil.replaceEmailDomainBack(apiOwner)).thenReturn(apiOwner);
    api.setApiOwner("John");
    PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(true);
    Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider(apiOwner, "test_user", 1, apiOwner, "").size());
    Assert.assertEquals(0, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 1, apiOwner, "").size());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) Matchers.anyString(org.mockito.Matchers.anyString) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) Association(org.wso2.carbon.registry.core.Association) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Association (org.wso2.carbon.registry.core.Association)10 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)8 Resource (org.wso2.carbon.registry.core.Resource)7 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)5 API (org.wso2.carbon.apimgt.api.model.API)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)4 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)4 UserStoreException (org.wso2.carbon.user.api.UserStoreException)4 ArrayList (java.util.ArrayList)3 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)3 Registry (org.wso2.carbon.registry.core.Registry)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 List (java.util.List)2 QName (javax.xml.namespace.QName)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)2 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)2