use of org.wso2.carbon.apimgt.persistence.APIPersistence in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testRegistrySelectionForSuperTenantUser.
@Test
public void testRegistrySelectionForSuperTenantUser() throws Exception {
RegistryService registryService = Mockito.mock(RegistryService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(tenantManager.getTenantId(SUPER_TENANT_DOMAIN)).thenReturn(SUPER_TENANT_ID);
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext context = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(context);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PrivilegedCarbonContext privilegedContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedContext);
PowerMockito.mockStatic(GovernanceUtils.class);
GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
artifacts.add(artifact);
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.any(String.class), Mockito.any(Registry.class), Mockito.any(String.class), Mockito.any(Boolean.class))).thenReturn(artifacts);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
ServiceReferenceHolder serviceRefHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceRefHolder);
RealmService realmService = Mockito.mock(RealmService.class);
PowerMockito.when(serviceRefHolder.getRealmService()).thenReturn(realmService);
UserRealm realm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(SUPER_TENANT_ID)).thenReturn(realm);
PowerMockito.doNothing().when(RegistryPersistenceUtil.class, "loadloadTenantAPIRXT", Mockito.any(String.class), Mockito.any(Integer.class));
Mockito.when(context.getTenantDomain()).thenReturn(SUPER_TENANT_DOMAIN);
Mockito.when(context.getTenantId()).thenReturn(SUPER_TENANT_ID);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// return null artifact because we are not testing artifact related params. this is only to get the registry obj
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.any(String.class))).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.any(String.class))).thenReturn(null);
// trigger registry object creation
UserContext ctx = new UserContext("user", new Organization(SUPER_TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("user", SUPER_TENANT_ID);
apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// trigger registry object creation
ctx = new UserContext("wso2.anonymous.user", new Organization(SUPER_TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("wso2.anonymous.user", SUPER_TENANT_ID);
}
use of org.wso2.carbon.apimgt.persistence.APIPersistence in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testGetPublisherAPIProduct.
@Test
public void testGetPublisherAPIProduct() 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.getSampleAPIProductArtifact();
String apiProductId = artifact.getId();
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
Organization org = new Organization(SUPER_TENANT_DOMAIN);
PublisherAPIProduct productAPI = apiPersistenceInstance.getPublisherAPIProduct(org, apiProductId);
Assert.assertEquals("API Product UUID does not match", apiProductId, productAPI.getId());
}
use of org.wso2.carbon.apimgt.persistence.APIPersistence 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.APIPersistence 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.APIPersistence in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testThumbnailTasks.
@Test
public void testThumbnailTasks() throws Exception {
Registry registry = Mockito.mock(Registry.class);
GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
String apiUUID = artifact.getId();
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
String apiProviderName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
String apiName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
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);
Mockito.when(registry.resourceExists(thumbPath)).thenReturn(true);
Resource imageResource = Mockito.mock(Resource.class);
Mockito.when(registry.get(thumbPath)).thenReturn(imageResource);
apiPersistenceInstance.getThumbnail(org, apiUUID);
Mockito.verify(registry, times(1)).get(thumbPath);
apiPersistenceInstance.deleteThumbnail(org, apiUUID);
Mockito.verify(registry, times(1)).delete(thumbPath);
}
Aggregations