SensorResource extends AbstractBrooklynRestResource implements SensorApi { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void set(String application, String entityToken, String sensorName, Object newValue) { final 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'", Entitlements.getEntitlementContext().user(), entity); } AttributeSensor sensor = findSensor(entity, sensorName); if (log.isDebugEnabled()) log.debug("REST user "+Entitlements.getEntitlementContext()+" setting sensor "+sensorName+" to "+newValue); entity.sensors().set(sensor, newValue); }  @Override List<SensorSummary> list(final String application, final String entityToken); @Override Map<String, Object> batchSensorRead(final String application, final String entityToken, final Boolean raw); @Override String getPlain(String application, String entityToken, String sensorName, final Boolean raw); @Override Object get(final String application, final String entityToken, String sensorName, final Boolean raw); @SuppressWarnings({ "rawtypes", "unchecked" }) @Override void setFromMap(String application, String entityToken, Map newValues); @SuppressWarnings({ "rawtypes", "unchecked" }) @Override void set(String application, String entityToken, String sensorName, Object newValue); @Override void delete(String application, String entityToken, String sensorName);  }
@Test public void testSet() throws Exception { try { Response response = client().path(SENSORS_ENDPOINT + "/" + SENSOR_NAME) .type(MediaType.APPLICATION_JSON_TYPE) .post(67890); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); assertEquals(entity.getAttribute(SENSOR), (Integer)67890); String value = client().path(SENSORS_ENDPOINT + "/" + SENSOR_NAME).accept(MediaType.TEXT_PLAIN_TYPE).get(String.class); assertEquals(value, "67890 frogs"); } finally { addAmphibianSensor(entity); } }
@Test public void testGetSensorValueOfTypeManagementContext() throws Exception { entity.sensors().set(Sensors.newSensor(ManagementContext.class, "myMgmt"), getManagementContext()); doGetSensorTest("myMgmt", Map.class, ImmutableMap.of("type", ManagementContext.class.getName())); }
@Test public void testGetSensorValueOfTypeEntity() throws Exception { entity.sensors().set(Sensors.newSensor(Entity.class, "myEntity"), entity); doGetSensorTest("myEntity", Map.class, ImmutableMap.of("type", Entity.class.getName(), "id", entity.getId()), true); doGetSensorTest("myEntity", String.class, "\"simple-ent\"", false); }
@Test public void testGetSensorValueOfTypeLocation() throws Exception { Location loc = getManagementContext().getLocationRegistry().getLocationManaged("localhost"); entity.sensors().set(Sensors.newSensor(Location.class, "myLocation"), loc); doGetSensorTest("myLocation", Map.class, ImmutableMap.of("type", Location.class.getName(), "id", loc.getId())); }
@Test public void testGetSensorValueOfTypePolicy() throws Exception { Policy policy = entity.policies().add(org.apache.brooklyn.api.policy.PolicySpec.create(TestPolicy.class)); entity.sensors().set(Sensors.newSensor(Policy.class, "myPolicy"), policy); doGetSensorTest("myPolicy", Map.class, ImmutableMap.of("type", Policy.class.getName(), "id", policy.getId(), "entityId", entity.getId())); }
@Test public void testGetSensorValueOfTypeEnricher() throws Exception { Enricher enricher = entity.enrichers().add(org.apache.brooklyn.api.sensor.EnricherSpec.create(TestEnricher.class)); entity.sensors().set(Sensors.newSensor(Enricher.class, "myEnricher"), enricher); doGetSensorTest("myEnricher", Map.class, ImmutableMap.of("type", Enricher.class.getName(), "id", enricher.getId(), "entityId", entity.getId())); }
@Test public void testGetSensorValueOfTypeFeed() throws Exception { Feed feed = entity.feeds().add(FunctionFeed.builder().entity(entity).build()); entity.sensors().set(Sensors.newSensor(Feed.class, "myFeed"), feed); doGetSensorTest("myFeed", Map.class, ImmutableMap.of("type", Feed.class.getName(), "id", feed.getId(), "entityId", entity.getId())); }
@Test public void testGetSensorValueOfTypeInprogressTask() throws Exception { Task<Void> task = Tasks.<Void>builder().displayName("my-task-name").body(new SleepingCallable(Duration.THIRTY_SECONDS)).build(); entity.getExecutionContext().submit(task); entity.sensors().set(Sensors.newSensor(Task.class, "myTask"), task); doGetSensorTest("myTask", Map.class, ImmutableMap.of("type", Task.class.getName(), "id", task.getId(), "displayName", "my-task-name")); }
@Test public void testGetSensorValueOfTypeEffectorTask() throws Exception { TestEntity testEntity = entity.addChild(org.apache.brooklyn.api.entity.EntitySpec.create(TestEntity.class)); Task<Void> task = testEntity.invoke(TestEntity.SLEEP_EFFECTOR, ImmutableMap.of("duration", Duration.THIRTY_SECONDS)); entity.sensors().set(Sensors.newSensor(Task.class, "myTask"), task); doGetSensorTest("myTask", Map.class, ImmutableMap.of("type", Task.class.getName(), "id", task.getId(), "displayName", "sleepEffector")); }