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();
}
}
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();
}
}
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;
}
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;
}
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);
}
Aggregations