use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testAddDocumentationContent.
@Test
public void testAddDocumentationContent() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
String docName = "HowTo";
Documentation doc = new Documentation(DocumentationType.HOWTO, docName);
doc.setVisibility(DocumentVisibility.API_LEVEL);
String docPath = "/apimgt/applicationdata/provider/admin/API1/1.0.0/documentation/contents";
String documentationPath = docPath + docName;
String contentPath = docPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + docName;
Mockito.when(APIUtil.getAPIDocPath(apiId)).thenReturn(docPath);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Resource docResource = Mockito.mock(Resource.class);
Mockito.when(docResource.getUUID()).thenReturn("678ghk");
Mockito.when(apiProvider.registry.get(documentationPath)).thenReturn(docResource);
GenericArtifact docArtifact = Mockito.mock(GenericArtifact.class);
PowerMockito.whenNew(GenericArtifactManager.class).withAnyArguments().thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact("678ghk")).thenReturn(docArtifact);
Mockito.when(APIUtil.getDocumentation(docArtifact)).thenReturn(doc);
Resource docContent = Mockito.mock(Resource.class);
Mockito.when(apiProvider.registry.resourceExists(contentPath)).thenReturn(true, false);
Mockito.when(apiProvider.registry.get(contentPath)).thenReturn(docContent);
Mockito.when(apiProvider.registry.newResource()).thenReturn(docContent);
apiProvider.addDocumentationContent(api, docName, "content");
RegistryAuthorizationManager registryAuthorizationManager = Mockito.mock(RegistryAuthorizationManager.class);
PowerMockito.whenNew(RegistryAuthorizationManager.class).withAnyArguments().thenReturn(registryAuthorizationManager);
apiProvider.tenantDomain = "carbon.super";
doc.setVisibility(DocumentVisibility.OWNER_ONLY);
apiProvider.addDocumentationContent(api, docName, "content");
doc.setVisibility(DocumentVisibility.PRIVATE);
apiProvider.addDocumentationContent(api, docName, "content");
Mockito.doThrow(RegistryException.class).when(apiProvider.registry).put(Matchers.anyString(), any(Resource.class));
try {
apiProvider.addDocumentationContent(api, docName, "content");
} catch (APIManagementException e) {
String msg = "Failed to add the documentation content of : " + docName + " of API :" + apiId.getApiName();
Assert.assertEquals(msg, e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testRestoreAPIRevision.
/**
* This method tests restoring an API Revision to Working Copy
*
* @throws APIManagementException
*/
@Test
public void testRestoreAPIRevision() throws APIManagementException, APIPersistenceException {
ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
apiRevision.setDescription("test description revision 1");
Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
try {
apiProvider.addAPIRevision(apiRevision, superTenantDomain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Mockito.when(apimgtDAO.getRevisionByRevisionUUID(Mockito.anyString())).thenReturn(apiRevision);
PowerMockito.doNothing().when(apiPersistenceInstance).restoreAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt());
try {
apiProvider.restoreAPIRevision("63e1e37e-a5b8-4be6-86a5-d6ae0749f131", "b55e0fc3-9829-4432-b99e-02056dc91838", superTenantDomain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAPILifeCycleData.
@Test
public void testGetAPILifeCycleData() throws Exception {
APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
PowerMockito.when(APIUtil.getAPIPath(identifier)).thenReturn(path);
PowerMockito.when(apiProvider.registry.get(path)).thenReturn(apiSourceArtifact);
Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
Mockito.when(artifact.getLifecycleState()).thenReturn("Created");
LifecycleBean bean = getLCBean();
Mockito.when(LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) apiProvider.registry, apiProvider.configRegistry)).thenReturn(bean);
Map<String, Object> lcData = apiProvider.getAPILifeCycleData(identifier);
List checkListItems = (List) lcData.get(APIConstants.LC_CHECK_ITEMS);
Assert.assertEquals(2, checkListItems.size());
if (checkListItems.get(0) instanceof CheckListItem) {
CheckListItem checkListItem = (CheckListItem) checkListItems.get(0);
Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
Assert.assertEquals("1", checkListItem.getOrder());
} else {
Assert.assertEquals("0", checkListItem.getOrder());
}
Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
}
if (checkListItems.get(1) instanceof CheckListItem) {
CheckListItem checkListItem = (CheckListItem) checkListItems.get(1);
Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
Assert.assertEquals("1", checkListItem.getOrder());
} else {
Assert.assertEquals("0", checkListItem.getOrder());
}
Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testCheckIfAPIExists.
@Test
public void testCheckIfAPIExists() throws APIManagementException, UserStoreException, RegistryException, XMLStreamException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn("testPath");
PowerMockito.mockStatic(MultitenantUtils.class);
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
// Mock Config system registry
ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry systemReg = Mockito.mock(UserRegistry.class);
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tm = Mockito.mock(TenantManager.class);
PowerMockito.when(sh.getRealmService()).thenReturn(realmService);
PowerMockito.when(realmService.getTenantManager()).thenReturn(tm);
PowerMockito.when(tm.getTenantId(Matchers.anyString())).thenReturn(-1234);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
PowerMockito.when(registryService.getGovernanceSystemRegistry(-1234)).thenReturn(systemReg);
Mockito.when(systemReg.resourceExists("testPath")).thenReturn(true);
Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super");
apiProvider.tenantDomain = "carbon.super1";
PowerMockito.when(registryService.getGovernanceUserRegistry("admin", -1234)).thenReturn(systemReg);
Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
apiProvider.tenantDomain = null;
apiProvider.registry = systemReg;
Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testAddAPINameWithIllegalCharacters.
@Test
public void testAddAPINameWithIllegalCharacters() throws APIManagementException, GovernanceException {
APIIdentifier apiId = new APIIdentifier("admin", "API2&", "1.0.2");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
try {
apiProvider.addAPI(api);
Assert.fail("Exception was expected, but wasn't thrown");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("API Name contains one or more illegal characters"));
}
}
Aggregations