diff options
8 files changed, 188 insertions, 50 deletions
diff --git a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFunctionCounterBuilderImpl.java b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFunctionCounterBuilderImpl.java index f2d00e987..f4c3e1914 100644 --- a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFunctionCounterBuilderImpl.java +++ b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFunctionCounterBuilderImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,28 +38,28 @@ public class DispatchingFunctionCounterBuilderImpl implements DispatchingFunctio @Override public DispatchingFunctionCounterBuilder withName(String name) { - this.name=name; + this.name = name; return this; } @Override public DispatchingFunctionCounterBuilder withAcceptRequestValue(long value) { - this.acceptedRequested=value; + this.acceptedRequested = value; return this; } @Override public DispatchingFunctionCounterBuilder withRejectRequestValue(long value) { - this.rejectedRequest=value; + this.rejectedRequest = value; return this; } @Override public DispatchingFunctionCounterBuilder withType(MetricType type) { - this.metricType=type; + this.metricType = type; return this; } @Override public DispatchingFuntionMetric build() { - return new DispatchingFuntionMetricImpl(this.name,this.metricType,this.acceptedRequested,this.rejectedRequest); + return new DispatchingFuntionMetricImpl(this.name, this.metricType, this.acceptedRequested, this.rejectedRequest); } } diff --git a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFuntionMetricImpl.java b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFuntionMetricImpl.java index 3d9f383ca..b985c6791 100644 --- a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFuntionMetricImpl.java +++ b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DispatchingFuntionMetricImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -78,7 +80,7 @@ public class DispatchingFuntionMetricImpl implements DispatchingFuntionMetric { return value; } catch (Exception e) { - logger.debug("Cant format the date.",e); + logger.debug("Cant format the date.", e); } return null; diff --git a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterBuilderImpl.java b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterBuilderImpl.java index 366ba2409..7507e4251 100644 --- a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterBuilderImpl.java +++ b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterBuilderImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,31 +38,31 @@ public class DmaapRequestCounterBuilderImpl implements DmaapRequestCounterBuilde @Override public DmaapRequestCounterBuilder withName(String name) { - this.name=name; + this.name = name; return this; } @Override public DmaapRequestCounterBuilder withRecievedMessage(long value) { - this.recievedMessage=value; + this.recievedMessage = value; return this; } @Override public DmaapRequestCounterBuilder withPublishedMessage(long value) { - this.publishedMessage=value; + this.publishedMessage = value; return this; } @Override public DmaapRequestCounterBuilder withType(MetricType type) { - this.metricType=type; + this.metricType = type; return this; } @Override public DmaapRequestCounterMetric build() { - return new DmaapRequestCounterMetricImpl(this.name,this.metricType,this.recievedMessage,this.publishedMessage); + return new DmaapRequestCounterMetricImpl(this.name, this.metricType, this.recievedMessage, this.publishedMessage); } } diff --git a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterMetricImpl.java b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterMetricImpl.java index 5243e72ce..c6e2eea3b 100644 --- a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterMetricImpl.java +++ b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/DmaapRequestCounterMetricImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -77,7 +79,7 @@ public class DmaapRequestCounterMetricImpl implements DmaapRequestCounterMetric logger.debug("Current value of the metric " + this.name + " :" + value); return value; } catch (Exception e) { - logger.debug("Cant format the date.",e); + logger.debug("Cant format the date.", e); } return null; } diff --git a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/PrimitiveCounterBuilderImpl.java b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/PrimitiveCounterBuilderImpl.java index bc745ce54..a6491c3e9 100644 --- a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/PrimitiveCounterBuilderImpl.java +++ b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/metric/impl/PrimitiveCounterBuilderImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,19 +37,19 @@ public class PrimitiveCounterBuilderImpl implements PrimitiveCounterBuilder { @Override public PrimitiveCounterBuilder withName(String name) { - this.name=name; + this.name = name; return this; } @Override public PrimitiveCounterBuilder withValue(long value) { - this.counter=value; + this.counter = value; return this; } @Override public PrimitiveCounterBuilder withType(MetricType type) { - this.metricType=type; + this.metricType = type; return this; } 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 0dd76b866..593c9ffa0 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 @@ -5,19 +5,21 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed 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. - * + * * ============LICENSE_END========================================================= */ @@ -35,7 +37,6 @@ import org.onap.appc.configuration.Configuration; import org.onap.appc.configuration.ConfigurationFactory; import org.onap.appc.exceptions.APPCException; import org.onap.appc.metricservice.MetricRegistry; -import org.onap.appc.metricservice.MetricService; import org.onap.appc.metricservice.Publisher; import org.onap.appc.metricservice.metric.Metric; import org.onap.appc.metricservice.policy.ScheduledPublishingPolicy; diff --git a/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/TestMetricServiceImpl.java b/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/TestMetricServiceImpl.java index c6f7fbf48..cdaf4bd1b 100644 --- a/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/TestMetricServiceImpl.java +++ b/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/TestMetricServiceImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,10 +63,10 @@ public class TestMetricServiceImpl { df.decrement(); Assert.assertEquals(1, df.value()); Assert.assertNotNull(df.getLastModified()); - Assert.assertEquals("TEST",df.name()); - Assert.assertEquals(MetricType.COUNTER,df.type()); + Assert.assertEquals("TEST", df.name()); + Assert.assertEquals(MetricType.COUNTER, df.type()); df.reset(); - Assert.assertEquals(0,df.value()); + Assert.assertEquals(0, df.value()); Assert.assertNotNull(df.getMetricsOutput()); } @@ -79,78 +81,78 @@ public class TestMetricServiceImpl { obj.decrement(); Assert.assertEquals(4, obj.value()); Assert.assertNotNull(obj.getLastModified()); - Assert.assertEquals("TEST",obj.name()); - Assert.assertEquals(MetricType.COUNTER,obj.type()); + Assert.assertEquals("TEST", obj.name()); + Assert.assertEquals(MetricType.COUNTER, obj.type()); obj.reset(); - Assert.assertEquals(0,obj.value()); + Assert.assertEquals(0, obj.value()); Assert.assertNotNull(obj.getMetricsOutput()); } @Test public void testDmaapRequestCounterMetricImpl() { - DmaapRequestCounterMetricImpl obj =new DmaapRequestCounterMetricImpl("TEST",MetricType.COUNTER,7,1); + DmaapRequestCounterMetricImpl obj =new DmaapRequestCounterMetricImpl("TEST", MetricType.COUNTER,7,1); String date = getCurrentDate(); obj.incrementPublishedMessage(); obj.incrementRecievedMessage(); - Assert.assertEquals(2,Integer.parseInt(obj.getMetricsOutput().get("Total Published messages"))); - Assert.assertEquals(8,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); - Assert.assertEquals(date+"[8],[2]",obj.value()); + Assert.assertEquals(2, Integer.parseInt(obj.getMetricsOutput().get("Total Published messages"))); + Assert.assertEquals(8, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); + Assert.assertEquals(date + "[8],[2]", obj.value()); Assert.assertNotNull(obj.getLastModified()); - Assert.assertEquals("TEST",obj.name()); - Assert.assertEquals(MetricType.COUNTER,obj.type()); + Assert.assertEquals("TEST", obj.name()); + Assert.assertEquals(MetricType.COUNTER, obj.type()); obj.reset(); - Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Published messages"))); - Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); + Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Published messages"))); + Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); } @Test public void testDispatchingFuntionMetricImpl() { - DispatchingFuntionMetricImpl obj= new DispatchingFuntionMetricImpl("TEST",MetricType.COUNTER,7,1); + DispatchingFuntionMetricImpl obj= new DispatchingFuntionMetricImpl("TEST", MetricType.COUNTER,7,1); String date = getCurrentDate(); obj.incrementAcceptedRequest(); obj.incrementRejectedRequest(); - Assert.assertEquals(10,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); - Assert.assertEquals(2,Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages"))); - Assert.assertEquals(date+"[8,2]@10",obj.value()); + Assert.assertEquals(10, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); + Assert.assertEquals(2, Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages"))); + Assert.assertEquals(date + "[8,2]@10", obj.value()); Assert.assertNotNull(obj.getLastModified()); - Assert.assertEquals("TEST",obj.name()); - Assert.assertEquals(MetricType.COUNTER,obj.type()); + Assert.assertEquals("TEST", obj.name()); + Assert.assertEquals(MetricType.COUNTER, obj.type()); obj.reset(); - Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); - Assert.assertEquals(0,Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages"))); + Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Received messages"))); + Assert.assertEquals(0, Integer.parseInt(obj.getMetricsOutput().get("Total Rejected messages"))); } @Test public void testDispatchingFunctionCounterBuilderImpl(){ - DispatchingFunctionCounterBuilderImpl obj=new DispatchingFunctionCounterBuilderImpl(); + DispatchingFunctionCounterBuilderImpl obj = new DispatchingFunctionCounterBuilderImpl(); String date = getCurrentDate(); - DispatchingFuntionMetric metric=obj.withName("TEST").withType(MetricType.COUNTER).withAcceptRequestValue(7).withRejectRequestValue(2).build(); + DispatchingFuntionMetric metric = obj.withName("TEST").withType(MetricType.COUNTER).withAcceptRequestValue(7).withRejectRequestValue(2).build(); metric.incrementAcceptedRequest(); metric.incrementRejectedRequest(); - Assert.assertEquals(date+"[8,3]@11",metric.value()); + Assert.assertEquals(date+"[8,3]@11", metric.value()); } @Test public void testDmaapRequestCounterBuilderImpl(){ - DmaapRequestCounterBuilderImpl obj=new DmaapRequestCounterBuilderImpl(); - DmaapRequestCounterMetric metric =obj.withName("TEST").withPublishedMessage(1).withRecievedMessage(21).withType(MetricType.COUNTER).build(); + DmaapRequestCounterBuilderImpl obj = new DmaapRequestCounterBuilderImpl(); + DmaapRequestCounterMetric metric = obj.withName("TEST").withPublishedMessage(1).withRecievedMessage(21).withType(MetricType.COUNTER).build(); metric.incrementPublishedMessage(); metric.incrementRecievedMessage(); - Assert.assertEquals(2,Integer.parseInt(metric.getMetricsOutput().get("Total Published messages"))); - Assert.assertEquals(22,Integer.parseInt(metric.getMetricsOutput().get("Total Received messages"))); + Assert.assertEquals(2, Integer.parseInt(metric.getMetricsOutput().get("Total Published messages"))); + Assert.assertEquals(22, Integer.parseInt(metric.getMetricsOutput().get("Total Received messages"))); } @Test public void testPrimitiveCounterBuilderImpl(){ - PrimitiveCounterBuilderImpl obj=new PrimitiveCounterBuilderImpl(); - PrimitiveCounter counter=obj.withName("TEST").withType(MetricType.COUNTER).withValue(1).build(); + PrimitiveCounterBuilderImpl obj = new PrimitiveCounterBuilderImpl(); + PrimitiveCounter counter = obj.withName("TEST").withType(MetricType.COUNTER).withValue(1).build(); counter.increment(); Assert.assertEquals(2, counter.value()); counter.decrement(); 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 new file mode 100644 index 000000000..31c23101d --- /dev/null +++ b/appc-metric/appc-metric-bundle/src/test/java/org/onap/appc/metricservice/policy/impl/ScheduledPublishingPolicyImplTest.java @@ -0,0 +1,125 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2019 Ericsson + * ============================================================================= + * Licensed 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. + * + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.metricservice.policy.impl; + +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +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) +@PrepareForTest(ConfigurationFactory.class) +public class ScheduledPublishingPolicyImplTest { + + private Configuration configuration = Mockito.mock(Configuration.class); + + @Before + public void setup() { + PowerMockito.mockStatic(ConfigurationFactory.class); + PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(configuration); + } + + @Test + public void testWithPeriodAndStartTime() { + Properties properties = new Properties(); + properties.setProperty("schedule.policy.metric.period", "1000"); + 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); + } + + @Test + public void testWithPeriod() { + Properties properties = new Properties(); + properties.setProperty("schedule.policy.metric.period", "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); + } + + @Test + public void testWithNeither() { + Properties properties = new Properties(); + Mockito.when(configuration.getProperties()).thenReturn(properties); + PolicyBuilderFactoryImpl builderFactory = new PolicyBuilderFactoryImpl(); + ScheduledPolicyBuilderImpl policyBuilder = (ScheduledPolicyBuilderImpl) builderFactory.scheduledPolicyBuilder(); + Metric[] metrics = new Metric[0]; + policyBuilder.withMetrics(metrics); + policyBuilder.withPublishers(null); + ScheduledPublishingPolicyImpl publishingPolicy = (ScheduledPublishingPolicyImpl) policyBuilder.build(); + Assert.assertEquals(0, publishingPolicy.metrics().length); + + } + + @Test + public void testWithNullProperties() { + Mockito.when(configuration.getProperties()).thenReturn(null); + PolicyBuilderFactoryImpl builderFactory = new PolicyBuilderFactoryImpl(); + ScheduledPolicyBuilderImpl policyBuilder = (ScheduledPolicyBuilderImpl) builderFactory.scheduledPolicyBuilder(); + Metric[] metrics = new Metric[0]; + policyBuilder.withMetrics(metrics); + policyBuilder.withPeriod(100000); + policyBuilder.withStartTime(1); + ScheduledPublishingPolicyImpl publishingPolicy = (ScheduledPublishingPolicyImpl) policyBuilder.build(); + Assert.assertEquals(0, publishingPolicy.metrics().length); + Assert.assertEquals(100000, publishingPolicy.getPeriod()); + Assert.assertEquals(1, publishingPolicy.getStartTime()); + } + + @Test + public void testConstructorAndInit() { + Properties properties = new Properties(); + properties.setProperty("schedule.policy.metric.period", "1000"); + properties.setProperty("schedule.policy.metric.start.time", "1000"); + properties.setProperty("metric.enabled" , "true"); + Publisher publisherMock = Mockito.mock(Publisher.class); + Publisher[] publisherArray = new Publisher[1]; + publisherArray[0] = publisherMock; + Mockito.when(configuration.getProperties()).thenReturn(properties); + ScheduledPublishingPolicyImpl publishingPolicy = new ScheduledPublishingPolicyImpl(1000, 1000, publisherArray, new Metric[0]); + Whitebox.setInternalState(publishingPolicy, "configuration", configuration); + publishingPolicy.init(); + Assert.assertEquals(1, publishingPolicy.getPublishers().length); + } + +} |