use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdGet.
/**
* Get API of given ID
*
* @param apiId API ID
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return API of the given ID
* @throws NotFoundException If failed to get the API
*/
@Override
public Response apisApiIdGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
APIDTO apiToReturn = null;
try {
String username = RestApiUtil.getLoggedInUsername(request);
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = apisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
return Response.notModified().build();
}
API api = apiStore.getAPIbyUUID(apiId);
boolean isWSDLExists = apiStore.isWSDLExists(apiId);
apiToReturn = APIMappingUtil.toAPIDTO(api);
if (isWSDLExists) {
String wsdlUri = RestApiConstants.WSDL_URI_TEMPLATE.replace(RestApiConstants.APIID_PARAM, api.getId());
apiToReturn.setWsdlUri(wsdlUri);
}
return Response.ok().entity(apiToReturn).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving API : " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class TestMappingUtilTestCase method testAPItoAPIDTOMappingAndAPIDTOtoAPIMapping.
@Test(description = "APIDTO mapping test case")
void testAPItoAPIDTOMappingAndAPIDTOtoAPIMapping() throws IOException {
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.build();
List<String> userSpecificPermission = new ArrayList<>();
userSpecificPermission.add("userPermissions");
api.setUserSpecificApiPermissions(userSpecificPermission);
// Test mappings from API to APIDTO
APIDTO apidto = MappingUtil.toAPIDto(api);
assertEquals(api.getId(), apidto.getId());
assertEquals(api.getName(), apidto.getName());
assertEquals(api.getVersion(), apidto.getVersion());
assertEquals(api.getContext(), apidto.getContext());
assertEquals(api.getDescription(), apidto.getDescription());
assertEquals((Boolean) api.isDefaultVersion(), apidto.getIsDefaultVersion());
assertEquals(api.getVisibility().name(), apidto.getVisibility().name());
assertEquals(api.isResponseCachingEnabled(), Boolean.parseBoolean(apidto.getResponseCaching()));
assertEquals((Integer) api.getCacheTimeout(), apidto.getCacheTimeout());
assertEquals(api.getVisibleRoles().size(), apidto.getVisibleRoles().size());
assertEquals(api.getProvider(), apidto.getProvider());
assertEquals(api.getApiPermission(), apidto.getPermission());
assertEquals(api.getLifeCycleStatus(), apidto.getLifeCycleStatus());
assertEquals(api.getWorkflowStatus(), apidto.getWorkflowStatus());
assertEquals(MappingUtil.mapSecuritySchemeIntToList(api.getSecurityScheme()), apidto.getSecurityScheme());
assertEquals(api.getTags().size(), apidto.getTags().size());
assertEquals(api.getLabels().size(), apidto.getLabels().size());
assertEquals(api.getTransport().size(), apidto.getTransport().size());
assertEquals(api.getUserSpecificApiPermissions().size(), apidto.getUserPermissionsForApi().size());
assertEquals(api.getPolicies().size(), apidto.getPolicies().size());
assertEquals(api.getBusinessInformation().getBusinessOwner(), apidto.getBusinessInformation().getBusinessOwner());
assertEquals(api.getBusinessInformation().getBusinessOwnerEmail(), apidto.getBusinessInformation().getBusinessOwnerEmail());
assertEquals(api.getBusinessInformation().getTechnicalOwner(), apidto.getBusinessInformation().getTechnicalOwner());
assertEquals(api.getBusinessInformation().getTechnicalOwnerEmail(), apidto.getBusinessInformation().getTechnicalOwnerEmail());
assertEquals((Boolean) api.getCorsConfiguration().isAllowCredentials(), apidto.getCorsConfiguration().getAccessControlAllowCredentials());
assertEquals((Boolean) api.getCorsConfiguration().isEnabled(), apidto.getCorsConfiguration().getCorsConfigurationEnabled());
assertEquals(api.getCorsConfiguration().getAllowHeaders(), apidto.getCorsConfiguration().getAccessControlAllowHeaders());
assertEquals(api.getCorsConfiguration().getAllowMethods(), apidto.getCorsConfiguration().getAccessControlAllowMethods());
assertEquals(api.getCorsConfiguration().getAllowOrigins(), apidto.getCorsConfiguration().getAccessControlAllowOrigins());
assertEquals(api.getEndpoint().get("TestEndpoint").getId(), apidto.getEndpoint().get(0).getKey());
assertEquals(api.getUriTemplates().get("getApisApiIdGet").getTemplateId(), apidto.getOperations().get(0).getId());
assertEquals(api.getUriTemplates().get("getApisApiIdGet").getAuthType(), apidto.getOperations().get(0).getAuthType());
assertEquals(api.getUriTemplates().get("getApisApiIdGet").getHttpVerb(), apidto.getOperations().get(0).getHttpVerb());
assertEquals(api.getUriTemplates().get("getApisApiIdGet").getUriTemplate(), apidto.getOperations().get(0).getUritemplate());
assertEquals(api.getUriTemplates().get("getApisApiIdGet").getPolicy().getPolicyName(), apidto.getOperations().get(0).getPolicy());
assertEquals(api.getApiPolicy().getPolicyName(), apidto.getApiPolicy());
assertEquals(api.getCreatedTime().toString(), apidto.getCreatedTime());
assertEquals(api.getLastUpdatedTime().toString(), apidto.getLastUpdatedTime());
// Test mappings from APIDTO to API
API mappedAPI = MappingUtil.toAPI(apidto).build();
assertEquals(apidto.getId(), mappedAPI.getId());
assertEquals(apidto.getName(), mappedAPI.getName());
assertEquals(apidto.getVersion(), mappedAPI.getVersion());
assertEquals(apidto.getContext(), mappedAPI.getContext());
assertEquals(apidto.getDescription(), mappedAPI.getDescription());
assertEquals(apidto.getIsDefaultVersion(), (Boolean) mappedAPI.isDefaultVersion());
assertEquals(apidto.getVisibility().name(), mappedAPI.getVisibility().name());
assertEquals(Boolean.parseBoolean(apidto.getResponseCaching()), mappedAPI.isResponseCachingEnabled());
assertEquals(apidto.getCacheTimeout(), (Integer) mappedAPI.getCacheTimeout());
assertEquals(apidto.getVisibleRoles().size(), mappedAPI.getVisibleRoles().size());
assertEquals(apidto.getProvider(), mappedAPI.getProvider());
assertEquals(apidto.getPermission(), mappedAPI.getApiPermission());
assertEquals(apidto.getLifeCycleStatus(), mappedAPI.getLifeCycleStatus());
assertEquals(MappingUtil.mapSecuritySchemeListToInt(apidto.getSecurityScheme()), mappedAPI.getSecurityScheme());
// Npt mapped it's intentional
assertNull(mappedAPI.getWorkflowStatus());
assertEquals(apidto.getTags().size(), mappedAPI.getTags().size());
assertEquals(apidto.getLabels().size(), mappedAPI.getLabels().size());
assertEquals(apidto.getTransport().size(), mappedAPI.getTransport().size());
// Npt mapped it's intentional
assertNull(mappedAPI.getUserSpecificApiPermissions());
assertEquals(apidto.getPolicies().size(), mappedAPI.getPolicies().size());
assertEquals(apidto.getBusinessInformation().getBusinessOwner(), mappedAPI.getBusinessInformation().getBusinessOwner());
assertEquals(apidto.getBusinessInformation().getBusinessOwnerEmail(), mappedAPI.getBusinessInformation().getBusinessOwnerEmail());
assertEquals(apidto.getBusinessInformation().getTechnicalOwner(), mappedAPI.getBusinessInformation().getTechnicalOwner());
assertEquals(apidto.getBusinessInformation().getTechnicalOwnerEmail(), mappedAPI.getBusinessInformation().getTechnicalOwnerEmail());
assertEquals(apidto.getCorsConfiguration().getAccessControlAllowCredentials(), (Boolean) mappedAPI.getCorsConfiguration().isAllowCredentials());
assertEquals(apidto.getCorsConfiguration().getCorsConfigurationEnabled(), (Boolean) mappedAPI.getCorsConfiguration().isEnabled());
assertEquals(apidto.getCorsConfiguration().getAccessControlAllowHeaders(), mappedAPI.getCorsConfiguration().getAllowHeaders());
assertEquals(apidto.getCorsConfiguration().getAccessControlAllowMethods(), mappedAPI.getCorsConfiguration().getAllowMethods());
assertEquals(apidto.getCorsConfiguration().getAccessControlAllowOrigins(), mappedAPI.getCorsConfiguration().getAllowOrigins());
assertEquals(apidto.getEndpoint().get(0).getKey(), mappedAPI.getEndpoint().get("TestEndpoint").getId());
assertEquals(apidto.getOperations().get(0).getId(), mappedAPI.getUriTemplates().get("getApisApiIdGet").getTemplateId());
assertEquals(apidto.getOperations().get(0).getAuthType(), mappedAPI.getUriTemplates().get("getApisApiIdGet").getAuthType());
assertEquals(apidto.getOperations().get(0).getHttpVerb(), mappedAPI.getUriTemplates().get("getApisApiIdGet").getHttpVerb());
assertEquals(apidto.getOperations().get(0).getUritemplate(), mappedAPI.getUriTemplates().get("getApisApiIdGet").getUriTemplate());
assertEquals(apidto.getOperations().get(0).getPolicy(), mappedAPI.getUriTemplates().get("getApisApiIdGet").getPolicy().getPolicyName());
assertEquals(apidto.getApiPolicy(), mappedAPI.getApiPolicy().getPolicyName());
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisImportDefinitionPostWSDLFile.
@Test
public void testApisImportDefinitionPostWSDLFile() throws Exception {
printTestMethodName();
File file = new File(getClass().getClassLoader().getResource(WSDL_FILE_LOCATION).getFile());
FileInputStream fis = new FileInputStream(file);
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(WSDL_FILE);
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = powerMockDefaultAPIPublisher();
String apiId = UUID.randomUUID().toString();
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(apiId);
API api = apiBuilder.build();
APIDTO apiDto = MappingUtil.toAPIDto(api);
ObjectMapper objectMapper = new ObjectMapper();
String additionalProperties = objectMapper.writeValueAsString(apiDto);
Mockito.doReturn(api).doThrow(new IllegalArgumentException()).when(apiPublisher).getAPIbyUUID(apiId);
Mockito.doReturn(apiId).when(apiPublisher).addAPIFromWSDLFile(Mockito.argThat(getAPIBuilderMatcher(apiBuilder)), Mockito.eq(fis), Mockito.eq(false));
Response response = apisApiService.apisImportDefinitionPost(WSDL, fis, fileInfo, null, additionalProperties, null, null, null, getRequest());
fis.close();
assertEquals(response.getStatus(), 201);
assertTrue(response.getEntity().toString().contains(apiId));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisPost.
@Test
public void testApisPost() throws Exception {
printTestMethodName();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String apiId = UUID.randomUUID().toString();
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.id(apiId).build();
APIDTO apidto = MappingUtil.toAPIDto(api);
Mockito.doReturn(apiId).doThrow(new IllegalArgumentException()).when(apiPublisher).addAPI(apiBuilder);
Mockito.doReturn(api).doThrow(new IllegalArgumentException()).when(apiPublisher).getAPIbyUUID(apiId);
Response response = apisApiService.apisPost(apidto, getRequest());
assertEquals(response.getStatus(), 201);
assertTrue(response.getEntity().toString().contains(api.getId()));
assertTrue(response.getEntity().toString().contains(api.getName()));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class MappingUtil method toAPI.
/**
* This method converts the API model object from the DTO object.
*
* @param apidto APIDTO object with API data
* @return APIBuilder object
*/
public static API.APIBuilder toAPI(APIDTO apidto) throws JsonProcessingException {
BusinessInformation businessInformation = new BusinessInformation();
API_businessInformationDTO apiBusinessInformationDTO = apidto.getBusinessInformation();
if (apiBusinessInformationDTO != null) {
businessInformation.setBusinessOwner(apiBusinessInformationDTO.getBusinessOwner());
businessInformation.setBusinessOwnerEmail(apiBusinessInformationDTO.getBusinessOwnerEmail());
businessInformation.setTechnicalOwner(apiBusinessInformationDTO.getTechnicalOwner());
businessInformation.setTechnicalOwnerEmail(apiBusinessInformationDTO.getTechnicalOwnerEmail());
}
API_corsConfigurationDTO apiCorsConfigurationDTO = apidto.getCorsConfiguration();
CorsConfiguration corsConfiguration = new CorsConfiguration();
if (apiCorsConfigurationDTO != null) {
corsConfiguration.setAllowCredentials(apiCorsConfigurationDTO.getAccessControlAllowCredentials());
corsConfiguration.setAllowHeaders(apiCorsConfigurationDTO.getAccessControlAllowHeaders());
corsConfiguration.setAllowMethods(apiCorsConfigurationDTO.getAccessControlAllowMethods());
corsConfiguration.setAllowOrigins(apiCorsConfigurationDTO.getAccessControlAllowOrigins());
corsConfiguration.setEnabled(apiCorsConfigurationDTO.getCorsConfigurationEnabled());
}
List<API_operationsDTO> operationList = apidto.getOperations();
Map<String, UriTemplate> uriTemplateList = new HashMap<>();
for (API_operationsDTO operationsDTO : operationList) {
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.uriTemplate(operationsDTO.getUritemplate());
uriTemplateBuilder.authType(operationsDTO.getAuthType());
uriTemplateBuilder.httpVerb(operationsDTO.getHttpVerb());
uriTemplateBuilder.policy(new APIPolicy(operationsDTO.getPolicy()));
uriTemplateBuilder.scopes(operationsDTO.getScopes());
if (operationsDTO.getEndpoint() != null && !operationsDTO.getEndpoint().isEmpty()) {
uriTemplateBuilder.endpoint(fromEndpointListToMap(operationsDTO.getEndpoint()));
}
if (operationsDTO.getId() != null) {
uriTemplateBuilder.templateId(operationsDTO.getId());
} else {
uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(operationsDTO.getUritemplate(), operationsDTO.getHttpVerb()));
}
uriTemplateList.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
}
Set<Policy> subscriptionPolicies = new HashSet<>();
apidto.getPolicies().forEach(v -> subscriptionPolicies.add(new SubscriptionPolicy(v)));
API.APIBuilder apiBuilder = new API.APIBuilder(apidto.getProvider(), apidto.getName(), apidto.getVersion()).id(apidto.getId()).context(apidto.getContext()).description(apidto.getDescription()).lifeCycleStatus(apidto.getLifeCycleStatus()).endpoint(fromEndpointListToMap(apidto.getEndpoint())).visibleRoles(new HashSet<>(apidto.getVisibleRoles())).policies(subscriptionPolicies).apiPermission(apidto.getPermission()).tags(new HashSet<>(apidto.getTags())).labels(new ArrayList<>(apidto.getLabels())).transport(new HashSet<>(apidto.getTransport())).isResponseCachingEnabled(Boolean.valueOf(apidto.getResponseCaching())).businessInformation(businessInformation).uriTemplates(uriTemplateList).corsConfiguration(corsConfiguration).wsdlUri(apidto.getWsdlUri()).scopes(apidto.getScopes()).securityScheme(mapSecuritySchemeListToInt(apidto.getSecurityScheme()));
if (apidto.getIsDefaultVersion() != null) {
apiBuilder.isDefaultVersion(apidto.getIsDefaultVersion());
}
if (apidto.getVisibility() != null) {
apiBuilder.visibility(API.Visibility.valueOf(apidto.getVisibility().toString()));
}
if (apidto.getCacheTimeout() != null) {
apiBuilder.cacheTimeout(apidto.getCacheTimeout());
}
if (apidto.getApiPolicy() != null) {
Policy policy = new APIPolicy(apidto.getApiPolicy());
apiBuilder.apiPolicy(policy);
}
if (apidto.getHasOwnGateway() != null) {
apiBuilder.hasOwnGateway(apidto.getHasOwnGateway());
}
if (apidto.getThreatProtectionPolicies() != null) {
API_threatProtectionPoliciesDTO threatProtectionPoliciesDTO = apidto.getThreatProtectionPolicies();
List<API_threatProtectionPolicies_listDTO> threatProtectionPolicies_listDTO = threatProtectionPoliciesDTO.getList();
Set<String> policyIdSet = new HashSet<>();
for (API_threatProtectionPolicies_listDTO listDTO : threatProtectionPolicies_listDTO) {
policyIdSet.add(listDTO.getPolicyId());
}
apiBuilder.threatProtectionPolicies(policyIdSet);
}
return apiBuilder;
}
Aggregations