Search in sources :

Example 6 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testUpdateAPI.

@Test
public void testUpdateAPI() throws APIPersistenceException, RegistryException, APIManagementException {
    PublisherAPI publisherAPI = new PublisherAPI();
    publisherAPI.setDescription("Modified description");
    API api = APIMapper.INSTANCE.toApi(publisherAPI);
    Registry registry = Mockito.mock(UserRegistry.class);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.get(anyString())).thenReturn(resource);
    Tag[] tags = new Tag[0];
    Mockito.when(registry.getTags(anyString())).thenReturn(tags);
    GenericArtifact existArtifact = PersistenceHelper.getSampleAPIArtifact();
    String apiUUID = existArtifact.getId();
    PowerMockito.mockStatic(RegistryPersistenceUtil.class);
    GenericArtifactManager manager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
    Mockito.when(manager.getGenericArtifact(apiUUID)).thenReturn(existArtifact);
    Mockito.doNothing().when(manager).updateGenericArtifact(existArtifact);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
    GenericArtifact updatedArtifact = PersistenceHelper.getSampleAPIArtifact();
    updatedArtifact.setAttribute(APIConstants.API_OVERVIEW_DESCRIPTION, api.getDescription());
    PowerMockito.when(RegistryPersistenceUtil.createAPIArtifactContent(any(GenericArtifact.class), any(API.class))).thenReturn(updatedArtifact);
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, existArtifact);
    PublisherAPI updatedAPI = apiPersistenceInstance.updateAPI(org, publisherAPI);
    Assert.assertEquals("Updated API description does not match", "Modified description", updatedAPI.getDescription());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) Tag(org.wso2.carbon.registry.core.Tag) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testGetPublisherAPI.

@Test
public void testGetPublisherAPI() throws Exception {
    Registry registry = Mockito.mock(UserRegistry.class);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.get(anyString())).thenReturn(resource);
    Tag[] tags = new Tag[1];
    Tag tag = new Tag();
    tag.setTagName("testTag");
    tags[0] = tag;
    Mockito.when(registry.getTags(anyString())).thenReturn(tags);
    GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
    String apiUUID = artifact.getId();
    String apiProviderName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
    apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
    String apiName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
    String apiDescription = artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION);
    String apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
    String apiPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR + "api";
    PowerMockito.when(GovernanceUtils.getArtifactPath(registry, apiUUID)).thenReturn(apiPath);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    PublisherAPI publisherAPI = apiPersistenceInstance.getPublisherAPI(org, apiUUID);
    Assert.assertEquals("API UUID does not match", apiUUID, publisherAPI.getId());
    Assert.assertEquals("API Description does not match", apiDescription, publisherAPI.getDescription());
    Assert.assertEquals("API audience does not match", artifact.getAttribute(APIConstants.API_OVERVIEW_AUDIENCE), publisherAPI.getAudience());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Tag(org.wso2.carbon.registry.core.Tag) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class APIMapperTestCase method testAPItoPublisherApiAndBack.

@Test
public void testAPItoPublisherApiAndBack() throws GovernanceException, APIManagementException {
    PublisherAPI pubAPI = APIMapper.INSTANCE.toPublisherApi(api);
    Assert.assertEquals("API name does not match", api.getId().getName(), pubAPI.getApiName());
    Assert.assertEquals("API uuid does not match", api.getUuid(), pubAPI.getId());
    Assert.assertTrue("Mapped api does not have status", pubAPI.toString().contains(api.getStatus()));
    API mappedAPI = APIMapper.INSTANCE.toApi(pubAPI);
    Assert.assertEquals("Mapped api name does not match", mappedAPI.getId().getName(), api.getId().getName());
}
Also used : PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class APIProviderImplTest method testUpdateAPIforStateChange_ToPublished.

@Test
public void testUpdateAPIforStateChange_ToPublished() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, APIPersistenceException, XMLStreamException {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    Set<String> environments = new HashSet<String>();
    environments.add("Production");
    environments.add("Sandbox");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    api.setAsDefaultVersion(true);
    api.setEnvironments(environments);
    api.setOrganization("carbon.super");
    Mockito.when(apimgtDAO.getPublishedDefaultVersion(apiId)).thenReturn("1.0.0");
    Map<String, String> failedGWEnv = new HashMap<String, String>();
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
    PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
    apiProvider.addAPI(api);
    // Mock Updating API
    Resource apiSourceArtifact = Mockito.mock(Resource.class);
    Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
    String apiSourcePath = "path";
    PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
    PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
    Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    Mockito.when(artifact.getId()).thenReturn("12640983654");
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
    apiProvider.updateAPIforStateChange(api, APIConstants.CREATED, APIConstants.PUBLISHED);
    // From the 2 environments, only 1 was successful
    Assert.assertEquals(2, api.getEnvironments().size());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RealmService(org.wso2.carbon.user.core.service.RealmService) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class APIProviderImplTest method testUpdateAPIforStateChange_ToRetired.

@Test
public void testUpdateAPIforStateChange_ToRetired() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, WorkflowException, APIPersistenceException, XMLStreamException {
    APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
    Set<String> environments = new HashSet<String>();
    environments.add("Production");
    environments.add("Sandbox");
    API api = new API(apiId);
    api.setContext("/test");
    api.setStatus(APIConstants.CREATED);
    api.setAsDefaultVersion(true);
    api.setEnvironments(environments);
    api.setOrganization("carbon.super");
    Mockito.when(apimgtDAO.getPublishedDefaultVersion(apiId)).thenReturn("1.0.0");
    Map<String, String> failedGWEnv = new HashMap<String, String>();
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
    Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
    PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
    apiProvider.addAPI(api);
    // Mock Updating API
    Resource apiSourceArtifact = Mockito.mock(Resource.class);
    Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
    String apiSourcePath = "path";
    PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
    PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
    Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
    PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
    Mockito.when(artifact.getId()).thenReturn("12640983654");
    PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
    Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    boolean status = apiProvider.updateAPIforStateChange(api, APIConstants.CREATED, APIConstants.RETIRED);
    Assert.assertEquals(2, api.getEnvironments().size());
    Assert.assertEquals(true, status);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RealmService(org.wso2.carbon.user.core.service.RealmService) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)29 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)24 API (org.wso2.carbon.apimgt.api.model.API)23 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)22 Test (org.junit.Test)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 Resource (org.wso2.carbon.registry.core.Resource)20 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)17 QName (javax.xml.namespace.QName)16 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)15 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)15 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)14 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)14 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)13 RealmService (org.wso2.carbon.user.core.service.RealmService)13 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)13 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 Registry (org.wso2.carbon.registry.core.Registry)9 HashSet (java.util.HashSet)8