Search in sources :

Example 41 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class RegistryPersistenceUtilTestCase method testAPIGet.

@Test
public void testAPIGet() throws APIManagementException, RegistryException, UserStoreException {
    GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
    API api = RegistryPersistenceUtil.getAPI(artifact, registry);
    Assert.assertEquals("Attibute overview_type does not match", artifact.getAttribute("overview_type"), api.getType());
    Assert.assertEquals("API id does not match", artifact.getId(), api.getUuid());
    Assert.assertEquals("API tag does not match", "testTag", api.getTags().iterator().next());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 42 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class RegistryPersistenceUtilTestCase method testcreateAPIArtifactContent.

@Test
public void testcreateAPIArtifactContent() throws APIPersistenceException, APIManagementException, RegistryException {
    API api = new API(new APIIdentifier("pubuser", "TestAPI", "1.0"));
    Set<Tier> availableTiers = new HashSet<Tier>();
    availableTiers.add(new Tier("Unlimited"));
    availableTiers.add(new Tier("Gold"));
    api.setAvailableTiers(availableTiers);
    Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
    URITemplate template = new URITemplate();
    template.setHTTPVerb("GET");
    template.setUriTemplate("/test");
    template.setAuthType("None");
    uriTemplates.add(template);
    api.setUriTemplates(uriTemplates);
    List<APICategory> categories = new ArrayList<APICategory>();
    APICategory category = new APICategory();
    category.setName("testcategory");
    categories.add(category);
    api.setApiCategories(categories);
    List<Label> gatewayLabels = new ArrayList<Label>();
    Label label = new Label();
    label.setName("TestLabel");
    gatewayLabels.add(label);
    GenericArtifact genericArtifact = new GenericArtifactImpl(new QName("", "TestAPI", ""), "application/vnd.wso2-api+xml");
    genericArtifact.setAttribute("URITemplate", "/test");
    GenericArtifact retArtifact = RegistryPersistenceUtil.createAPIArtifactContent(genericArtifact, api);
    Assert.assertEquals("API name does not match", api.getId().getApiName(), retArtifact.getAttribute("overview_name"));
    Assert.assertEquals("API version does not match", api.getId().getVersion(), retArtifact.getAttribute("overview_version"));
    Assert.assertEquals("API provider does not match", api.getId().getProviderName(), retArtifact.getAttribute("overview_provider"));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Tier(org.wso2.carbon.apimgt.api.model.Tier) QName(javax.xml.namespace.QName) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.api.model.Label) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 43 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class RegistryPersistenceUtilTestCase method testcreateAPIProductArtifactContent.

@Test
public void testcreateAPIProductArtifactContent() throws APIPersistenceException, APIManagementException, RegistryException {
    APIProduct product = new APIProduct(new APIProductIdentifier("pubuser", "TestAPIProd", "1.0.0"));
    GenericArtifact genericArtifact = new GenericArtifactImpl(new QName("", "TestAPIProd", ""), "application/vnd.wso2-api+xml");
    List<APICategory> categories = new ArrayList<APICategory>();
    APICategory category = new APICategory();
    category.setName("testcategory");
    categories.add(category);
    product.setApiCategories(categories);
    Set<Tier> availableTiers = new HashSet<Tier>();
    availableTiers.add(new Tier("Unlimited"));
    availableTiers.add(new Tier("Gold"));
    product.setAvailableTiers(availableTiers);
    GenericArtifact retArtifact = RegistryPersistenceUtil.createAPIProductArtifactContent(genericArtifact, product);
    Assert.assertEquals("API name does not match", product.getId().getName(), retArtifact.getAttribute("overview_name"));
    Assert.assertEquals("API version does not match", product.getId().getVersion(), retArtifact.getAttribute("overview_version"));
    Assert.assertEquals("API provider does not match", product.getId().getProviderName(), retArtifact.getAttribute("overview_provider"));
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Tier(org.wso2.carbon.apimgt.api.model.Tier) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 44 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class WorkflowUtils method completeStateChangeWorkflow.

/**
 * Complete the lifecycle state change workflow
 *
 * @param workflowDTO Workflow DTO object
 * @throws WorkflowException Exception when completing the workflow
 */
protected static void completeStateChangeWorkflow(WorkflowDTO workflowDTO) throws WorkflowException {
    String externalWorkflowRef = workflowDTO.getExternalWorkflowReference();
    try {
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        Workflow workflow = apiMgtDAO.getworkflowReferenceByExternalWorkflowReference(externalWorkflowRef);
        String apiName = workflow.getMetadata("ApiName");
        String action = workflow.getMetadata("Action");
        String providerName = workflow.getMetadata("ApiProvider");
        String version = workflow.getMetadata("ApiVersion");
        String invoker = workflow.getMetadata("Invoker");
        String currentStatus = workflow.getMetadata("CurrentState");
        int tenantId = workflowDTO.getTenantId();
        try {
            // tenant flow is already started from the rest api service impl. no need to start from here
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(invoker);
            Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(invoker, tenantId);
            APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, version);
            GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
            if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
                String targetStatus;
                apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
                targetStatus = apiArtifact.getLifecycleState();
                if (!currentStatus.equals(targetStatus)) {
                    apiMgtDAO.recordAPILifeCycleEvent(apiArtifact.getId(), currentStatus.toUpperCase(), targetStatus.toUpperCase(), invoker, tenantId);
                }
                if (log.isDebugEnabled()) {
                    String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API " + "Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
                    log.debug(logMessage);
                }
            }
        } catch (RegistryException e) {
            String errorMsg = "Could not complete api state change workflow";
            log.error(errorMsg, e);
            throw new WorkflowException(errorMsg, e);
        }
    } catch (APIManagementException e) {
        String errorMsg = "Could not complete api state change workflow";
        log.error(errorMsg, e);
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Workflow(org.wso2.carbon.apimgt.api.model.Workflow) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 45 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class APIProviderImplTest method testSearchAPIs_NoProviderId.

@Test
public void testSearchAPIs_NoProviderId() throws APIManagementException, RegistryException {
    API api1 = new API(new APIIdentifier("admin", "API1", "1.0.1"));
    api1.setContext("api1context");
    api1.setStatus(APIConstants.PUBLISHED);
    api1.setDescription("API 1 Desciption");
    Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
    URITemplate uriTemplate1 = new URITemplate();
    uriTemplate1.setHTTPVerb("POST");
    uriTemplate1.setAuthType("Application");
    uriTemplate1.setUriTemplate("/add");
    uriTemplate1.setThrottlingTier("Gold");
    uriTemplates.add(uriTemplate1);
    api1.setUriTemplates(uriTemplates);
    API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
    api2.setContext("api2context");
    api2.setStatus(APIConstants.CREATED);
    api2.setDescription("API 2 Desciption");
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    GenericArtifact genericArtifact1 = Mockito.mock(GenericArtifact.class);
    GenericArtifact genericArtifact2 = Mockito.mock(GenericArtifact.class);
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API1");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.1");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api1context");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 1 Desciption");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
    Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Published");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API2");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.0");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api2context");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 2 Desciption");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
    Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Created");
    Mockito.when(APIUtil.getAPI(genericArtifact1, apiProvider.registry)).thenReturn(api1);
    Mockito.when(APIUtil.getAPI(genericArtifact2, apiProvider.registry)).thenReturn(api2);
    Mockito.when(APIUtil.getAPI(genericArtifact1)).thenReturn(api1);
    Mockito.when(APIUtil.getAPI(genericArtifact2)).thenReturn(api2);
    GenericArtifact[] genericArtifacts = { genericArtifact1, genericArtifact2 };
    Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
    // Search by Name matching both APIs
    List<API> foundApiList0 = apiProvider.searchAPIs("API", "Name", null);
    Assert.assertNotNull(foundApiList0);
    Assert.assertEquals(2, foundApiList0.size());
    // Search by exact name
    List<API> foundApiList1 = apiProvider.searchAPIs("API1", "Name", null);
    Assert.assertNotNull(foundApiList1);
    Assert.assertEquals(1, foundApiList1.size());
    // Search by exact provider
    List<API> foundApiList2 = apiProvider.searchAPIs("admin", "Provider", null);
    Assert.assertNotNull(foundApiList2);
    Assert.assertEquals(2, foundApiList2.size());
    // Search by exact version
    List<API> foundApiList3 = apiProvider.searchAPIs("1.0.0", "Version", null);
    Assert.assertNotNull(foundApiList3);
    Assert.assertEquals(1, foundApiList3.size());
    // Search by exact context
    List<API> foundApiList4 = apiProvider.searchAPIs("api1context", "Context", null);
    Assert.assertNotNull(foundApiList4);
    Assert.assertEquals(1, foundApiList4.size());
    // Search by exact context
    List<API> foundApiList5 = apiProvider.searchAPIs("api2context", "Context", null);
    Assert.assertNotNull(foundApiList5);
    Assert.assertEquals(1, foundApiList5.size());
    // Search by wrong context
    List<API> foundApiList6 = apiProvider.searchAPIs("test", "Context", null);
    Assert.assertNotNull(foundApiList6);
    Assert.assertEquals(0, foundApiList6.size());
    // Search by status
    List<API> foundApiList7 = apiProvider.searchAPIs("Published", "Status", null);
    Assert.assertNotNull(foundApiList7);
    Assert.assertEquals(1, foundApiList7.size());
    // Search by Description
    List<API> foundApiList8 = apiProvider.searchAPIs("API 1 Desciption", "Description", null);
    Assert.assertNotNull(foundApiList8);
    Assert.assertEquals(1, foundApiList8.size());
    // Search by Description
    List<API> foundApiList9 = apiProvider.searchAPIs("API", "Description", null);
    Assert.assertNotNull(foundApiList9);
    Assert.assertEquals(2, foundApiList9.size());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)141 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)84 Resource (org.wso2.carbon.registry.core.Resource)74 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)74 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)73 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)66 Registry (org.wso2.carbon.registry.core.Registry)65 Test (org.junit.Test)54 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)54 API (org.wso2.carbon.apimgt.api.model.API)51 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)40 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)38 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)36 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)35 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)35 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)26 UserStoreException (org.wso2.carbon.user.api.UserStoreException)24 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)22 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)19