Search in sources :

Example 36 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testGetOASDefinition.

@Test
public void testGetOASDefinition() throws OASPersistenceException, 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 definitionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(apiProviderName) + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR + APIConstants.API_OAS_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 = "{\"swagger\":\"2.0\",\"info\":{\"description\":\"This is a sample server Petstore server\"}}";
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
    Mockito.when(registry.resourceExists(definitionPath)).thenReturn(true);
    Resource oasResource = new ResourceImpl();
    oasResource.setContent(definition.getBytes());
    Mockito.when(registry.get(definitionPath)).thenReturn(oasResource);
    String def = apiPersistenceInstance.getOASDefinition(org, apiUUID);
    Assert.assertEquals("API oas definition does not match", definition, def);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl 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());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Resource(org.wso2.carbon.registry.core.Resource) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Tag(org.wso2.carbon.registry.core.Tag) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl 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()));
}
Also used : LocalDateTime(java.time.LocalDateTime) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Timestamp(java.sql.Timestamp) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) Tag(org.wso2.carbon.registry.core.Tag) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl 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());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) Tag(org.wso2.carbon.registry.core.Tag) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with ResourceImpl

use of org.wso2.carbon.registry.core.ResourceImpl in project carbon-apimgt by wso2.

the class SequenceUtilsTestCase method testGetRestToSoapConvertedSequence.

public void testGetRestToSoapConvertedSequence() throws Exception {
    String provider = "admin";
    String apiName = "test-api";
    String version = "1.0.0";
    String seqType = "in";
    API api = new API(new APIIdentifier(provider, apiName, version));
    List<SOAPToRestSequence> soapToRestSequences = new ArrayList<SOAPToRestSequence>();
    SOAPToRestSequence seq = new SOAPToRestSequence("post", "test", "<>", Direction.IN);
    soapToRestSequences.add(seq);
    SOAPToRestSequence seq2 = new SOAPToRestSequence("post", "test", "<>", Direction.OUT);
    soapToRestSequences.add(seq2);
    api.setSoapToRestSequences(soapToRestSequences);
    String resourceName = "test_get.xml";
    Resource resource = Mockito.mock(Resource.class);
    ResourceImpl resourceImpl = Mockito.mock(ResourceImpl.class);
    Collection collection = Mockito.mock(Collection.class);
    String[] paths = new String[0];
    byte[] content = new byte[1];
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super");
    PowerMockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(((Collection) userRegistry.get(Mockito.anyString()))).thenReturn(collection);
    Mockito.when(collection.getChildren()).thenReturn(paths);
    Mockito.when(userRegistry.get(Mockito.anyString())).thenReturn(resource);
    Mockito.when(resource.getContent()).thenReturn(content);
    Mockito.when(resourceImpl.getName()).thenReturn(resourceName);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenReturn(-1);
    try {
        SequenceUtils.getRestToSoapConvertedSequence(api, seqType);
    } catch (APIManagementException e) {
        Assert.fail("Failed to get the sequences from the registry");
    }
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)43 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)43 Test (org.junit.Test)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)36 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)20 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)20 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)20 Registry (org.wso2.carbon.registry.core.Registry)17 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)17 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)16 Matchers.anyString (org.mockito.Matchers.anyString)12 Collection (org.wso2.carbon.registry.core.Collection)12 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)10 API (org.wso2.carbon.apimgt.api.model.API)9 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)9 CollectionImpl (org.wso2.carbon.registry.core.CollectionImpl)9 Tag (org.wso2.carbon.registry.core.Tag)9 ResourceDO (org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO)9 InputStream (java.io.InputStream)7 ArrayInputStream (org.apache.derby.iapi.services.io.ArrayInputStream)6