APIPublisherUtil { public static APIConfig buildApiConfig(ServletContext servletContext, APIResourceConfiguration apiDef) throws UserStoreException { APIConfig apiConfig = new APIConfig(); String name = apiDef.getName(); if (name == null || name.isEmpty()) { if (log.isDebugEnabled()) { log.debug("API Name not set in @SwaggerDefinition Annotation"); } name = servletContext.getServletContextName(); } apiConfig.setName(name); String version = apiDef.getVersion(); if (version == null || version.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'API Version not set in @SwaggerDefinition Annotation'"); } version = API_CONFIG_DEFAULT_VERSION; } apiConfig.setVersion(version); String context = apiDef.getContext(); if (context == null || context.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'API Context not set in @SwaggerDefinition Annotation'"); } context = servletContext.getContextPath(); } apiConfig.setContext(context); String[] tags = apiDef.getTags(); if (tags == null || tags.length == 0) { if (log.isDebugEnabled()) { log.debug("'API tag not set in @SwaggerDefinition Annotation'"); } } else { apiConfig.setTags(tags); } String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); servletContext.setAttribute(PARAM_PROVIDER_TENANT_DOMAIN, tenantDomain); tenantDomain = (tenantDomain != null && !tenantDomain.isEmpty()) ? tenantDomain : MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; apiConfig.setTenantDomain(tenantDomain); String endpoint = servletContext.getInitParameter(PARAM_MANAGED_API_ENDPOINT); if (endpoint == null || endpoint.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'managed-api-endpoint' attribute is not configured"); } String endpointContext = apiDef.getContext(); endpoint = APIPublisherUtil.getApiEndpointUrl(endpointContext); } apiConfig.setEndpoint(endpoint); String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration() .getAdminUserName(); if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { owner = owner + "@" + tenantDomain; } if (owner == null || owner.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'managed-api-owner' attribute is not configured"); } } apiConfig.setOwner(owner); apiConfig.setSecured(false); boolean isDefault = true; String isDefaultParam = servletContext.getInitParameter(PARAM_IS_DEFAULT); if (isDefaultParam != null && !isDefaultParam.isEmpty()) { isDefault = Boolean.parseBoolean(isDefaultParam); } apiConfig.setDefault(isDefault); String transports = servletContext.getInitParameter(PARAM_MANAGED_API_TRANSPORTS); if (transports == null || transports.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'managed-api-transports' attribute is not configured. Therefore using the default, " + "which is 'https'"); } transports = "https,http"; } apiConfig.setTransports(transports); String sharingValueParam = servletContext.getInitParameter(PARAM_SHARED_WITH_ALL_TENANTS); boolean isSharedWithAllTenants = Boolean.parseBoolean(sharingValueParam); if (isSharedWithAllTenants && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { isSharedWithAllTenants = false; } apiConfig.setSharedWithAllTenants(isSharedWithAllTenants); Set<ApiUriTemplate> uriTemplates = new LinkedHashSet<>(); for (APIResource apiResource : apiDef.getResources()) { ApiUriTemplate template = new ApiUriTemplate(); template.setAuthType(apiResource.getAuthType()); template.setHttpVerb(apiResource.getHttpVerb()); template.setResourceURI(apiResource.getUri()); template.setUriTemplate(apiResource.getUriTemplate()); template.setScope(apiResource.getScope()); uriTemplates.add(template); } apiConfig.setUriTemplates(uriTemplates); Map<String, ApiScope> apiScopes = new HashMap<>(); if (uriTemplates != null) { for (ApiUriTemplate template : uriTemplates) { ApiScope scope = template.getScope(); if (scope != null) { if (apiScopes.get(scope.getKey()) == null) { apiScopes.put(scope.getKey(), scope); } } } Set<ApiScope> scopes = new HashSet<>(apiScopes.values()); apiConfig.setScopes(scopes); } String policy = servletContext.getInitParameter(PARAM_MANAGED_API_POLICY); if (policy == null || policy.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'managed-api-policy' attribute is not configured. Therefore using the default, " + "which is 'null'"); } policy = null; } apiConfig.setPolicy(policy); return apiConfig; }  static String getServerBaseUrl(); static String getApiEndpointUrl(String context); static APIConfig buildApiConfig(ServletContext servletContext, APIResourceConfiguration apiDef); static String getSwaggerDefinition(APIConfig apiConfig); static void setResourceAuthTypes(ServletContext servletContext, APIConfig apiConfig); static final String API_VERSION_PARAM; }
@Test(description = "test buildAPIConfig method and ensures an APIConfig is created") public void buildApiConfigTest() throws UserStoreException, RegistryException { try { startTenantFlowAsTestTenant(); ServletContext servletContext = new MockServletContext(); APIResourceConfiguration apiDef = new APIResourceConfiguration(); List<APIResource> resources = new ArrayList<>(); apiDef.setResources(resources); APIConfig apiConfig = buildApiConfig(servletContext, apiDef); Assert.assertNotNull(apiConfig, "API configuration is null."); } finally { PrivilegedCarbonContext.endTenantFlow(); } }
@Test(description = "test buildAPIConfig method as SuperTenant and ensures" + " an APIConfig is created") public void buildApiConfigAsSuperTenant() throws UserStoreException { ServletContext servletContext = new MockServletContext(); APIResourceConfiguration apiDef = new APIResourceConfiguration(); List<APIResource> resources = new ArrayList<>(); apiDef.setResources(resources); APIConfig apiConfig = buildApiConfig(servletContext, apiDef); Assert.assertNotNull(apiConfig, "API configuration is null."); }
@Test(description = "test buildAPIConfig with API tags specified and ensures " + "an APIConfig is created") public void buildApiConfigTestWithTags() throws UserStoreException { ServletContext servletContext = new MockServletContext(); APIResourceConfiguration apiDef = new APIResourceConfiguration(); List<APIResource> resources = new ArrayList<>(); APIResource apiResource = new APIResource(); resources.add(apiResource); apiDef.setResources(resources); apiDef.setTags(new String[]{"windows", "device_management"}); APIConfig apiConfig = buildApiConfig(servletContext, apiDef); Assert.assertNotNull(apiConfig, "API configuration is null."); }
@Test(description = "test buildAPIConfig method with API scopes specified and " + "ensures an APIConfig is created") public void buildApiConfigTestWithScope() throws UserStoreException, APIManagerPublisherException { ServletContext servletContext = new MockServletContext(); APIResourceConfiguration apiDef = new APIResourceConfiguration(); List<APIResource> resources = new ArrayList<>(); APIResource apiResource = new APIResource(); ApiScope apiScope = new ApiScope(); apiScope.setDescription("testing"); apiResource.setScope(apiScope); resources.add(apiResource); apiDef.setResources(resources); apiDef.setTags(new String[]{"windows", "device_management"}); APIConfig apiConfig = buildApiConfig(servletContext, apiDef); Assert.assertNotNull(apiConfig, "API configuration is null."); }