use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.
/**
* generate the swagger from uri templates.
*
* @param api API Object
* @return Generated swagger resources as string.
*/
@Override
public String generateSwaggerFromResources(API.APIBuilder api) {
Swagger swagger = new Swagger();
Info info = new Info();
info.setTitle(api.getName());
info.setDescription(api.getDescription());
Contact contact = new Contact();
if (api.getBusinessInformation() != null) {
BusinessInformation businessInformation = api.getBusinessInformation();
contact.setName(businessInformation.getBusinessOwner());
contact.setEmail(businessInformation.getBusinessOwnerEmail());
}
info.setContact(contact);
info.setVersion(api.getVersion());
swagger.setInfo(info);
addSecuritySchemeToSwaggerDefinition(swagger, api.build());
Map<String, Path> stringPathMap = new HashMap();
for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
String uriTemplateString = uriTemplate.getUriTemplate();
List<Parameter> parameterList = getParameters(uriTemplateString);
if (uriTemplate.getParameters() == null || uriTemplate.getParameters().isEmpty()) {
if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
parameterList.add(getDefaultBodyParameter());
}
} else {
for (URITemplateParam uriTemplateParam : uriTemplate.getParameters()) {
Parameter parameter = getParameterFromURITemplateParam(uriTemplateParam);
parameterList.add(parameter);
}
}
Operation operation = new Operation();
operation.setParameters(parameterList);
operation.setOperationId(uriTemplate.getTemplateId());
// having content types like */* can break swagger definition
if (!StringUtils.isEmpty(uriTemplate.getContentType()) && !uriTemplate.getContentType().contains("*")) {
List<String> consumesList = new ArrayList<>();
consumesList.add(uriTemplate.getContentType());
operation.setConsumes(consumesList);
}
operation.addResponse("200", getDefaultResponse());
if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 2) == 2)) {
log.debug("API security scheme : API Key Scheme ---- Resource Auth Type : Not None");
operation.addSecurity(APIMgtConstants.SWAGGER_APIKEY, null);
}
if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 1) == 1)) {
log.debug("API security scheme : Oauth Scheme ---- Resource Auth Type : Not None");
operation.addSecurity(APIMgtConstants.SWAGGER_OAUTH2, null);
}
if (stringPathMap.containsKey(uriTemplateString)) {
Path path = stringPathMap.get(uriTemplateString);
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
} else {
Path path = new Path();
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
stringPathMap.put(uriTemplateString, path);
}
}
swagger.setPaths(stringPathMap);
swagger.setPaths(stringPathMap);
return Json.pretty(swagger);
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIMWSDLUtils method getUriTemplatesForWSDLOperations.
/**
* Generates URI templates to be assigned to an API from a set of operations extracted from WSDL.
*
* @param operations a Set of {@link WSDLOperation} objects
* @return Map of URI Templates
*/
public static Map<String, UriTemplate> getUriTemplatesForWSDLOperations(Set<WSDLOperation> operations, boolean isHttpBinding) {
Map<String, UriTemplate> uriTemplateMap = new HashMap<>();
// add default "POST /" operation if no http binding methods required or operations are not provided
if (!isHttpBinding || operations == null || operations.isEmpty()) {
if (log.isDebugEnabled()) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Adding the POST / operation. ");
stringBuilder.append("isHttpBinding: ").append(isHttpBinding).append(". ");
stringBuilder.append("operations are null?: ").append(operations == null).append(". ");
if (operations != null) {
stringBuilder.append("operations are empty?: ").append(operations.isEmpty()).append(". ");
}
log.debug(stringBuilder.toString());
}
UriTemplate.UriTemplateBuilder builderForPOSTRootCtx = new UriTemplate.UriTemplateBuilder();
builderForPOSTRootCtx.uriTemplate("/");
builderForPOSTRootCtx.httpVerb("POST");
builderForPOSTRootCtx.policy(APIUtils.getDefaultAPIPolicy());
builderForPOSTRootCtx.templateId(APIUtils.generateOperationIdFromPath("/", "POST"));
uriTemplateMap.put(builderForPOSTRootCtx.getTemplateId(), builderForPOSTRootCtx.build());
return uriTemplateMap;
}
// add URI templates for operations
for (WSDLOperation operation : operations) {
if (log.isDebugEnabled()) {
log.debug("Adding URI template for WSDL operation: " + operation.getVerb() + ", " + operation.getURI());
}
UriTemplate.UriTemplateBuilder builder = new UriTemplate.UriTemplateBuilder();
builder.uriTemplate(operation.getURI().startsWith("/") ? operation.getURI() : "/" + operation.getURI());
builder.httpVerb(operation.getVerb());
builder.policy(APIUtils.getDefaultAPIPolicy());
builder.templateId(APIUtils.generateOperationIdFromPath(builder.getUriTemplate(), operation.getVerb()));
builder.contentType(operation.getContentType());
List<URITemplateParam> uriTemplateParams = getUriTemplatesParamsForWSDLOperationParams(operation.getParameters());
builder.parameters(uriTemplateParams);
uriTemplateMap.put(builder.getTemplateId(), builder.build());
}
return uriTemplateMap;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiWithEmptyTemplateIdAndApiDefinition.
@Test(description = "Test add api with empty templateId and api definition")
public void testAddApiWithEmptyTemplateIdAndApiDefinition() throws APIManagementException, LifecycleException {
Map<String, UriTemplate> uriTemplateMap = new HashMap();
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.endpoint(Collections.emptyMap());
uriTemplateBuilder.templateId("");
uriTemplateBuilder.uriTemplate("/apis/");
uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
uriTemplateBuilder.httpVerb("GET");
uriTemplateMap.put("", uriTemplateBuilder.build());
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().uriTemplates(uriTemplateMap).apiDefinition("");
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
apiPublisher.addAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testResourceProductionAndSandboxEndpoint.
@Test(description = "Test add api with Api Specific Endpoint")
public void testResourceProductionAndSandboxEndpoint() throws APIManagementException, LifecycleException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint").applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build();
Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Endpoint resourceEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("resourceEndpoint").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Endpoint resourceEndpoint1 = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("resourceEndpoint1").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build();
Map<String, Endpoint> endpointMap = new HashMap();
endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint);
endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint);
Map<String, Endpoint> resourceEndpoints = new HashMap();
resourceEndpoints.put(APIMgtConstants.SANDBOX_ENDPOINT, resourceEndpoint);
resourceEndpoints.put(APIMgtConstants.PRODUCTION_ENDPOINT, resourceEndpoint1);
Map<String, UriTemplate> uriTemplateMap = SampleTestObjectCreator.getMockUriTemplates();
uriTemplateMap.forEach((k, v) -> {
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(v);
uriTemplateBuilder.endpoint(resourceEndpoints);
uriTemplateMap.replace(k, uriTemplateBuilder.build());
});
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap).uriTemplates(uriTemplateMap);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO, labelDao);
Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint);
Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(null);
Mockito.when(apiDAO.getEndpointByName(resourceEndpoint.getName())).thenReturn(null);
Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false);
apiPublisher.addAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName());
Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(resourceEndpoint.getName());
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class ResourcesApiServiceImplTestCase method resourcesGetTest.
@Test
public void resourcesGetTest() throws Exception {
APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
PowerMockito.mockStatic(APIManagerFactory.class);
PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
Mockito.when(instance.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
ResourcesApiServiceImpl resourcesApiService = new ResourcesApiServiceImpl();
UriTemplate uriTemplateOne = SampleTestObjectCreator.createUniqueUriTemplate();
UriTemplate uriTemplateTwo = SampleTestObjectCreator.createUniqueUriTemplate();
UriTemplate uriTemplateThree = SampleTestObjectCreator.createUniqueUriTemplate();
List<UriTemplate> uriTemplateList = new ArrayList<>();
uriTemplateList.add(uriTemplateOne);
uriTemplateList.add(uriTemplateTwo);
uriTemplateList.add(uriTemplateThree);
Mockito.when(apiMgtAdminService.getAllResourcesForApi(API_CONTEXT, API_VERSION)).thenReturn(uriTemplateList);
Response response = resourcesApiService.resourcesGet(API_CONTEXT, API_VERSION, null, getRequest());
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(((ResourcesListDTO) response.getEntity()).getList().size(), 3);
}
Aggregations