diff options
Diffstat (limited to 'dcae-analytics-tca/src/test/java')
8 files changed, 1002 insertions, 0 deletions
diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAUnitTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAUnitTest.java new file mode 100644 index 0000000..187ffa2 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/BaseAnalyticsTCAUnitTest.java @@ -0,0 +1,157 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Suppliers; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelIOUtils; +import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier; +import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest; + +import java.io.IOException; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * @author Rajiv Singla . Creation Date: 10/25/2016. + */ +public abstract class BaseAnalyticsTCAUnitTest extends BaseDCAEAnalyticsUnitTest { + + /** + * Object mapper to be used for all TCA Json Parsing + */ + protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER = + Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get(); + + protected static final String TCA_POLICY_JSON_FILE_LOCATION = "data/json/policy/tca_policy.json"; + protected static final String CEF_MESSAGES_JSON_FILE_LOCATION = "data/json/cef/cef_messages.json"; + protected static final String CEF_MESSAGE_JSON_FILE_LOCATION = "data/json/cef/cef_message.json"; + protected static final String CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION = + "data/json/cef/cef_message_with_threshold_violation.json"; + + protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION = + "data/properties/tca_controller_policy.properties"; + + protected static final String TCA_TEST_APP_CONFIG_NAME = "testTCAAppName"; + protected static final String TCA_TEST_APP_CONFIG_DESCRIPTION = "testTCAAppDescription"; + protected static final String TCA_TEST_APP_CONFIG_SUBSCRIBER_OUTPUT_STREAM_NAME = + "testTcaSubscriberOutputStreamName"; + protected static final String TCA_TEST_APP_CONFIG_VES_ALERT_TABLE_NAME = "testTcaVESAlertsTableName"; + protected static final String TCA_TEST_APP_CONFIG_VES_MESSAGE_STATUS_TABLE_NAME = + "testTcaVESMessageStatusTableName"; + + + /** + * Provides TCA Policy that can be used for testing + * + * @return test TCA Policy Object + */ + protected TCAPolicy getSampleTCAPolicy() { + try { + return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(fromStream(TCA_POLICY_JSON_FILE_LOCATION), TCAPolicy.class); + } catch (IOException e) { + LOG.error("Error while parsing policy: {}", e); + throw new RuntimeException("Error while parsing policy", e); + } + } + + /** + * Provides list containing 350 CEF messages + * + * @return CEF Test Message + * @throws Exception Exception + */ + protected List<EventListener> getCEFMessages() throws Exception { + final String cefMessageAsString = fromStream(CEF_MESSAGES_JSON_FILE_LOCATION); + final TypeReference<List<EventListener>> eventListenerListTypeReference = + new TypeReference<List<EventListener>>() { + }; + return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference); + } + + /** + * Provides 1 valid CEF messages which does not violate Threshold as String + * + * @return CEF Test Message String + * @throws Exception Exception + */ + protected String getValidCEFMessage() throws Exception { + return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + } + + + /** + * Provides single CEF Test Message + * + * @return CEF Test Message + * @throws Exception Exception + */ + protected EventListener getCEFEventListener() throws Exception { + final String cefMessageAsString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, EventListener.class); + } + + protected static List<Threshold> getThresholds() { + Threshold majorThreshold = new Threshold(); + majorThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); + majorThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + majorThreshold.setVersion("Test Version"); + majorThreshold.setThresholdValue(500L); + majorThreshold.setDirection(Direction.LESS_OR_EQUAL); + + Threshold criticalThreshold = new Threshold(); + criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); + criticalThreshold.setThresholdValue(5000L); + criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL); + return Arrays.asList(majorThreshold, criticalThreshold); + } + + protected static Threshold getCriticalThreshold() { + Threshold criticalThreshold = new Threshold(); + criticalThreshold.setClosedLoopControlName("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A"); + criticalThreshold.setThresholdValue(5000L); + criticalThreshold.setFieldPath("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + criticalThreshold.setDirection(Direction.GREATER_OR_EQUAL); + return criticalThreshold; + } + + protected static Map<String, String> getControllerRuntimeArguments() { + final Properties controllerProperties = + AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION, new Properties()); + + final Map<String, String> runtimeArgs = new LinkedHashMap<>(); + for (Map.Entry<Object, Object> property : controllerProperties.entrySet()) { + runtimeArgs.put(property.getKey().toString(), property.getValue().toString()); + } + + return runtimeArgs; + } + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/AbstractTCAECEFPolicyProcessorTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/AbstractTCAECEFPolicyProcessorTest.java new file mode 100644 index 0000000..716d5cc --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/AbstractTCAECEFPolicyProcessorTest.java @@ -0,0 +1,56 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca.processor; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +/** + * @author Rajiv Singla . Creation Date: 12/16/2016. + */ +public class AbstractTCAECEFPolicyProcessorTest extends BaseAnalyticsTCAUnitTest { + + private class DummyAbstractTCAECEFPolicyProcessor extends AbstractTCAECEFPolicyProcessor { + + @Override + public String getProcessorDescription() { + return "dummy"; + } + + @Override + public TCACEFProcessorContext processMessage(TCACEFProcessorContext processorContext) { + return processorContext; + } + } + + + @Test(expected = MessageProcessingException.class) + public void preProcessorWhenThereIsNoCEFMessage() throws Exception { + DummyAbstractTCAECEFPolicyProcessor dummyAbstractTCAECEFPolicyProcessor = new + DummyAbstractTCAECEFPolicyProcessor(); + + final TCACEFProcessorContext processorContext = new TCACEFProcessorContext(null, getSampleTCAPolicy()); + processorContext.setCEFEventListener(null); + dummyAbstractTCAECEFPolicyProcessor.preProcessor(processorContext); + } + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessorTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessorTest.java new file mode 100644 index 0000000..6f83653 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFJsonProcessorTest.java @@ -0,0 +1,117 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca.processor; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +/** + * + * @author Rajiv Singla . Creation Date: 11/9/2016. + */ +public class TCACEFJsonProcessorTest extends BaseAnalyticsTCAUnitTest { + + + // A valid CEF Message + @Test + public void testCEFJsonProcessorWithValidCEFMessage() throws Exception { + + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = + new TCACEFProcessorContext(cefMessageString, getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + + final EventListener cefEventListener = finalProcessorContext.getCEFEventListener(); + + assertNotNull("CEF Event Listener must be present", cefEventListener); + + } + + // Even if message is not a valid CEF format but still a Json - Json Processor will parse it + @Test + public void testCEFJsonProcessorWithValidJson() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext( + " { \"key\" : \"value\" } ", getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + final EventListener cefEventListener = finalProcessorContext.getCEFEventListener(); + + assertNotNull("Even if message is not a valid CEF format but a valid Json.Json Processor must be able to " + + "parse it", + cefEventListener); + } + + @Test(expected = MessageProcessingException.class) + public void testCEFJsonProcessorWithCEFMessageAsNull() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(null, getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + tcacefJsonProcessor.apply(tcacefProcessorContext); + + } + + @Test + public void testCEFJsonProcessorWithCEFMessageIsBlank() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(" ", getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + assertFalse("Blank message must terminate processing of message chain", finalProcessorContext + .canProcessingContinue()); + } + + + @Test + public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidMessage() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(" Invalid Message ", + getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + final TCACEFProcessorContext finalProcessorContext = tcacefJsonProcessor.apply(tcacefProcessorContext); + assertFalse("Invalid message must terminate processing of message chain", finalProcessorContext + .canProcessingContinue()); + } + + + @Test(expected = MessageProcessingException.class) + public void testCEFJsonProcessorWithCEFMessageWhichIsNotValidJson() throws Exception { + + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext( + " { \"Invalid Event Listener Json\" } ", getSampleTCAPolicy()); + + TCACEFJsonProcessor tcacefJsonProcessor = new TCACEFJsonProcessor(); + tcacefJsonProcessor.apply(tcacefProcessorContext); + } + + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyDomainFilterTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyDomainFilterTest.java new file mode 100644 index 0000000..f6925cb --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyDomainFilterTest.java @@ -0,0 +1,73 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca.processor; + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.common.service.processor.ProcessingState; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author Rajiv Singla . Creation Date: 12/16/2016. + */ +public class TCACEFPolicyDomainFilterTest extends BaseAnalyticsTCAUnitTest { + + private TCACEFPolicyDomainFilter tcacefPolicyDomainFilter; + private TCACEFProcessorContext processorContext; + private EventListener cefEventListener; + + @Before + public void before() throws Exception { + tcacefPolicyDomainFilter = new TCACEFPolicyDomainFilter(); + processorContext = new TCACEFProcessorContext("", getSampleTCAPolicy()); + cefEventListener = getCEFEventListener(); + processorContext.setCEFEventListener(cefEventListener); + } + + @Test + public void testProcessMessageWhenMessageIsValid() throws Exception { + tcacefPolicyDomainFilter.processMessage(processorContext); + assertThat("Processing must finish successfully", + tcacefPolicyDomainFilter.getProcessingState(), is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY)); + } + + @Test + public void testProcessMessageWhenCEFEventIsNull() throws Exception { + cefEventListener.setEvent(null); + processorContext.setCEFEventListener(cefEventListener); + tcacefPolicyDomainFilter.processMessage(processorContext); + assertThat("Processing must terminate early", + tcacefPolicyDomainFilter.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); + } + + @Test + public void testProcessMessageWhenPolicyDomainDoesNotMatchMessageDomain() throws Exception { + cefEventListener.getEvent().getCommonEventHeader().setDomain("notPolicyDomain"); + tcacefPolicyDomainFilter.processMessage(processorContext); + assertThat("Processing must terminate early", + tcacefPolicyDomainFilter.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); + } + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilterTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilterTest.java new file mode 100644 index 0000000..b9a1a65 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyFunctionalRoleFilterTest.java @@ -0,0 +1,75 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca.processor; + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.common.service.processor.ProcessingState; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author Rajiv Singla . Creation Date: 12/19/2016. + */ +public class TCACEFPolicyFunctionalRoleFilterTest extends BaseAnalyticsTCAUnitTest { + + private TCACEFPolicyFunctionalRoleFilter tcacefPolicyFunctionalRoleFilter; + private TCACEFProcessorContext processorContext; + private EventListener cefEventListener; + + @Before + public void before() throws Exception { + tcacefPolicyFunctionalRoleFilter = new TCACEFPolicyFunctionalRoleFilter(); + processorContext = new TCACEFProcessorContext("", getSampleTCAPolicy()); + cefEventListener = getCEFEventListener(); + processorContext.setCEFEventListener(cefEventListener); + } + + @Test + public void testProcessMessageWhenMessageIsValid() throws Exception { + tcacefPolicyFunctionalRoleFilter.processMessage(processorContext); + assertThat("Processing must finish successfully", + tcacefPolicyFunctionalRoleFilter.getProcessingState(), + is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY)); + } + + @Test + public void testProcessMessageWhenCEFEventIsNull() throws Exception { + cefEventListener.setEvent(null); + processorContext.setCEFEventListener(cefEventListener); + tcacefPolicyFunctionalRoleFilter.processMessage(processorContext); + assertThat("Processing must terminate early", + tcacefPolicyFunctionalRoleFilter.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); + } + + @Test + public void testProcessMessageWhenPolicyFunctionalRoleDoesNotMatchMessageFunctionalRole() throws Exception { + cefEventListener.getEvent().getCommonEventHeader().setFunctionalRole("someNonPolicyFunctionalRole"); + tcacefPolicyFunctionalRoleFilter.processMessage(processorContext); + assertThat("Processing must terminate early", + tcacefPolicyFunctionalRoleFilter.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); + } + + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java new file mode 100644 index 0000000..fb69e95 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessorTest.java @@ -0,0 +1,80 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca.processor; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.common.service.processor.ProcessingState; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +/** + * + * @author Rajiv Singla . Creation Date: 11/9/2016. + */ +public class TCACEFPolicyThresholdsProcessorTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testCEFPolicyThresholdProcessorWithNoThresholdViolation() throws Exception { + + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(cefMessageString, + getSampleTCAPolicy()); + tcacefProcessorContext.setCEFEventListener(getCEFEventListener()); + + AbstractTCAECEFPolicyProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); + final TCACEFProcessorContext finalProcessorContext = policyThresholdsProcessor.apply(tcacefProcessorContext); + + assertFalse("Process Context can Processing Continue flag should be false", finalProcessorContext + .canProcessingContinue()); + assertThat("Policy Threshold Processor State must be terminated early", + policyThresholdsProcessor.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); + assertEquals("Policy must not change", getSampleTCAPolicy(), finalProcessorContext.getTCAPolicy()); + + } + + @Test + public void testCEFPolicyThresholdProcessorWithThresholdViolation() throws Exception { + + final String cefMessageString = fromStream(CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(cefMessageString, + getSampleTCAPolicy()); + + final EventListener eventListener = getCEFEventListener(); + tcacefProcessorContext.setCEFEventListener(eventListener); + + AbstractTCAECEFPolicyProcessor policyThresholdsProcessor = new TCACEFPolicyThresholdsProcessor(); + final TCACEFProcessorContext finalProcessorContext = policyThresholdsProcessor.apply(tcacefProcessorContext); + + assertTrue("Process Context can Processing Continue flag should be true", finalProcessorContext + .canProcessingContinue()); + assertThat("Policy Threshold Processor State must be successful", + policyThresholdsProcessor.getProcessingState(), is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY)); + assertEquals("Policy must not change", getSampleTCAPolicy(), finalProcessorContext.getTCAPolicy()); + + } + +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContextTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContextTest.java new file mode 100644 index 0000000..d3b0ea4 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFProcessorContextTest.java @@ -0,0 +1,38 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca.processor; + +import org.junit.Test; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; + +/** + * + * @author Rajiv Singla . Creation Date: 11/14/2016. + */ +public class TCACEFProcessorContextTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testProcessorContextSerialization() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = new TCACEFProcessorContext(getValidCEFMessage(), + getSampleTCAPolicy()); + testSerialization(tcacefProcessorContext, TCACEFProcessorContextTest.class); + } +} diff --git a/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java new file mode 100644 index 0000000..7674eb8 --- /dev/null +++ b/dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java @@ -0,0 +1,406 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.dcae.apod.analytics.tca.utils; + +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Table; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; +import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants; +import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException; +import org.openecomp.dcae.apod.analytics.model.domain.cef.CommonEventHeader; +import org.openecomp.dcae.apod.analytics.model.domain.cef.Event; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener; +import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy; +import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold; +import org.openecomp.dcae.apod.analytics.model.facade.tca.TCAVESResponse; +import org.openecomp.dcae.apod.analytics.tca.BaseAnalyticsTCAUnitTest; +import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext; +import org.quartz.Job; +import org.quartz.JobDataMap; +import org.quartz.JobDetail; +import org.quartz.Scheduler; +import org.quartz.SimpleTrigger; +import org.quartz.impl.StdSchedulerFactory; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author Rajiv Singla . Creation Date: 11/9/2016. + */ +public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest { + + @Test + public void testGetPolicyFunctionalRoles() throws Exception { + + final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy(); + final List<String> policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(sampleTCAPolicy); + + assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles, + containsInAnyOrder("vFirewall", "vLoadBalancer")); + } + + @Test + public void testGetPolicyFunctionalRoleSupplier() throws Exception { + final TCAPolicy sampleTCAPolicy = getSampleTCAPolicy(); + final Supplier<List<String>> policyFunctionalRoleSupplier = TCAUtils.getPolicyFunctionalRoleSupplier + (sampleTCAPolicy); + final List<String> policyFunctionalRoles = policyFunctionalRoleSupplier.get(); + assertThat("Policy Functional Roles must contain vFirewall and vLoadBalancer", policyFunctionalRoles, + containsInAnyOrder("vFirewall", "vLoadBalancer")); + } + + @Test + public void testProcessCEFMessage() throws Exception { + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final TCACEFProcessorContext tcacefProcessorContext = TCAUtils.filterCEFMessage(cefMessageString, + getSampleTCAPolicy()); + assertThat("TCAECEFProcessor Processor Context can continue flag is true", tcacefProcessorContext + .canProcessingContinue(), is(true)); + } + + @Test + public void testGetPolicyFRThresholdsTableSupplier() throws Exception { + final Table<String, String, List<Threshold>> policyFRThresholdPathTable = TCAUtils + .getPolicyFRThresholdsTableSupplier(getSampleTCAPolicy()).get(); + + final Map<String, List<Threshold>> vFirewall = policyFRThresholdPathTable.row("vFirewall"); + final Map<String, List<Threshold>> vLoadBalancer = policyFRThresholdPathTable.row("vLoadBalancer"); + + final Set<String> vFirewallThresholdPaths = vFirewall.keySet(); + final Set<String> vLoadBalancerPaths = vLoadBalancer.keySet(); + + assertThat("vFirewall threshold field path size must be " + + "\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn\"", + vFirewallThresholdPaths.iterator().next(), + is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn")); + + assertThat("vLoadBalancer threshold field path size must be " + + "\"\"$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn\"", + vLoadBalancerPaths.iterator().next(), + is("$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn")); + + final List<Threshold> firewallThresholds = policyFRThresholdPathTable.get("vFirewall", + "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"); + final List<Threshold> vLoadBalancerThresholds = policyFRThresholdPathTable.get("vLoadBalancer", + "$.event.measurementsForVfScalingFields.vNicUsageArray[*].packetsIn"); + + assertThat("vFirewall Threshold size must be 2", firewallThresholds.size(), is(2)); + assertThat("vLoadBalancer Threshold size must be 2", vLoadBalancerThresholds.size(), is(2)); + } + + @Test + public void testGetJsonPathValueWithValidMessageAndPolicy() throws Exception { + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].bytesIn"; + final ImmutableSet<String> fieldPaths = ImmutableSet.of(jsonPath); + final Map<String, List<Long>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths); + assertThat("Json Path value must match", jsonPathValueMap.get(jsonPath).get(0), is(6086L)); + + } + + @Test + public void testGetJsonPathValueWithValidPath() throws Exception { + final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION); + final String jsonPath = "$.event.measurementsForVfScalingFields.vNicUsageArray[*].invalid"; + final ImmutableSet<String> fieldPaths = ImmutableSet.of(jsonPath); + final Map<String, List<Long>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths); + assertThat("Json path value must be empty", jsonPathValueMap.size(), is(0)); + + } + + + @Test + public void testCreateNewTCAVESResponseWithFunctionalRolevLoadBalancer() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + + MetricsPerFunctionalRole metricsPerFunctionalRole = mock(MetricsPerFunctionalRole.class); + when(metricsPerFunctionalRole.getThresholds()).thenReturn(getThresholds()); + when(metricsPerFunctionalRole.getPolicyScope()).thenReturn("Test Policy scope"); + when(tcacefProcessorContext.getMetricsPerFunctionalRole()).thenReturn(metricsPerFunctionalRole); + when(metricsPerFunctionalRole.getFunctionalRole()).thenReturn("vLoadBalancer"); + + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME"); + + //TODO : Add proper assertions, as the usage is not clearly understood + assertThat(tcaVESResponse.getClosedLoopControlName(), + is("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A")); + assertThat(tcaVESResponse.getVersion(), is("Test Version")); + assertThat(tcaVESResponse.getPolicyScope(), is("Test Policy scope")); + assertNull(tcaVESResponse.getAai().getGenericVNFId()); + assertNotNull(tcaVESResponse.getAai().getGenericServerId()); + } + + @Test + public void testCreateNewTCAVESResponseWithFunctionalRolevFirewall() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + + MetricsPerFunctionalRole metricsPerFunctionalRole = mock(MetricsPerFunctionalRole.class); + when(metricsPerFunctionalRole.getThresholds()).thenReturn(getThresholds()); + when(metricsPerFunctionalRole.getPolicyScope()).thenReturn("Test Policy scope"); + when(tcacefProcessorContext.getMetricsPerFunctionalRole()).thenReturn(metricsPerFunctionalRole); + when(metricsPerFunctionalRole.getFunctionalRole()).thenReturn("vFirewall"); + + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME"); + + //TODO : Add proper assertions, as the usage is not clearly understood + assertThat(tcaVESResponse.getClosedLoopControlName(), + is("CL-LBAL-LOW-TRAFFIC-SIG-FB480F95-A453-6F24-B767-FD703241AB1A")); + assertThat(tcaVESResponse.getVersion(), is("Test Version")); + assertThat(tcaVESResponse.getPolicyScope(), is("Test Policy scope")); + assertNotNull(tcaVESResponse.getAai().getGenericVNFId()); + assertNull(tcaVESResponse.getAai().getGenericServerId()); + } + + @Rule + public ExpectedException expectedIllegalArgumentException = ExpectedException.none(); + + @Test + public void testCreateNewTCAVESResponseNullFunctionalRole() throws Exception { + expectedIllegalArgumentException.expect(MessageProcessingException.class); + expectedIllegalArgumentException.expectCause(isA(IllegalArgumentException.class)); + expectedIllegalArgumentException.expectMessage("No violations metrics. Unable to create VES Response"); + + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + TCAVESResponse tcaVESResponse = TCAUtils.createNewTCAVESResponse(tcacefProcessorContext, "TCA_APP_NAME"); + assertNotNull(tcaVESResponse.getClosedLoopControlName()); + } + + @Test + public void testPrioritizeThresholdViolations() throws Exception { + + Map<String, Threshold> thresholdMap = new HashMap<>(); + Threshold majorThreshold = mock(Threshold.class); + when(majorThreshold.getSeverity()).thenReturn(EventSeverity.MAJOR); + thresholdMap.put("MAJOR", majorThreshold); + + Threshold result1 = TCAUtils.prioritizeThresholdViolations(thresholdMap); + assertEquals(result1.getSeverity(), EventSeverity.MAJOR); + + Threshold criticalThreshold = mock(Threshold.class); + when(criticalThreshold.getSeverity()).thenReturn(EventSeverity.CRITICAL); + thresholdMap.put("CRITICAL", criticalThreshold); + + Threshold result2 = TCAUtils.prioritizeThresholdViolations(thresholdMap); + assertEquals(result2.getSeverity(), EventSeverity.CRITICAL); + } + + @Test + public void testCreateViolatedMetrics() throws Exception { + TCAPolicy tcaPolicy = getSampleTCAPolicy(); + Threshold violatedThreshold = getCriticalThreshold(); + String functionalRole = "vFirewall"; + MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole); + assertThat(result.getPolicyScope(), is("resource=vFirewall;type=configuration")); + assertThat(result.getPolicyName(), is("configuration.dcae.microservice.tca.xml")); + } + + @Test + public void testCreateViolatedMetricsWrongFunctionalRole() throws Exception { + expectedIllegalArgumentException.expect(MessageProcessingException.class); + expectedIllegalArgumentException.expectCause(isA(IllegalStateException.class)); + expectedIllegalArgumentException.expectMessage("TCA Policy must contain functional Role: badFunctionRoleName"); + + TCAPolicy tcaPolicy = getSampleTCAPolicy(); + Threshold violatedThreshold = getCriticalThreshold(); + String functionalRole = "badFunctionRoleName"; + MetricsPerFunctionalRole result = TCAUtils.createViolatedMetrics(tcaPolicy, violatedThreshold, functionalRole); + } + + @Test + public void testGetDomainAndFunctionalRole() { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + EventListener eventListener = mock(EventListener.class); + Event event = mock(Event.class); + CommonEventHeader commonEventHeader = mock(CommonEventHeader.class); + + Pair<String, String> result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(eventListener); + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(eventListener.getEvent()).thenReturn(event); + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(event.getCommonEventHeader()).thenReturn(commonEventHeader); + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertNull(result.getLeft()); + assertNull(result.getRight()); + + when(commonEventHeader.getDomain()).thenReturn("testDomain"); + when(commonEventHeader.getFunctionalRole()).thenReturn("functionalRole"); + + result = TCAUtils.getDomainAndFunctionalRole(tcacefProcessorContext); + assertEquals(result.getLeft(), "testDomain"); + assertEquals(result.getRight(), "functionalRole"); + + } + + @Test + public void testComputeThresholdViolationsNotPresent() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true); + when(tcacefProcessorContext.getMessage()).thenReturn(getValidCEFMessage()); + + when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy()); + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + + TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext); + assertNotNull(result); + verify(result, times(0)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class)); + assertEquals("Policy must not change", getSampleTCAPolicy(), result.getTCAPolicy()); + } + + @Test + public void testComputeThresholdViolationsPresent() throws Exception { + TCACEFProcessorContext tcacefProcessorContext = mock(TCACEFProcessorContext.class); + when(tcacefProcessorContext.canProcessingContinue()).thenReturn(true); + final String cefMessageString = fromStream(CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION); + when(tcacefProcessorContext.getMessage()).thenReturn(cefMessageString); + + when(tcacefProcessorContext.getTCAPolicy()).thenReturn(getSampleTCAPolicy()); + when(tcacefProcessorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + + TCACEFProcessorContext result = TCAUtils.computeThresholdViolations(tcacefProcessorContext); + verify(result, times(1)).setMetricsPerFunctionalRole(Mockito.any(MetricsPerFunctionalRole.class)); + + assertEquals("Policy must not change", getSampleTCAPolicy(), result.getTCAPolicy()); + } + + + @Test + public void testCreateTCAPolicyMetricsPerFunctionalRoleList() throws Exception { + + final Map<String, String> tcaPolicyMap = TCAUtils.filterMapByKeyNamePrefix(getControllerRuntimeArguments(), + AnalyticsConstants.TCA_POLICY_METRICS_PER_FUNCTIONAL_ROLE_PATH); + + // determine functional Roles + final Map<String, Map<String, String>> functionalRolesMap = + TCAUtils.extractSubTree(tcaPolicyMap, 2, 3, AnalyticsConstants.TCA_POLICY_DELIMITER); + + final List<MetricsPerFunctionalRole> tcaPolicyMetricsPerFunctionalRoleList = + TCAUtils.createTCAPolicyMetricsPerFunctionalRoleList(functionalRolesMap); + + assertThat("There are two Metrics per function role", 2, + is(tcaPolicyMetricsPerFunctionalRoleList.size())); + } + + + @Test + public void testCreateQuartzScheduler() throws Exception { + final Scheduler scheduler = Mockito.mock(Scheduler.class); + final StdSchedulerFactory stdSchedulerFactory = Mockito.mock(StdSchedulerFactory.class); + when(stdSchedulerFactory.getScheduler()).thenReturn(scheduler); + final JobDataMap jobDataMap = Mockito.mock(JobDataMap.class); + TCAUtils.createQuartzScheduler(1000, stdSchedulerFactory, + "data/properties/quartz-test.properties", jobDataMap, Job.class, + "testJob", "testTigger"); + verify(scheduler, times(1)) + .scheduleJob(Mockito.any(JobDetail.class), Mockito.any(SimpleTrigger.class)); + } + + + @Test + public void testCreateTCAAlertStringWhenCEFIsEnabled() throws Exception { + final MetricsPerFunctionalRole violatedMetrics = createViolatedMetricsPerFunctionalRole(EventSeverity.CRITICAL); + TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class); + when(processorContext.getMetricsPerFunctionalRole()).thenReturn(violatedMetrics); + when(processorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + final String alertString = TCAUtils.createTCAAlertString(processorContext, "testApp", true); + assertTrue(alertString.contains("thresholdCrossingAlertFields")); + } + + @Test(expected = MessageProcessingException.class) + public void testCreateTCAAlertStringWhenViolatedMetricsNotPresentAndCEFIsEnabled() throws Exception { + TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class); + when(processorContext.getMetricsPerFunctionalRole()).thenReturn(null); + TCAUtils.createTCAAlertString(processorContext, "testApp", true); + } + + @Test + public void testCreateTCAAlertStringWhenCEFIsDisabled() throws Exception { + final MetricsPerFunctionalRole violatedMetrics = createViolatedMetricsPerFunctionalRole(EventSeverity.MAJOR); + TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class); + when(processorContext.getMetricsPerFunctionalRole()).thenReturn(violatedMetrics); + when(processorContext.getCEFEventListener()).thenReturn(getCEFEventListener()); + final String alertString = TCAUtils.createTCAAlertString(processorContext, "testApp", false); + assertFalse(alertString.contains("thresholdCrossingAlertFields")); + } + + @Test(expected = MessageProcessingException.class) + public void testCreateTCAAlertStringWhenViolatedMetricsNotPresentAndCEFIsDisabled() throws Exception { + TCACEFProcessorContext processorContext = mock(TCACEFProcessorContext.class); + when(processorContext.getMetricsPerFunctionalRole()).thenReturn(null); + TCAUtils.createTCAAlertString(processorContext, "testApp", false); + } + + private static MetricsPerFunctionalRole createViolatedMetricsPerFunctionalRole(EventSeverity severity) { + final Threshold violatedThreshold = new Threshold(); + violatedThreshold.setSeverity(severity); + violatedThreshold.setDirection(Direction.GREATER); + violatedThreshold.setClosedLoopControlName("violatedThresholdClosedLoopName"); + violatedThreshold.setActualFieldValue(100L); + violatedThreshold.setFieldPath("violatedThresholdFieldPath"); + violatedThreshold.setVersion("violatedThresholdVersion"); + violatedThreshold.setThresholdValue(50L); + + final MetricsPerFunctionalRole violatedMetrics = new MetricsPerFunctionalRole(); + violatedMetrics.setPolicyName("violatePolicyName"); + violatedMetrics.setPolicyVersion("violatedPolicyVersion"); + violatedMetrics.setPolicyScope("violatedPolicyScope"); + violatedMetrics.setFunctionalRole("violatedFunctionalRole"); + violatedMetrics.setThresholds(Arrays.asList(violatedThreshold)); + return violatedMetrics; + } +} |