use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiSandboxEndpoint.
@Test(description = "Test add api with sandbox endpoint")
public void testAddApiSandboxEndpoint() throws APIManagementException, LifecycleException {
Map<String, Endpoint> endpointMap = new HashMap<>();
Map<String, Endpoint> resourceEndpointMap = new HashMap<>();
Map<String, UriTemplate> uriTemplateMap = new HashMap();
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.endpoint(resourceEndpointMap);
uriTemplateBuilder.templateId("getApisApiIdGet");
uriTemplateBuilder.uriTemplate("/apis/");
uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
uriTemplateBuilder.httpVerb("GET");
uriTemplateMap.put("getApisApiIdGet", uriTemplateBuilder.build());
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(endpointMap).uriTemplates(uriTemplateMap);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
String endpointId = String.valueOf(apiBuilder.getEndpoint().get("sandbox"));
Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
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.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultAPI.
public static API.APIBuilder createDefaultAPI() {
Set<String> transport = new HashSet<>();
transport.add(HTTP);
transport.add(HTTPS);
Set<String> tags = new HashSet<>();
tags.add(TAG_CLIMATE);
Set<Policy> policies = new HashSet<>();
policies.add(goldSubscriptionPolicy);
policies.add(silverSubscriptionPolicy);
policies.add(bronzeSubscriptionPolicy);
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner(NAME_BUSINESS_OWNER_1);
businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_1);
businessInformation.setTechnicalOwner(NAME_TECHNICAL_OWNER_1);
businessInformation.setTechnicalOwnerEmail(EMAIL_TECHNICAL_OWNER_1);
String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
Set<String> visibleRoles = new HashSet<>();
visibleRoles.add("testRple");
List<String> labels = new ArrayList<>();
labels.add("testLabel");
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList(APIMgtConstants.FunctionsConstants.GET, APIMgtConstants.FunctionsConstants.POST, APIMgtConstants.FunctionsConstants.DELETE));
corsConfiguration.setAllowHeaders(Arrays.asList(ALLOWED_HEADER_AUTHORIZATION, ALLOWED_HEADER_CUSTOM));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Arrays.asList("*"));
Map<String, Endpoint> endpointMap = new HashMap<>();
endpointMap.put("TestEndpoint", createMockEndpoint());
API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).wsdlUri("http://localhost:9443/echo?wsdl").isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(unlimitedApiPolicy).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(visibleRoles).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition).workflowStatus(WORKFLOW_STATUS).labels(labels).endpoint(endpointMap);
Map map = new HashMap();
map.put(DEVELOPER_ROLE_ID, 6);
map.put(ADMIN_ROLE_ID, 15);
apiBuilder.permissionMap(map);
return apiBuilder;
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIImportExportTestCase method createApi.
private static API.APIBuilder createApi(String provider, String apiId, String name, String version, String description, Map<String, Endpoint> endpointTypeToIdMap) throws APIManagementException {
Set<String> transport = new HashSet<>();
transport.add("http");
Set<Policy> policies = new HashSet<>();
policies.add(new SubscriptionPolicy("Silver"));
policies.add(new SubscriptionPolicy("Bronze"));
Set<String> tags = new HashSet<>();
tags.add("food");
tags.add("beverage");
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner("John Doe");
businessInformation.setBusinessOwnerEmail("john.doe@annonymous.com");
businessInformation.setTechnicalOwner("Jane Doe");
businessInformation.setBusinessOwnerEmail("jane.doe@annonymous.com");
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList("GET", "POST", "DELETE"));
corsConfiguration.setAllowHeaders(Arrays.asList("Authorization", "X-Custom"));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Collections.singletonList("*"));
API.APIBuilder apiBuilder = new API.APIBuilder(provider, name, version).id(apiId).context(UUID.randomUUID().toString()).description(description).lifeCycleStatus("CREATED").apiDefinition(api1Definition).wsdlUri("http://www.webservicex.net/globalweather.asmx?op=GetWeather?wsdl").isResponseCachingEnabled(true).cacheTimeout(120).isDefaultVersion(true).apiPolicy(new APIPolicy("Gold")).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.RESTRICTED).visibleRoles(new HashSet<>(Arrays.asList("customer", "manager", "employee"))).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy("Adam Doe").lastUpdatedTime(LocalDateTime.now()).endpoint(endpointTypeToIdMap);
apiBuilder.uriTemplates(Collections.emptyMap());
return apiBuilder;
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder 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.core.models.API.APIBuilder 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));
}
Aggregations