use of org.wso2.carbon.apimgt.core.models.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);
}
use of org.wso2.carbon.apimgt.core.models.UriTemplate 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());
}
}
use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.
the class APIPublisherImpl method createUriTemplateList.
private void createUriTemplateList(API.APIBuilder apiBuilder, boolean update) throws APIManagementException {
Map<String, UriTemplate> uriTemplateMap = new HashMap();
if (apiBuilder.getUriTemplates().isEmpty()) {
apiBuilder.uriTemplates(APIUtils.getDefaultUriTemplates());
apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
}
for (UriTemplate uriTemplate : apiBuilder.getUriTemplates().values()) {
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
if (StringUtils.isEmpty(uriTemplateBuilder.getTemplateId())) {
uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(uriTemplate.getUriTemplate(), uriTemplate.getHttpVerb()));
}
Map<String, Endpoint> endpointMap = uriTemplateBuilder.getEndpoint();
validateEndpoints(endpointMap, update);
validateApiPolicy(uriTemplateBuilder.getPolicy());
uriTemplateMap.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
}
apiBuilder.uriTemplates(uriTemplateMap);
}
use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.
the class APIPublisherImpl method getAPIbyUUID.
@Override
public API getAPIbyUUID(String uuid) throws APIManagementException {
API api = null;
try {
api = super.getAPIbyUUID(uuid);
if (api != null) {
api.setUserSpecificApiPermissions(getAPIPermissionsOfLoggedInUser(getUsername(), api));
String permissionString = api.getApiPermission();
if (!StringUtils.isEmpty(permissionString)) {
api.setApiPermission(replaceGroupIdWithName(permissionString));
}
if (!getScopesForApi(uuid).isEmpty()) {
String swagger = getApiSwaggerDefinition(uuid);
List<String> globalScopes = new APIDefinitionFromSwagger20().getGlobalAssignedScopes(swagger);
List<APIResource> apiResourceList = new APIDefinitionFromSwagger20().parseSwaggerAPIResources(new StringBuilder(swagger));
api.setScopes(globalScopes);
for (APIResource apiResource : apiResourceList) {
if (apiResource.getUriTemplate().getScopes().isEmpty()) {
UriTemplate retrievedUriTemplateFromApi = api.getUriTemplates().get(apiResource.getUriTemplate().getTemplateId());
if (retrievedUriTemplateFromApi != null) {
UriTemplate.UriTemplateBuilder uriTemplate = new UriTemplate.UriTemplateBuilder(retrievedUriTemplateFromApi);
uriTemplate.scopes(apiResource.getScope());
api.getUriTemplates().replace(apiResource.getUriTemplate().getTemplateId(), uriTemplate.build());
}
}
}
}
}
} catch (ParseException e) {
String errorMsg = "Error occurred while parsing the permission json string for API " + api.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.JSON_PARSE_ERROR);
}
return api;
}
use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.
the class APIStoreImpl method setUriTemplates.
private void setUriTemplates(CompositeAPI.Builder apiBuilder) {
Map<String, UriTemplate> uriTemplateMap = new HashMap();
if (apiBuilder.getUriTemplates() == null || apiBuilder.getUriTemplates().isEmpty()) {
apiBuilder.uriTemplates(APIUtils.getDefaultUriTemplates());
apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
} else {
for (UriTemplate uriTemplate : apiBuilder.getUriTemplates().values()) {
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
if (StringUtils.isEmpty(uriTemplateBuilder.getTemplateId())) {
uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(uriTemplate.getUriTemplate(), uriTemplate.getHttpVerb()));
}
uriTemplateMap.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
}
apiBuilder.uriTemplates(uriTemplateMap);
}
}
Aggregations