use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetDocumentation.
@Test
public void testGetDocumentation() throws APIManagementException, RegistryException {
Registry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registry, null);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String documentationName = "doc1";
String contentPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR + documentationName;
GenericArtifact genericArtifact = getGenericArtifact(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION, "sample");
String docType = "other";
genericArtifact.setAttribute(APIConstants.DOC_TYPE, docType);
genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "URL");
Resource resource = new ResourceImpl();
resource.setUUID(SAMPLE_RESOURCE_ID);
Mockito.when(registry.get(contentPath)).thenThrow(RegistryException.class).thenReturn(resource);
Mockito.when(genericArtifactManager.getGenericArtifact(SAMPLE_RESOURCE_ID)).thenReturn(genericArtifact);
try {
abstractAPIManager.getDocumentation(identifier, DocumentationType.OTHER, documentationName);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get documentation details"));
}
Assert.assertTrue(abstractAPIManager.getDocumentation(identifier, DocumentationType.OTHER, documentationName).getId().equals(genericArtifact.getId()));
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testUpdateWsdl.
@Test
public void testUpdateWsdl() throws APIManagementException, RegistryException {
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
Resource resource = new ResourceImpl();
String resourcePath = "/test/wsdl";
String wsdlContent = "sample wsdl";
Mockito.when(registry.get(resourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
try {
abstractAPIManager.updateWsdl(resourcePath, wsdlContent);
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while updating the existing wsdl"));
}
try {
abstractAPIManager.updateWsdl(resourcePath, wsdlContent);
} catch (APIManagementException e) {
Assert.fail("Error while updating wsdl");
}
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class SequenceUtils method updateResourcePolicyFromRegistryResourceId.
/**
* Updates resource policy resource for the given resource id from the registry.
*
* @param identifier API identifier
* @param resourceId Resource identifier
* @param content resource policy content
* @throws APIManagementException
*/
public static void updateResourcePolicyFromRegistryResourceId(APIIdentifier identifier, String resourceId, String content) throws APIManagementException {
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
APIUtil.loadTenantRegistry(tenantId);
UserRegistry registry = registryService.getGovernanceSystemRegistry(tenantId);
String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SOAP_TO_REST_RESOURCE;
Collection collection = (Collection) registry.get(resourcePath);
String[] resources = collection.getChildren();
if (resources == null) {
handleException("Cannot find any resource policies at the path: " + resourcePath);
}
for (String path : resources) {
Collection resourcePolicyCollection = (Collection) registry.get(path);
String[] resourcePolicies = resourcePolicyCollection.getChildren();
if (resourcePolicies == null) {
if (log.isDebugEnabled()) {
log.debug("Cannot find resource policies under path: " + path);
}
continue;
}
for (String resourcePolicyPath : resourcePolicies) {
Resource resource = registry.get(resourcePolicyPath);
if (StringUtils.isNotEmpty(resourceId) && resourceId.equals(((ResourceImpl) resource).getUUID())) {
resource.setContent(content);
resource.setMediaType(SOAPToRESTConstants.TEXT_XML);
registry.put(resourcePolicyPath, resource);
break;
}
}
}
if (log.isDebugEnabled()) {
log.debug("Number of REST resources for " + resourcePath + " is: " + resources.length);
}
} catch (UserStoreException e) {
handleException("Error while reading tenant information", e);
} catch (RegistryException e) {
handleException("Error when create registry instance", e);
} catch (org.wso2.carbon.registry.api.RegistryException e) {
handleException("Error while setting the resource policy content for the registry resource", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class APIProviderImplTest method testCopyAllDocumentation.
@Test
public void testCopyAllDocumentation() throws APIManagementException, RegistryException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
PowerMockito.when(APIUtil.getAPIDocPath(apiId)).thenReturn("oldVersion");
Resource resource = new ResourceImpl();
Mockito.when(apiProvider.registry.get("oldVersion")).thenReturn(resource);
apiProvider.copyAllDocumentation(apiId, "testVersion");
Mockito.verify(apiProvider.registry);
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class APIProviderImplTest method testSaveGraphqlSchemaDefinition.
@Test
public void testSaveGraphqlSchemaDefinition() throws Exception {
Resource resource = new ResourceImpl();
String resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + "admin" + RegistryConstants.PATH_SEPARATOR + "API1" + RegistryConstants.PATH_SEPARATOR + "1.0.0" + RegistryConstants.PATH_SEPARATOR;
String schemaContent = "sample schema";
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
Mockito.when(APIUtil.getGraphqlDefinitionFilePath("API1", "1.0.0", "admin")).thenReturn(resourcePath);
Resource resourceMock = Mockito.mock(Resource.class);
resourceMock.setContent(schemaContent);
resourceMock.setMediaType(String.valueOf(ContentType.TEXT_PLAIN));
ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
Mockito.when(userRegistry.newResource()).thenReturn(resource);
PowerMockito.doNothing().when(APIUtil.class, "clearResourcePermissions", Mockito.any(), Mockito.any(), Mockito.anyInt());
PowerMockito.doNothing().when(APIUtil.class, "setResourcePermissions", Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
GraphQLSchemaDefinition graphQLSchemaDefinition = Mockito.mock(GraphQLSchemaDefinition.class);
PowerMockito.doCallRealMethod().when(graphQLSchemaDefinition).saveGraphQLSchemaDefinition(api, schemaContent, userRegistry);
// org.wso2.carbon.registry.api.RegistryException
Mockito.doThrow(RegistryException.class).when(registry).put(Matchers.anyString(), any(Resource.class));
try {
graphQLSchemaDefinition.saveGraphQLSchemaDefinition(api, schemaContent, registry);
} catch (APIManagementException e) {
String msg = "Error while adding Graphql Definition for API1-1.0.0";
Assert.assertEquals(msg, e.getMessage());
}
}
Aggregations