diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2019-02-22 09:51:58 +0000 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-02-22 16:22:56 +0000 |
commit | 6fbff11fd9d6bcf0319aeb8951bcd9c4e80be8d9 (patch) | |
tree | 070bc16f1690488ffe993118048ca47e7d77ec38 /appc-metric/appc-metric-bundle/src | |
parent | a90eb56177c2e832e71f3329a624583d4ffb36b4 (diff) |
Error in ScheduledPublishingPolicyImpl
Fix for error in code that causes failure in a
particular case
Issue-ID: APPC-1479
Change-Id: If9f8dc21d151ea3e06939ad4db4272f48a8e8b58
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-metric/appc-metric-bundle/src')
2 files changed, 57 insertions, 38 deletions
diff --git a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImpl.java b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImpl.java index 593c9ffa0..4cbc45b2a 100644 --- a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImpl.java +++ b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImpl.java @@ -51,6 +51,7 @@ public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy private Metric[] metrics; private MetricRegistry metricRegistry; private static final EELFLogger logger = EELFManager.getInstance().getLogger(ScheduledPublishingPolicyImpl.class); + private static final int DEFAULT_PERIOD = 100000; private ScheduledExecutorService scheduleExecutor; private Configuration configuration; @@ -64,34 +65,34 @@ public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy public ScheduledPublishingPolicyImpl( Publisher[] publishers, Metric[] metrics) { configuration = ConfigurationFactory.getConfiguration(); - Properties properties=configuration.getProperties(); - if(properties!=null){ - if(properties.getProperty("schedule.policy.metric.period")!=null && properties.getProperty("schedule.policy.metric.start.time")!=null){ + Properties properties = configuration.getProperties(); + if(properties != null){ + if(properties.getProperty("schedule.policy.metric.period") != null && properties.getProperty("schedule.policy.metric.start.time") != null) { this.startTime = getConfigStartTime(properties); this.period = getConfigPeriod(properties); - logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period); - }else if(properties.getProperty("schedule.policy.metric.period")!=null){ - this.startTime=1; - this.period=getConfigPeriod(properties); - logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period); + logger.info("Metric Properties read from configuration Start Time :" + this.startTime+", Period :" + this.period); + }else if(properties.getProperty("schedule.policy.metric.period") != null){ + this.startTime = 1; + this.period = getConfigPeriod(properties); + logger.info("Metric Properties read from configuration Start Time :" + this.startTime+", Period :" + this.period); - }else if(properties.getProperty("schedule.policy.metric.period")==null && properties.getProperty("schedule.policy.metric.start.time")!=null){ - this.startTime=getConfigStartTime("00:00:00",properties); - this.period=(24*60*60*1000l)-1; - logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period); + }else if(properties.getProperty("schedule.policy.metric.period") == null && properties.getProperty("schedule.policy.metric.start.time") != null) { + this.startTime = getConfigStartTime("00:00:00", properties); + this.period = (24 * 60 * 60 * 1000l) -1; + logger.info("Metric Properties read from configuration Start Time :" + this.startTime+", Period :" + this.period); }else{ logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms"); this.startTime = 1; - this.period = 100000; - logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period); + this.period = DEFAULT_PERIOD; + logger.info("Metric Properties read from configuration Start Time :" + this.startTime+", Period :" + this.period); } } else { logger.info("Metric Properties coming as null,setting to default Start Time :1 ms,Period : 100000 ms"); this.startTime = 1; - this.period = 100000; - logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :"+this.period); + this.period = DEFAULT_PERIOD; + logger.info("Metric Properties read from configuration Start Time :"+this.startTime+", Period :" + this.period); } this.publishers = publishers; this.metrics = metrics; @@ -99,20 +100,24 @@ public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy } private long getConfigPeriod(Properties properties) { - String period=properties.getProperty("schedule.policy.metric.period"); - logger.info("Metric period : " +period); - long periodInMs=Integer.parseInt(period)*1000l; - logger.info("Metric period in long : " +periodInMs); - return periodInMs; + String period = properties.getProperty("schedule.policy.metric.period"); + // Check for when this is called as part of the case when metric period property is not set + if (period != null) { + logger.info("Metric period : " + period); + long periodInMs = Integer.parseInt(period) * 1000l; + logger.info("Metric period in long : " + periodInMs); + return periodInMs; + } + return DEFAULT_PERIOD; } private long getTimeInMs(String time) { String[] strings=time.split(":"); - if(strings.length==3) { + if(strings.length == 3) { long hour = Integer.parseInt(strings[0]) * 60 * 60 * 1000l; long min = Integer.parseInt(strings[1]) * 60 * 1000l; long sec = Integer.parseInt(strings[2]) * 1000l; - return hour+min+sec; + return hour + min + sec; }else{ return 0; } @@ -122,27 +127,30 @@ public class ScheduledPublishingPolicyImpl implements ScheduledPublishingPolicy private long getConfigStartTime(Properties properties) { - String startTime=properties.getProperty("schedule.policy.metric.start.time"); - if(startTime!=null){ - long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date()))); - long period=getConfigPeriod(properties); - if(timeDiff>=0){ + System.out.println(properties); + + String startTime = properties.getProperty("schedule.policy.metric.start.time"); + + if(startTime != null){ + long timeDiff = (getTimeInMs(startTime)) - (getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date()))); + long period = getConfigPeriod(properties); + if(timeDiff >= 0){ return timeDiff; }else{ - return period-((timeDiff*-1)%period); + return period - ((timeDiff * -1) % period); } } return 0; } - private long getConfigStartTime(String startTime,Properties properties) { - if(startTime!=null){ - long timeDiff=(getTimeInMs(startTime))-(getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date()))); - long period=getConfigPeriod(properties); - if(timeDiff>=0){ - return timeDiff%period; + private long getConfigStartTime(String startTime, Properties properties) { + if(startTime != null) { + long timeDiff = (getTimeInMs(startTime)) - (getTimeInMs((new SimpleDateFormat("HH:mm:ss")).format(new Date()))); + long period = getConfigPeriod(properties); + if(timeDiff >= 0){ + return timeDiff % period; }else{ - return period-((timeDiff*-1)%period); + return period-((timeDiff * -1) % period); } } return 0; diff --git a/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImplTest.java b/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImplTest.java index 31c23101d..d33d57184 100644 --- a/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImplTest.java +++ b/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImplTest.java @@ -30,12 +30,10 @@ import org.onap.appc.configuration.Configuration; import org.onap.appc.configuration.ConfigurationFactory; import org.onap.appc.metricservice.Publisher; import org.onap.appc.metricservice.metric.Metric; -import org.onap.appc.metricservice.metric.impl.DmaapRequestCounterMetricImpl; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import com.att.eelf.configuration.EELFLogger; import org.junit.Assert; @RunWith(PowerMockRunner.class) @@ -122,4 +120,17 @@ public class ScheduledPublishingPolicyImplTest { Assert.assertEquals(1, publishingPolicy.getPublishers().length); } + @Test + public void testWithStartTimeOnly() { + Properties properties = new Properties(); + properties.setProperty("schedule.policy.metric.start.time", "1000"); + Mockito.when(configuration.getProperties()).thenReturn(properties); + PolicyBuilderFactoryImpl builderFactory = new PolicyBuilderFactoryImpl(); + ScheduledPolicyBuilderImpl policyBuilder = (ScheduledPolicyBuilderImpl) builderFactory.scheduledPolicyBuilder(); + Metric[] metrics = new Metric[0]; + policyBuilder.withMetrics(metrics); + ScheduledPublishingPolicyImpl publishingPolicy = (ScheduledPublishingPolicyImpl) policyBuilder.build(); + Assert.assertEquals(0, publishingPolicy.metrics().length); + } + } |