Search in sources :

Example 46 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class RegistrySearchUtilTestCase method testAnonymousUserQueryInDevPortal.

@Test
public void testAnonymousUserQueryInDevPortal() throws APIPersistenceException {
    // Normal dev portal api listing
    String inputQuery = "";
    UserContext ctx = new UserContext("wso2.anonymous.user", organization, null, anonymousRoles);
    String searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    String expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&name=*&enableStore=(true OR null)" + "&group=true&group.field=name&group.ngroups=true&group.sort=versionTimestamp desc&lcState=(PUBLISHED OR PROTOTYPED)";
    Assert.assertEquals("Generated query mismatched. ", expected, searchQuery);
    // search for 'test' in description
    inputQuery = "description:test";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&" + "description=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for description search. ", expected, searchQuery);
    // search for provider 'pubuser'
    inputQuery = "provider:pubuser";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&" + "provider=*pubuser*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for provider search. ", expected, searchQuery);
    // search for propertyname 'test'
    inputQuery = "property_name:test";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)" + "&api_meta.property_name__display=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for property search. ", expected, searchQuery);
}
Also used : UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Test(org.junit.Test)

Example 47 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class RegistrySearchUtilTestCase method testPublisherUserQueryInPublisher.

@Test
public void testPublisherUserQueryInPublisher() throws APIPersistenceException {
    // Normal publisher api listing
    String inputQuery = "";
    UserContext ctx = new UserContext("publisher", organization, null, publisherRoles);
    String searchQuery = RegistrySearchUtil.getPublisherSearchQuery(inputQuery, ctx);
    String expected = "publisher_roles=(null OR internal\\/creator OR internal\\/publisher OR internal\\/everyone)" + "&name=*&type=(HTTP OR WS OR SOAPTOREST OR GRAPHQL OR SOAP OR SSE OR WEBSUB OR WEBHOOK OR ASYNC)";
    Assert.assertEquals("Generated query mismatched. ", expected, searchQuery);
    // search for 'test' in description
    inputQuery = "description:test";
    expected = "publisher_roles=(null OR internal\\/creator OR internal\\/publisher OR internal\\/everyone)" + "&description=*test*&type=(HTTP OR WS OR SOAPTOREST OR GRAPHQL OR SOAP OR SSE OR WEBSUB OR WEBHOOK " + "OR ASYNC)";
    searchQuery = RegistrySearchUtil.getPublisherSearchQuery(inputQuery, ctx);
    Assert.assertEquals("Generated query mismatched for description search. ", expected, searchQuery);
}
Also used : UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Test(org.junit.Test)

Example 48 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method getApiByUUID.

public API getApiByUUID(String apiId, String deployment, String organization) {
    String sql = SubscriptionValidationSQLConstants.GET_API_BY_UUID_SQL;
    if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
        sql = sql.concat("AND AM_API.CONTEXT NOT LIKE '/t/%'");
    } else {
        sql = sql.concat("AND AM_API.CONTEXT LIKE '/t/" + organization + "%'");
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, apiId);
            preparedStatement.setString(2, deployment);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    String deploymentName = resultSet.getString("DEPLOYMENT_NAME");
                    String apiType = resultSet.getString("API_TYPE");
                    String version = resultSet.getString("API_VERSION");
                    String apiUuid = resultSet.getString("API_UUID");
                    API api = new API();
                    String provider = resultSet.getString("API_PROVIDER");
                    String name = resultSet.getString("API_NAME");
                    api.setApiUUID(apiUuid);
                    api.setApiId(resultSet.getInt("API_ID"));
                    api.setVersion(version);
                    api.setProvider(provider);
                    api.setName(name);
                    api.setApiType(apiType);
                    api.setPolicy(resultSet.getString("API_TIER"));
                    api.setContext(resultSet.getString("CONTEXT"));
                    api.setStatus(resultSet.getString("STATUS"));
                    String revision = resultSet.getString("REVISION_UUID");
                    api.setIsDefaultVersion(isAPIDefaultVersion(connection, provider, name, version));
                    api.setPolicy(getAPILevelTier(connection, apiUuid, revision));
                    if (APIConstants.API_PRODUCT.equals(apiType)) {
                        attachURlMappingDetailsOfApiProduct(connection, api);
                        return api;
                    } else {
                        if (deployment.equals(deploymentName)) {
                            attachURLMappingDetails(connection, revision, api);
                            return api;
                        }
                    }
                }
            }
        }
    } catch (SQLException e) {
        log.error("Error in loading API for api : " + apiId + " : " + deployment, e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) API(org.wso2.carbon.apimgt.api.model.subscription.API)

Example 49 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testGetWSDL.

@Test
public void testGetWSDL() throws Exception {
    Registry registry = Mockito.mock(Registry.class);
    GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
    String apiUUID = artifact.getId();
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, 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 artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
    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 wsdlResourcePath = artifactPath + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
    Mockito.when(registry.resourceExists(wsdlResourcePath)).thenReturn(true);
    Resource wsdlResource = Mockito.mock(Resource.class);
    Mockito.when(registry.get(wsdlResourcePath)).thenReturn(wsdlResource);
    apiPersistenceInstance.getWSDL(org, apiUUID);
    Mockito.verify(registry, times(1)).get(wsdlResourcePath);
    // WSDL zip test
    String wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
    Mockito.when(registry.resourceExists(wsdlResourcePath)).thenReturn(false);
    Mockito.when(registry.resourceExists(wsdlResourcePathOld)).thenReturn(false);
    // zip location
    wsdlResourcePath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
    Mockito.when(registry.resourceExists(wsdlResourcePath)).thenReturn(true);
    Mockito.when(registry.get(wsdlResourcePath)).thenReturn(wsdlResource);
    apiPersistenceInstance.getWSDL(org, apiUUID);
    Mockito.verify(registry, times(1)).get(wsdlResourcePath);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 50 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testRegistrySelectionForTenantUserCrossTenatAccess.

@Test
public void testRegistrySelectionForTenantUserCrossTenatAccess() throws Exception {
    RegistryService registryService = Mockito.mock(RegistryService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    Mockito.when(tenantManager.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID);
    Mockito.when(tenantManager.getTenantId(SUPER_TENANT_DOMAIN)).thenReturn(SUPER_TENANT_ID);
    PowerMockito.mockStatic(CarbonContext.class);
    CarbonContext context = Mockito.mock(CarbonContext.class);
    PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(context);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PrivilegedCarbonContext privilegedContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedContext);
    PowerMockito.mockStatic(GovernanceUtils.class);
    GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
    List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
    artifacts.add(artifact);
    PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.any(String.class), Mockito.any(Registry.class), Mockito.any(String.class), Mockito.any(Boolean.class))).thenReturn(artifacts);
    PowerMockito.mockStatic(RegistryPersistenceUtil.class);
    ServiceReferenceHolder serviceRefHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceRefHolder);
    RealmService realmService = Mockito.mock(RealmService.class);
    PowerMockito.when(serviceRefHolder.getRealmService()).thenReturn(realmService);
    UserRealm realm = Mockito.mock(UserRealm.class);
    PowerMockito.when(realmService.getTenantUserRealm(TENANT_ID)).thenReturn(realm);
    PowerMockito.doNothing().when(RegistryPersistenceUtil.class, "loadloadTenantAPIRXT", Mockito.any(String.class), Mockito.any(Integer.class));
    Mockito.when(context.getTenantDomain()).thenReturn(TENANT_DOMAIN);
    Mockito.when(context.getTenantId()).thenReturn(TENANT_ID);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
    // return null artifact because we are not testing artifact related params. this is only to get the registry obj
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.any(String.class))).thenReturn(artifactManager);
    Mockito.when(artifactManager.getGenericArtifact(Mockito.any(String.class))).thenReturn(null);
    // trigger registry object creation. access super tenant api
    UserContext ctx = new UserContext("user", new Organization(TENANT_DOMAIN), null, null);
    apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
    // check whether super tenant's system registy is accessed
    Mockito.verify(registryService, times(1)).getGovernanceSystemRegistry((SUPER_TENANT_ID));
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.persistence.internal.ServiceReferenceHolder) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) ArrayList(java.util.ArrayList) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Matchers.anyString(org.mockito.Matchers.anyString) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) CarbonContext(org.wso2.carbon.context.CarbonContext) UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)304 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)106 API (org.wso2.carbon.apimgt.api.model.API)100 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)89 ArrayList (java.util.ArrayList)79 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)72 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)65 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)64 IOException (java.io.IOException)61 Registry (org.wso2.carbon.registry.core.Registry)58 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)57 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)56 HashMap (java.util.HashMap)54 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)53 Resource (org.wso2.carbon.registry.core.Resource)51 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)49 JSONObject (org.json.simple.JSONObject)45 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 URISyntaxException (java.net.URISyntaxException)42