Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 464E018B9F for ; Mon, 5 Oct 2015 13:41:58 +0000 (UTC) Received: (qmail 63916 invoked by uid 500); 5 Oct 2015 13:41:58 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 63879 invoked by uid 500); 5 Oct 2015 13:41:58 -0000 Mailing-List: contact dev-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list dev@brooklyn.incubator.apache.org Received: (qmail 63867 invoked by uid 99); 5 Oct 2015 13:41:57 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Oct 2015 13:41:57 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id EB48FC0952 for ; Mon, 5 Oct 2015 13:41:56 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.975 X-Spam-Level: X-Spam-Status: No, score=0.975 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.006, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id h3sP_B4_UsXz for ; Mon, 5 Oct 2015 13:41:49 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with SMTP id 6835142B36 for ; Mon, 5 Oct 2015 13:41:49 +0000 (UTC) Received: (qmail 63850 invoked by uid 99); 5 Oct 2015 13:41:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Oct 2015 13:41:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 8AB43E0441; Mon, 5 Oct 2015 13:41:48 +0000 (UTC) From: neykov To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Allow sensors to be aggregated us... Content-Type: text/plain Message-Id: <20151005134148.8AB43E0441@git1-us-west.apache.org> Date: Mon, 5 Oct 2015 13:41:48 +0000 (UTC) Github user neykov commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/919#discussion_r41144182 --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/reducer/ReducerTest.java --- @@ -0,0 +1,255 @@ +/* + * 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.enricher.stock.reducer; + +import java.util.List; +import java.util.Map; + +import javax.annotation.Nullable; + +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.sensor.AttributeSensor; +import org.apache.brooklyn.api.sensor.EnricherSpec; +import org.apache.brooklyn.core.sensor.Sensors; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.enricher.stock.Enrichers; +import org.apache.brooklyn.enricher.stock.reducer.Reducer; +import org.apache.brooklyn.enricher.stock.reducer.StringStringReducer; +import org.apache.brooklyn.test.Asserts; +import org.apache.brooklyn.test.EntityTestUtils; +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class ReducerTest extends BrooklynAppUnitTestSupport { + + public static final AttributeSensor STR1 = Sensors.newStringSensor("test.str1"); + public static final AttributeSensor STR2 = Sensors.newStringSensor("test.str2"); + public static final AttributeSensor STR3 = Sensors.newStringSensor("test.str3"); + public static final AttributeSensor INT1 = Sensors.newIntegerSensor("test.int1"); + + private TestEntity entity; + + @BeforeMethod(alwaysRun=true) + @Override + public void setUp() throws Exception { + super.setUp(); + entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + } + + @Test + public void testBasicReducer(){ + entity.addEnricher(EnricherSpec.create(StringStringReducer.class).configure( + MutableMap.of( + Reducer.SOURCE_SENSORS, ImmutableList.of(STR1, STR2), + Reducer.PRODUCER, entity, + Reducer.TARGET_SENSOR, STR3, + Reducer.REDUCER_FUNCTION, new Concatenator()) + ) + ); + + EntityTestUtils.assertAttributeEquals(entity, STR3, null); + + entity.sensors().set(STR1, "foo"); + EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + + entity.sensors().set(STR2, "bar"); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foobar"); + } + + @Test + public void testReducingBuilderWithConcatenator() { + entity.addEnricher(Enrichers.builder() + .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + .from(entity) + .computing(new Concatenator()) + .publishing(STR3) + .build() + ); + + EntityTestUtils.assertAttributeEquals(entity, STR3, null); + + entity.sensors().set(STR1, "foo"); + EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + + entity.sensors().set(STR2, "bar"); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foobar"); + } + + @Test + public void testReducingBuilderWithLengthCalculator() { + entity.addEnricher(Enrichers.builder() + .reducing(StringIntegerReducer.class, ImmutableList.of(STR1, STR2)) + .from(entity) + .computing(new LengthCalculator()) + .publishing(INT1) + .build() + ); + + EntityTestUtils.assertAttributeEquals(entity, INT1, null); + + entity.sensors().set(STR1, "foo"); + EntityTestUtils.assertAttributeEqualsContinually(entity, INT1, null); + + entity.sensors().set(STR2, "bar"); + EntityTestUtils.assertAttributeEqualsEventually(entity, INT1, 6); + } + + @Test + public void testReducingBuilderWithJoinerFunction() { + entity.addEnricher(Enrichers.builder() + .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + .from(entity) + .computing("joiner", ImmutableMap.of("separator", "-")) + .publishing(STR3) + .build() + ); + + EntityTestUtils.assertAttributeEquals(entity, STR3, null); + + entity.sensors().set(STR1, "foo"); + EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + + entity.sensors().set(STR2, "bar"); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo-bar"); + } + + @Test + public void testReducingBuilderWithJoinerFunctionWithDefaultParameter() { + entity.addEnricher(Enrichers.builder() + .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + .from(entity) + .computing("joiner") + .publishing(STR3) + .build() + ); + EntityTestUtils.assertAttributeEquals(entity, STR3, null); + + entity.sensors().set(STR1, "foo"); + EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + + entity.sensors().set(STR2, "bar"); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo, bar"); + } + + @Test + public void testReducingBuilderWithJoinerFunctionAndUnusedParameter() { + + entity.addEnricher(Enrichers.builder() + .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + .from(entity) + .computing("joiner", ImmutableMap.of("non.existent.parameter", "-")) + .publishing(STR3) + .build() + ); + EntityTestUtils.assertAttributeEquals(entity, STR3, null); + + entity.sensors().set(STR1, "foo"); + EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + + entity.sensors().set(STR2, "bar"); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "foo, bar"); + } + + @Test + public void testReducingBuilderWithFormatStringFunction() { + + entity.addEnricher(Enrichers.builder() + .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + .from(entity) + .computing("formatString", ImmutableMap.of("format", "hello, %s and %s")) + .publishing(STR3) + .build() + ); + EntityTestUtils.assertAttributeEquals(entity, STR3, null); + + entity.sensors().set(STR1, "foo"); + EntityTestUtils.assertAttributeEqualsContinually(entity, STR3, null); + + entity.sensors().set(STR2, "bar"); + EntityTestUtils.assertAttributeEqualsEventually(entity, STR3, "hello, foo and bar"); + } + + @Test + public void testReducingBuilderWithNamedNonExistentFunction() { + try { + entity.addEnricher(Enrichers.builder() + .reducing(StringStringReducer.class, ImmutableList.of(STR1, STR2)) + .from(entity) + .computing("unknown function name", ImmutableMap.of("separator", "-")) + .publishing(STR3) + .build() + ); + Asserts.fail("Expected exception when adding reducing enricher with unknown named function"); + } catch (Exception e) { + Throwable t = Exceptions.getFirstThrowableOfType(e, IllegalStateException.class); + Assert.assertNotNull(t); + } + } + + private static class Concatenator implements Function, String> { + @Nullable + @Override + public String apply(List values) { + StringBuilder result = new StringBuilder(); + for (String value : values) { + if (value == null) { + return null; + } else { + result.append(value); + } + } + return result.toString(); + } + } + + public static class StringIntegerReducer extends Reducer { + + public StringIntegerReducer() {} + + @Override + protected Function, Integer> createReducerFunction( --- End diff -- Have this as the default implementation in `Reducer`? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---