add adjuncts rest test, and minor fixes
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/d5d72cb9 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/d5d72cb9 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/d5d72cb9 Branch: refs/heads/master Commit: d5d72cb9d98430112b93559a7f51d6c5b3e6a62f Parents: 28dbb83 Author: Alex Heneveld <[email protected]> Authored: Tue Oct 3 13:18:20 2017 +0100 Committer: Alex Heneveld <[email protected]> Committed: Tue Oct 3 13:18:20 2017 +0100 ---------------------------------------------------------------------- .../apache/brooklyn/rest/api/AdjunctApi.java | 2 +- .../rest/resources/AdjunctResource.java | 7 +- .../brooklyn/rest/resources/PolicyResource.java | 1 - .../rest/resources/AdjunctResourceTest.java | 198 +++++++++++++++++++ 4 files changed, 202 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d5d72cb9/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/AdjunctApi.java ---------------------------------------------------------------------- diff --git a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/AdjunctApi.java b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/AdjunctApi.java index da5c1bf..ee43441 100644 --- a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/AdjunctApi.java +++ b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/AdjunctApi.java @@ -72,7 +72,7 @@ public interface AdjunctApi { @ApiResponse(code = 404, message = "Could not find application or entity"), @ApiResponse(code = 400, message = "Type is not a suitable adjunct") }) - public AdjunctSummary addAdjunct( + public AdjunctDetail addAdjunct( @ApiParam(name = "application", value = "Application ID or name", required = true) @PathParam("application") String application, http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d5d72cb9/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/AdjunctResource.java ---------------------------------------------------------------------- diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/AdjunctResource.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/AdjunctResource.java index 6090e6d..1111193 100644 --- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/AdjunctResource.java +++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/AdjunctResource.java @@ -106,7 +106,7 @@ public class AdjunctResource extends AbstractBrooklynRestResource implements Adj // TODO would like to make 'config' arg optional but jersey complains if we do @SuppressWarnings({ "rawtypes", "unchecked" }) @Override - public AdjunctSummary addAdjunct(String application, String entityToken, String adjunctTypeName, Map<String, String> config) { + public AdjunctDetail addAdjunct(String application, String entityToken, String adjunctTypeName, Map<String, String> config) { Entity entity = brooklyn().getEntity(application, entityToken); if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.MODIFY_ENTITY, entity)) { throw WebResourceUtils.forbidden("User '%s' is not authorized to modify entity '%s'", @@ -227,7 +227,7 @@ public class AdjunctResource extends AbstractBrooklynRestResource implements Adj EntityAdjunct adjunct = brooklyn().getAdjunct(entity, adjunctToken); List<ConfigSummary> result = Lists.newArrayList(); - for (ConfigKey<?> key : adjunct.config().findKeysPresent(Predicates.alwaysTrue())) { + for (ConfigKey<?> key : adjunct.config().findKeysDeclared(Predicates.alwaysTrue())) { result.add(ConfigTransformer.of(key).on(entity, adjunct).includeLinks(ui.getBaseUriBuilder(), false, true).transform()); } return result; @@ -237,7 +237,6 @@ public class AdjunctResource extends AbstractBrooklynRestResource implements Adj // (and in sensors class) @Override public Map<String, Object> batchConfigRead(String application, String entityToken, String adjunctToken) { - // TODO: add test return EntityTransformer.getConfigValues(brooklyn(), brooklyn().getAdjunct(application, entityToken, adjunctToken) ); } @@ -266,7 +265,7 @@ public class AdjunctResource extends AbstractBrooklynRestResource implements Adj if (cki.isEmpty()) throw WebResourceUtils.notFound("Cannot find config key '%s' in adjunct '%s' of entity '%s'", configKeyName, adjunctToken, entityToken); ConfigKey<?> ck = cki.iterator().next(); - adjunct.config().set((ConfigKey) cki, TypeCoercions.coerce(value, ck.getTypeToken())); + adjunct.config().set((ConfigKey) ck, TypeCoercions.coerce(value, ck.getTypeToken())); return Response.status(Response.Status.OK).build(); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d5d72cb9/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java ---------------------------------------------------------------------- diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java index 125b0b7..924d3fd 100644 --- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java +++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/PolicyResource.java @@ -71,7 +71,6 @@ public class PolicyResource extends AbstractBrooklynRestResource implements Poli // (and in sensors class) @Override public Map<String, Boolean> batchConfigRead( String application, String entityToken) { - // TODO: add test Entity entity = brooklyn().getEntity(application, entityToken); Map<String, Boolean> result = Maps.newLinkedHashMap(); for (Policy p : entity.policies()) { http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d5d72cb9/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/AdjunctResourceTest.java ---------------------------------------------------------------------- diff --git a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/AdjunctResourceTest.java b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/AdjunctResourceTest.java new file mode 100644 index 0000000..2bd9f2d --- /dev/null +++ b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/AdjunctResourceTest.java @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.rest.resources; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.fail; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.apache.brooklyn.api.objs.HighlightTuple; +import org.apache.brooklyn.rest.domain.AdjunctDetail; +import org.apache.brooklyn.rest.domain.AdjunctSummary; +import org.apache.brooklyn.rest.domain.ApplicationSpec; +import org.apache.brooklyn.rest.domain.ConfigSummary; +import org.apache.brooklyn.rest.domain.EntitySpec; +import org.apache.brooklyn.rest.testing.BrooklynRestResourceTest; +import org.apache.brooklyn.rest.testing.mocks.RestMockSimpleEntity; +import org.apache.brooklyn.rest.testing.mocks.RestMockSimplePolicy; +import org.apache.brooklyn.test.Asserts; +import org.apache.brooklyn.util.collections.MutableList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; + +@Test(singleThreaded = true, + // by using a different suite name we disallow interleaving other tests between the methods of this test class, which wrecks the test fixtures + suiteName = "AdjunctResourceTest") +public class AdjunctResourceTest extends BrooklynRestResourceTest { + + private static final Logger log = LoggerFactory.getLogger(AdjunctResourceTest.class); + + private static final String ENDPOINT = "/applications/simple-app/entities/simple-ent/adjuncts/"; + + private final ApplicationSpec simpleSpec = ApplicationSpec.builder().name("simple-app").entities( + ImmutableSet.of(new EntitySpec("simple-ent", RestMockSimpleEntity.class.getName()))).locations( + ImmutableSet.of("localhost")).build(); + + private String policyId; + + @BeforeClass(alwaysRun = true) + public void setUp() throws Exception { + startServer(); + Response aResponse = clientDeploy(simpleSpec); + waitForApplicationToBeRunning(aResponse.getLocation()); + + Response pResponse = client().path(ENDPOINT) + .query("type", RestMockSimplePolicy.class.getCanonicalName()) + .type(MediaType.APPLICATION_JSON_TYPE) + .post(toJsonEntity(ImmutableMap.of())); + + AdjunctDetail response = pResponse.readEntity(AdjunctDetail.class); + assertNotNull(response.getId()); + policyId = response.getId(); + + } + + @Test + public void testListAdjuncts() throws Exception { + Set<AdjunctSummary> adjuncts = client().path(ENDPOINT) + .get(new GenericType<Set<AdjunctSummary>>() {}); + + AdjunctSummary policy = null; + List<AdjunctSummary> others = MutableList.of(); + for (AdjunctSummary adj : adjuncts) { + if (adj.getId().equals(policyId)) { + policy = adj; + } else { + others.add(adj); + } + } + + log.info("Non-policy adjuncts: "+others); + Asserts.assertSize(others, 4); + + assertEquals(policy.getName(), RestMockSimplePolicy.class.getName()); + } + + + @Test + public void testGetDetail() throws Exception { + AdjunctDetail policy = client().path(ENDPOINT+policyId) + .get(AdjunctDetail.class); + + assertEquals(policy.getName(), RestMockSimplePolicy.class.getName()); + } + + @Test + public void testListConfig() throws Exception { + Set<ConfigSummary> config = client().path(ENDPOINT + policyId + "/config") + .get(new GenericType<Set<ConfigSummary>>() {}); + + Set<String> configNames = Sets.newLinkedHashSet(); + for (ConfigSummary conf : config) { + configNames.add(conf.getName()); + } + + assertEquals(configNames, ImmutableSet.of( + RestMockSimplePolicy.SAMPLE_CONFIG.getName(), + RestMockSimplePolicy.INTEGER_CONFIG.getName())); + } + + @Test + public void testGetNonExistentConfigReturns404() throws Exception { + String invalidConfigName = "doesnotexist"; + try { + ConfigSummary summary = client().path(ENDPOINT + policyId + "/config/" + invalidConfigName) + .get(ConfigSummary.class); + fail("Should have thrown 404, but got "+summary); + } catch (Exception e) { + if (!e.toString().contains("404")) throw e; + } + } + + @Test + public void testGetDefaultValue() throws Exception { + String configName = RestMockSimplePolicy.SAMPLE_CONFIG.getName(); + String expectedVal = RestMockSimplePolicy.SAMPLE_CONFIG.getDefaultValue(); + + String configVal = client().path(ENDPOINT + policyId + "/config/" + configName) + .get(String.class); + assertEquals(configVal, expectedVal); + } + + @Test(dependsOnMethods = "testGetDefaultValue") + public void testReconfigureConfig() throws Exception { + String configName = RestMockSimplePolicy.SAMPLE_CONFIG.getName(); + + Response response = client().path(ENDPOINT + policyId + "/config/" + configName) + .post(toJsonEntity("newval")); + + assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + } + + @Test(dependsOnMethods = "testReconfigureConfig") + public void testGetConfigValue() throws Exception { + String configName = RestMockSimplePolicy.SAMPLE_CONFIG.getName(); + String expectedVal = "newval"; + + Map<String, Object> allState = client().path(ENDPOINT + policyId + "/config-current") + .get(new GenericType<Map<String, Object>>() {}); + assertEquals(allState, ImmutableMap.of(configName, expectedVal)); + + String configVal = client().path(ENDPOINT + policyId + "/config/" + configName) + .get(String.class); + assertEquals(configVal, expectedVal); + } + + @Test + public void testHighlights() throws Exception { + Set<AdjunctSummary> policies = client().path(ENDPOINT) + .query("adjunctType", "policy") + .get(new GenericType<Set<AdjunctSummary>>() {}); + + assertEquals(policies.size(), 1); + AdjunctSummary policySummary = policies.iterator().next(); + + Map<String, HighlightTuple> highlights = policySummary.getHighlights(); + + assertEquals(highlights.size(), 2); + HighlightTuple highlightTupleTask = highlights.get("testNameTask"); + assertEquals(highlightTupleTask.getDescription(), "testDescription"); + assertEquals(highlightTupleTask.getTime(), 123L); + assertEquals(highlightTupleTask.getTaskId(), "testTaskId"); + + HighlightTuple highlightTupleNoTask = highlights.get("testNameNoTask"); + assertEquals(highlightTupleNoTask.getDescription(), "testDescription"); + assertEquals(highlightTupleNoTask.getTime(), 123L); + assertEquals(highlightTupleNoTask.getTaskId(), null); + } +}
