brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grkvlt <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #220: Added a percentage enricher
Date Fri, 24 Jun 2016 18:25:16 GMT
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68440401
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java
---
    @@ -0,0 +1,275 @@
    +/*
    + * 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;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +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.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothInputNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void currentZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 0d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0d);
    +    }
    +
    +    @Test
    +    public void totalZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 0d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalLessThanCurrentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 25d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void oneHundredPercentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 50d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    @Test
    +    public void negativeCurrent() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void negativeTotal() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothSourceNegative() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalDoubleMaxValue() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, Double.MAX_VALUE);
    +        app.sensors().set(totalSensor, Double.MAX_VALUE);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    --- End diff --
    
    Again, result should be 1.0d


---
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.
---

Mime
View raw message