Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISPN-11001 Get config templates #7637

Merged
merged 1 commit into from Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -134,6 +134,14 @@ configuration, as in the following example:
include::json_examples/rest_cache_config_response.json[]
----

[id='rest_v2_cache_manager_cache_templates']
= Listing Cache Templates
List all templates available for creating caches with `GET` requests.

[source,options="nowrap",subs=attributes+]
----
GET /rest/v2/cache-managers/{cacheManagerName}/cache-configs/templates
----

[id='rest_v2_cache_manager_cache_list']
= (Experimental) Obtaining Cache Status and Information
Expand Down
Expand Up @@ -84,6 +84,7 @@ public Invocations getInvocations() {

// Config
.invocation().methods(GET).path("/v2/cache-managers/{name}/cache-configs").handleWith(this::getAllCachesConfiguration)
.invocation().methods(GET).path("/v2/cache-managers/{name}/cache-configs/templates").handleWith(this::getAllCachesConfigurationTemplates)
.invocation().methods(POST).path("/v2/cache-managers/{name}/config").withAction("toJSON").handleWith(this::convertToJson)

// Cache Manager config
Expand Down Expand Up @@ -236,13 +237,40 @@ private CompletionStage<RestResponse> getAllCachesConfiguration(RestRequest requ
try {
Set<String> cacheConfigurationNames = cacheManager.getCacheConfigurationNames();

Set<NamedCacheConfiguration> configurations = cacheConfigurationNames.stream()
List<NamedCacheConfiguration> configurations = cacheConfigurationNames.stream()
.filter(n -> !internalCacheRegistry.isInternalCache(n))
.distinct()
.map(n -> {
Configuration cacheConfiguration = cacheManager.getCacheConfiguration(n);
String json = jsonWriter.toJSON(cacheConfiguration);
return new NamedCacheConfiguration(n, json);
}).collect(Collectors.toSet());
})
.sorted(Comparator.comparing(c -> c.name))
.collect(Collectors.toList());

byte[] bytes = objectMapper.writeValueAsBytes(configurations);
responseBuilder.contentType(APPLICATION_JSON).entity(bytes);
} catch (JsonProcessingException e) {
responseBuilder.status(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}

return completedFuture(responseBuilder.build());
}

private CompletionStage<RestResponse> getAllCachesConfigurationTemplates(RestRequest request) {
NettyRestResponse.Builder responseBuilder = checkCacheManager(request);
if (responseBuilder.getHttpStatus() == NOT_FOUND) return completedFuture(responseBuilder.build());

try {
Set<String> cacheConfigurationNames = cacheManager.getCacheConfigurationNames();

List<NamedCacheConfiguration> configurations = cacheConfigurationNames.stream()
.filter(n -> !internalCacheRegistry.isInternalCache(n))
.filter(n -> cacheManager.getCacheConfiguration(n).isTemplate())
.distinct()
.map(n -> new NamedCacheConfiguration(n, jsonWriter.toJSON(cacheManager.getCacheConfiguration(n))))
.sorted(Comparator.comparing(c -> c.name))
.collect(Collectors.toList());

byte[] bytes = objectMapper.writeValueAsBytes(configurations);
responseBuilder.contentType(APPLICATION_JSON).entity(bytes);
Expand Down
Expand Up @@ -41,13 +41,18 @@ public class CacheManagerResourceTest extends AbstractRestResourceTest {
private Configuration cache2Config;
private ObjectMapper mapper = new ObjectMapper();
private JsonWriter jsonWriter = new JsonWriter();
private Configuration templateConfig;

@Override
protected void defineCaches(EmbeddedCacheManager cm) {
cache1Config = getCache1Config();
cache2Config = getCache2Config();
ConfigurationBuilder templateConfigBuilder = new ConfigurationBuilder();
templateConfigBuilder.template(true).clustering().cacheMode(LOCAL).encoding().key().mediaType(TEXT_PLAIN_TYPE);
templateConfig = templateConfigBuilder.build();
cm.defineConfiguration("cache1", cache1Config);
cm.defineConfiguration("cache2", cache2Config);
cm.defineConfiguration("template", templateConfig);
}

private Configuration getCache1Config() {
Expand Down Expand Up @@ -92,13 +97,30 @@ public void testCacheConfigs() throws Exception {
ResponseAssertion.assertThat(response).isOk();

String json = response.getContentAsString();
JsonNode jsonNode = mapper.readTree(json);
Map<String, String> cachesAndConfig = cacheAndConfig((ArrayNode) jsonNode);
ArrayNode jsonNode = (ArrayNode) mapper.readTree(json);
Map<String, String> cachesAndConfig = cacheAndConfig(jsonNode);

assertEquals(cachesAndConfig.get("cache1"), jsonWriter.toJSON(cache1Config));
assertEquals(cachesAndConfig.get("template"), jsonWriter.toJSON(templateConfig));
assertEquals(cachesAndConfig.get("cache2"), jsonWriter.toJSON(cache2Config));
assertEquals(cachesAndConfig.get("cache2"), jsonWriter.toJSON(cache2Config));
}

@Test
public void testCacheConfigsTemplates() throws Exception {
String accept = "text/plain; q=0.9, application/json; q=0.6";
String url = String.format("http://localhost:%d/rest/v2/cache-managers/default/cache-configs/templates", restServer().getPort());
ContentResponse response = client.newRequest(url).header("Accept", accept).send();
ResponseAssertion.assertThat(response).isOk();

String json = response.getContentAsString();
ArrayNode jsonNode = (ArrayNode) mapper.readTree(json);
Map<String, String> cachesAndConfig = cacheAndConfig(jsonNode);

assertEquals(cachesAndConfig.get("template"), jsonWriter.toJSON(templateConfig));
assertFalse(cachesAndConfig.containsKey("cache1"));
assertFalse(cachesAndConfig.containsKey("cache2"));
}

@Test
public void testCaches() throws Exception {
String accept = "text/plain; q=0.9, application/json; q=0.6";
Expand Down