use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class CustomAPIIndexerTest method testIndexingCustomProperties.
/**
* This method checks the indexer's behaviour for APIs which does not have the relevant custom properties.
*
* @throws RegistryException Registry Exception.
* @throws APIManagementException API Management Exception.
*/
@Test
public void testIndexingCustomProperties() throws RegistryException, APIManagementException {
Resource resource = new ResourceImpl();
PowerMockito.mockStatic(APIUtil.class);
Mockito.doReturn(resource).when(userRegistry).get(Mockito.anyString());
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
GenericArtifact genericArtifact = Mockito.mock(GenericArtifact.class);
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.anyString())).thenReturn(genericArtifact);
Mockito.when(genericArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY)).thenReturn("public");
PowerMockito.when(APIUtil.getAPI(genericArtifact, userRegistry)).thenReturn(Mockito.mock(API.class));
resource.setProperty(APIConstants.API_RELATED_CUSTOM_PROPERTIES_PREFIX + APIConstants.CUSTOM_API_INDEXER_PROPERTY, APIConstants.CUSTOM_API_INDEXER_PROPERTY);
Assert.assertEquals(APIConstants.OVERVIEW_PREFIX + APIConstants.API_RELATED_CUSTOM_PROPERTIES_PREFIX + APIConstants.CUSTOM_API_INDEXER_PROPERTY, indexer.getIndexedDocument(file2Index).getFields().keySet().toArray()[0].toString());
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getSoapToRestSequences.
protected List<SOAPToRestSequence> getSoapToRestSequences(Registry registry, API api, Direction direction) throws RegistryException, APIPersistenceException {
String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(api.getId().getProviderName()) + RegistryConstants.PATH_SEPARATOR + api.getId().getName() + RegistryConstants.PATH_SEPARATOR + api.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR;
if (direction == Direction.IN) {
resourcePath = resourcePath + "in";
} else if (direction == Direction.OUT) {
resourcePath = resourcePath + "out";
} else {
throw new APIPersistenceException("Invalid sequence type");
}
List<SOAPToRestSequence> sequences = new ArrayList<SOAPToRestSequence>();
if (registry.resourceExists(resourcePath)) {
Collection collection = (Collection) registry.get(resourcePath);
String[] resources = collection.getChildren();
for (String path : resources) {
Resource resource = registry.get(path);
String content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
String resourceName;
if (resource.getProperty("resourcePath") != null) {
resourceName = resource.getProperty("resourcePath");
} else {
resourceName = ((ResourceImpl) resource).getName();
}
resourceName = resourceName.replaceAll("\\.xml", "");
resourceName = resourceName.split("_")[0];
String httpMethod = resource.getProperty("method");
SOAPToRestSequence seq = new SOAPToRestSequence(httpMethod, resourceName, content, direction);
seq.setUuid(resource.getUUID());
sequences.add(seq);
}
}
return sequences;
}
use of org.wso2.carbon.registry.core.ResourceImpl 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.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testGetGraphQLSchema.
@Test
public void testGetGraphQLSchema() throws GraphQLPersistenceException, RegistryException {
Registry registry = Mockito.mock(Registry.class);
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 apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR;
String schemaName = apiProviderName + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
String schemaResourePath = path + schemaName;
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);
String schema = "{\n" + " hero {\n" + " name\n" + " friends {\n" + " name\n" + " }\n" + " }\n" + "}";
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
Mockito.when(registry.resourceExists(schemaResourePath)).thenReturn(true);
Resource oasResource = new ResourceImpl();
oasResource.setContent(schema.getBytes());
Mockito.when(registry.get(schemaResourePath)).thenReturn(oasResource);
String def = apiPersistenceInstance.getGraphQLSchema(org, apiUUID);
Assert.assertEquals("API graphql schema does not match", schema, def);
}
use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testGetAsyncDefinition.
@Test
public void testGetAsyncDefinition() throws AsyncSpecPersistenceException, RegistryException, APIPersistenceException, APIManagementException {
Registry registry = Mockito.mock(UserRegistry.class);
GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
String apiUUID = artifact.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(artifact);
Mockito.doNothing().when(manager).updateGenericArtifact(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 definitionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(apiProviderName) + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ASYNC_API_DEFINITION_RESOURCE_NAME;
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);
String definition = "{\"asyncapi\":\"2.0.0\",\"info\":{\"description\":\"This is a sample Async API\"}}";
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
Mockito.when(registry.resourceExists(definitionPath)).thenReturn(true);
Resource asyncResource = new ResourceImpl();
asyncResource.setContent(definition.getBytes());
Mockito.when(registry.get(definitionPath)).thenReturn(asyncResource);
String def = apiPersistenceInstance.getAsyncDefinition(org, apiUUID);
Assert.assertEquals("API Async definition does not match", definition, def);
}
Aggregations