use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testAddAPIProduct.
@Test
public void testAddAPIProduct() throws RegistryException, APIPersistenceException, APIManagementException {
GenericArtifact artifact = PersistenceHelper.getSampleAPIProductArtifact();
PublisherAPIProduct publisherAPI = new PublisherAPIProduct();
publisherAPI.setApiProductName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
publisherAPI.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
publisherAPI.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
APIProduct api = APIProductMapper.INSTANCE.toApiProduct(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);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
GenericArtifactManager manager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
PowerMockito.when(RegistryPersistenceUtil.createAPIProductArtifactContent(any(GenericArtifact.class), any(APIProduct.class))).thenReturn(artifact);
GenericArtifact newArtifact = Mockito.mock(GenericArtifact.class);
Mockito.when(manager.newGovernanceArtifact(new QName(publisherAPI.getApiProductName()))).thenReturn(newArtifact);
Mockito.when(manager.getGenericArtifact(any(String.class))).thenReturn(newArtifact);
Mockito.doNothing().when(newArtifact).invokeAction("Publish", APIConstants.API_LIFE_CYCLE);
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
apiPersistenceInstance.addAPIProduct(org, publisherAPI);
}
use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testAddAPI.
@Test
public void testAddAPI() throws RegistryException, APIPersistenceException, APIManagementException {
GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
PublisherAPI publisherAPI = new PublisherAPI();
publisherAPI.setApiName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
publisherAPI.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
publisherAPI.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
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);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
GenericArtifactManager manager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
// add 1 year old timestamp since it is not the latest
LocalDateTime now = LocalDateTime.now().minusDays(365);
Timestamp timestamp = Timestamp.valueOf(now);
PowerMockito.when(RegistryPersistenceUtil.createAPIArtifactContent(any(GenericArtifact.class), any(API.class))).thenReturn(artifact);
GenericArtifact newArtifact = Mockito.mock(GenericArtifact.class);
publisherAPI.setVersionTimestamp(timestamp.getTime() + "");
Mockito.when(manager.newGovernanceArtifact(new QName(api.getId().getApiName()))).thenReturn(newArtifact);
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
PublisherAPI returnAPI = apiPersistenceInstance.addAPI(org, publisherAPI);
Assert.assertEquals(returnAPI.getVersionTimestamp(), String.valueOf(timestamp.getTime()));
}
use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testUpdateAPIProduct.
@Test
public void testUpdateAPIProduct() throws APIPersistenceException, RegistryException, APIManagementException {
PublisherAPIProduct publisherAPI = new PublisherAPIProduct();
publisherAPI.setDescription("Modified description");
APIProduct api = APIProductMapper.INSTANCE.toApiProduct(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.getSampleAPIProductArtifact();
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.getSampleAPIProductArtifact();
updatedArtifact.setAttribute(APIConstants.API_OVERVIEW_DESCRIPTION, api.getDescription());
PowerMockito.when(RegistryPersistenceUtil.createAPIProductArtifactContent(any(GenericArtifact.class), any(APIProduct.class))).thenReturn(updatedArtifact);
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, existArtifact);
PublisherAPIProduct updatedAPI = apiPersistenceInstance.updateAPIProduct(org, publisherAPI);
Assert.assertEquals("Updated API description does not match", "Modified description", updatedAPI.getDescription());
}
use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.
the class RegistryPersistenceUtilTestCase method testGetAPIForSearch.
@Test
public void testGetAPIForSearch() throws APIPersistenceException, GovernanceException {
GenericArtifact genericArtifact = PersistenceHelper.getSampleAPIArtifact();
PublisherAPI api = RegistryPersistenceUtil.getAPIForSearch(genericArtifact);
Assert.assertEquals("API name does not match", genericArtifact.getAttribute("overview_name"), api.getApiName());
Assert.assertEquals("API version does not match", genericArtifact.getAttribute("overview_version"), api.getVersion());
Assert.assertEquals("API provider does not match", genericArtifact.getAttribute("overview_provider"), api.getProviderName());
}
use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.
the class APIProviderImplTest method testUpdateAPIforStateChange_ToRetiredWithFaultyGWs.
@Test
public void testUpdateAPIforStateChange_ToRetiredWithFaultyGWs() 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");
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");
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);
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);
try {
apiProvider.updateAPIforStateChange(api, APIConstants.CREATED, APIConstants.RETIRED);
} catch (FaultGatewaysException e) {
Assert.assertTrue(e.getFaultMap().contains("Failed to un-publish"));
}
Assert.assertEquals(1, api.getEnvironments().size());
}
Aggregations