Search in sources :

Example 31 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class APIUtilTest method testGetGatewayEndpoint.

@Test
public void testGetGatewayEndpoint() throws Exception {
    System.setProperty("carbon.home", APIUtilTest.class.getResource("/").getFile());
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        Environment environment = new Environment();
        environment.setType("Production");
        environment.setName("Production");
        environment.setApiGatewayEndpoint("http://localhost:8280,https://localhost:8243");
        Map<String, Environment> environmentMap = new HashMap<String, Environment>();
        environmentMap.put("Production", environment);
        ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
        PowerMockito.mockStatic(ServiceReferenceHolder.class);
        APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
        APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
        Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
        Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
        Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
        Mockito.when(apiManagerConfiguration.getApiGatewayEnvironments()).thenReturn(environmentMap);
        ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
        PowerMockito.mockStatic(ApiMgtDAO.class);
        Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
        Mockito.when(apiMgtDAO.getAllEnvironments(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(new ArrayList<org.wso2.carbon.apimgt.api.model.Environment>());
        String gatewayEndpoint = APIUtil.getGatewayEndpoint("http,https", "Production", "Production", "61416403c40f086ad2dc5eed");
        Assert.assertEquals("https://localhost:8243", gatewayEndpoint);
    } catch (APIManagementException ex) {
        Assert.assertTrue(ex.getMessage().contains("Failed to create API for :"));
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) HashMap(java.util.HashMap) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class APIUtilTest method testCreateSwaggerJSONContent.

@Test
public void testCreateSwaggerJSONContent() throws Exception {
    System.setProperty("carbon.home", APIUtilTest.class.getResource("/").getFile());
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
        APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
        APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
        Environment environment = Mockito.mock(Environment.class);
        Map<String, Environment> environmentMap = new HashMap<String, Environment>();
        environmentMap.put("Production", environment);
        PowerMockito.mockStatic(ServiceReferenceHolder.class);
        Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
        Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
        Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
        Mockito.when(apiManagerConfiguration.getApiGatewayEnvironments()).thenReturn(environmentMap);
        Mockito.when(environment.getApiGatewayEndpoint()).thenReturn("");
        ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
        PowerMockito.mockStatic(ApiMgtDAO.class);
        Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
        Mockito.when(apiMgtDAO.getAllEnvironments(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(new ArrayList<org.wso2.carbon.apimgt.api.model.Environment>());
        String swaggerJSONContent = APIUtil.createSwaggerJSONContent(getUniqueAPI());
        Assert.assertNotNull(swaggerJSONContent);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) HashMap(java.util.HashMap) Environment(org.wso2.carbon.apimgt.api.model.Environment) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class APIConsumerImpl method getHostWithSchemeMappingForEnvironmentWS.

/**
 * Get host names with transport scheme mapping from Gateway Environments in api-manager.xml or from the tenant
 * custom url config in registry. (For WebSockets)
 *
 * @param apiTenantDomain Tenant domain
 * @param environmentName Environment name
 * @return Host name to transport scheme mapping
 * @throws APIManagementException if an error occurs when getting host names with schemes
 */
private Map<String, String> getHostWithSchemeMappingForEnvironmentWS(String apiTenantDomain, String environmentName, String organization) throws APIManagementException {
    Map<String, String> domains = getTenantDomainMappings(apiTenantDomain, APIConstants.API_DOMAIN_MAPPINGS_GATEWAY);
    Map<String, String> hostsWithSchemes = new HashMap<>();
    if (!domains.isEmpty()) {
        String customUrl = domains.get(APIConstants.CUSTOM_URL);
        if (customUrl.startsWith(APIConstants.WS_PROTOCOL_URL_PREFIX)) {
            hostsWithSchemes.put(APIConstants.WS_PROTOCOL, customUrl);
        } else {
            hostsWithSchemes.put(APIConstants.WSS_PROTOCOL, customUrl);
        }
    } else {
        Map<String, Environment> allEnvironments = APIUtil.getEnvironments(organization);
        Environment environment = allEnvironments.get(environmentName);
        if (environment == null) {
            handleResourceNotFoundException("Could not find provided environment '" + environmentName + "'");
        }
        assert environment != null;
        String[] hostsWithScheme = environment.getWebsocketGatewayEndpoint().split(",");
        for (String url : hostsWithScheme) {
            if (url.startsWith(APIConstants.WSS_PROTOCOL_URL_PREFIX)) {
                hostsWithSchemes.put(APIConstants.WSS_PROTOCOL, url);
            }
            if (url.startsWith(APIConstants.WS_PROTOCOL_URL_PREFIX)) {
                hostsWithSchemes.put(APIConstants.WS_PROTOCOL, url);
            }
        }
    }
    return hostsWithSchemes;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Environment(org.wso2.carbon.apimgt.api.model.Environment) RecommendationEnvironment(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment)

Example 34 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class APIConsumerImpl method getOpenAPIDefinitionForDeployment.

/**
 * Get server URL updated Open API definition for given synapse gateway environment
 * @param environmentName Name of the synapse gateway environment
 * @return Updated Open API definition
 * @throws APIManagementException
 */
private String getOpenAPIDefinitionForDeployment(API api, String environmentName) throws APIManagementException {
    String apiTenantDomain;
    String updatedDefinition = null;
    Map<String, String> hostsWithSchemes;
    String definition;
    if (api.getSwaggerDefinition() != null) {
        definition = api.getSwaggerDefinition();
    } else {
        throw new APIManagementException("Missing API definition in the api " + api.getUuid());
    }
    APIDefinition oasParser = OASParserUtil.getOASParser(definition);
    api.setScopes(oasParser.getScopes(definition));
    api.setUriTemplates(oasParser.getURITemplates(definition));
    apiTenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(api.getId().getProviderName()));
    hostsWithSchemes = getHostWithSchemeMappingForEnvironment(api, apiTenantDomain, environmentName);
    api.setContext(getBasePath(apiTenantDomain, api.getContext()));
    updatedDefinition = oasParser.getOASDefinitionForStore(api, definition, hostsWithSchemes);
    return updatedDefinition;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition)

Example 35 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class APIAdminImpl method updateEnvironment.

@Override
public Environment updateEnvironment(String tenantDomain, Environment environment) throws APIManagementException {
    // check if the VHost exists in the tenant domain with given UUID, throw error if not found
    Environment existingEnv = getEnvironment(tenantDomain, environment.getUuid());
    if (existingEnv.isReadOnly()) {
        String errorMessage = String.format("Failed to update Environment with UUID '%s'. Environment is read only", environment.getUuid());
        throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT, String.format("UUID '%s'", environment.getUuid())));
    }
    if (!existingEnv.getName().equals(environment.getName())) {
        String errorMessage = String.format("Failed to update Environment with UUID '%s'. Environment name " + "can not be changed", environment.getUuid());
        throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT_NAME));
    }
    validateForUniqueVhostNames(environment);
    environment.setId(existingEnv.getId());
    return apiMgtDAO.updateEnvironment(environment);
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Aggregations

Environment (org.wso2.carbon.apimgt.api.model.Environment)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 ArrayList (java.util.ArrayList)38 HashMap (java.util.HashMap)28 API (org.wso2.carbon.apimgt.api.model.API)22 IOException (java.io.IOException)21 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)19 Map (java.util.Map)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)12 JsonObject (com.google.gson.JsonObject)11 Gson (com.google.gson.Gson)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 HashSet (java.util.HashSet)10 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)10 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)10 JSONObject (org.json.simple.JSONObject)9 Test (org.junit.Test)9 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)9