use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class APIConfigContextTestCase method testAPINameWithNumber.
@Test
public void testAPINameWithNumber() throws APITemplateException {
String apiId = UUID.randomUUID().toString();
API nameWithNumberAPI = new API.APIBuilder("provider", "1111testapi", "1.0.0").id(apiId).context("testcontext").build();
APIConfigContext apiConfigContext = new APIConfigContext(nameWithNumberAPI, "org.test");
apiConfigContext.validate();
String actualServiceName = (String) apiConfigContext.getContext().get("serviceName");
Assert.assertEquals(actualServiceName, "prefix_1111testapi_" + apiId.replaceAll("-", "_"));
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class APIConfigContextTestCase method testWithNullParameters.
@Test(expectedExceptions = APITemplateException.class)
public void testWithNullParameters() throws APITemplateException {
API emptyNamedAPI = new API.APIBuilder(null, null, "1.0.0").context("testcontext").id(UUID.randomUUID().toString()).build();
APIConfigContext apiConfigContext = new APIConfigContext(emptyNamedAPI, "org.test");
apiConfigContext.validate();
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class APIConfigContextTestCase method testWithEmptyParameters.
@Test(expectedExceptions = APITemplateException.class)
public void testWithEmptyParameters() throws APITemplateException {
API emptyNamedAPI = new API.APIBuilder("", "", "").context("testcontext").id(UUID.randomUUID().toString()).build();
APIConfigContext apiConfigContext = new APIConfigContext(emptyNamedAPI, "org.test");
apiConfigContext.validate();
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class APIConfigContextTestCase method testResourceConfigContext.
@Test
public void testResourceConfigContext() {
APIConfigContext apiConfigContext = new APIConfigContext(SampleTestObjectCreator.createDefaultAPI().build(), "org.test");
TemplateBuilderDTO templateBuilderDTO = new TemplateBuilderDTO();
templateBuilderDTO.setTemplateId("t1");
List<TemplateBuilderDTO> templateList = new ArrayList<TemplateBuilderDTO>();
templateList.add(templateBuilderDTO);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(apiConfigContext, templateList);
List<TemplateBuilderDTO> templatesFromContext = (List<TemplateBuilderDTO>) resourceConfigContext.getContext().get("apiResources");
String templateIdFromContext = templatesFromContext.get(0).getTemplateId();
Assert.assertEquals(templateIdFromContext, "t1");
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class APIPublisherImpl method saveSwagger20Definition.
/**
* {@inheritDoc}
*/
@Override
public void saveSwagger20Definition(String apiId, String jsonText) throws APIManagementException {
try {
LocalDateTime localDateTime = LocalDateTime.now();
Map<String, String> oldScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(apiId));
Map<String, String> newScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(jsonText);
Map<String, String> updatedScopes = new HashMap<>(newScopes);
updatedScopes.keySet().retainAll(oldScopes.keySet());
oldScopes.keySet().removeAll(updatedScopes.keySet());
newScopes.keySet().removeAll(updatedScopes.keySet());
for (Map.Entry<String, String> scopeEntry : newScopes.entrySet()) {
getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
}
for (Map.Entry<String, String> scopeEntry : oldScopes.entrySet()) {
getKeyManager().deleteScope(scopeEntry.getKey());
}
for (Map.Entry<String, String> scopeEntry : updatedScopes.entrySet()) {
Scope scope = getKeyManager().retrieveScope(scopeEntry.getKey());
scope.setDescription(scopeEntry.getValue());
getKeyManager().updateScope(scope);
}
API api = getAPIbyUUID(apiId);
Map<String, UriTemplate> oldUriTemplateMap = api.getUriTemplates();
List<APIResource> apiResourceList = apiDefinitionFromSwagger20.parseSwaggerAPIResources(new StringBuilder(jsonText));
Map<String, UriTemplate> updatedUriTemplateMap = new HashMap<>();
for (APIResource apiResource : apiResourceList) {
updatedUriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
}
Map<String, UriTemplate> uriTemplateMapNeedTobeUpdate = APIUtils.getMergedUriTemplates(oldUriTemplateMap, updatedUriTemplateMap);
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.uriTemplates(uriTemplateMapNeedTobeUpdate);
createUriTemplateList(apiBuilder, true);
apiBuilder.updatedBy(getUsername());
apiBuilder.lastUpdatedTime(localDateTime);
api = apiBuilder.build();
GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
String existingGatewayConfig = getApiGatewayConfig(apiId);
String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(existingGatewayConfig, jsonText);
getApiDAO().updateAPI(apiId, api);
getApiDAO().updateApiDefinition(apiId, jsonText, getUsername());
getApiDAO().updateGatewayConfig(apiId, updatedGatewayConfig, getUsername());
} catch (APIMgtDAOException e) {
String errorMsg = "Couldn't update the Swagger Definition";
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations