ac03f7e82c64f7b094fc6ae56fd3c5b0c4b6f50d
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-web / src / test / java / org / onap / dcae / analytics / web / spring / ConfigBindingServiceEnvironmentPostProcessorTest.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.web.spring;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.util.Map;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.junit.jupiter.api.Test;
29 import org.mockito.Mockito;
30 import org.onap.dcae.analytics.model.AnalyticsProfile;
31 import org.onap.dcae.analytics.web.BaseAnalyticsWebTest;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
35 import org.springframework.boot.SpringApplication;
36 import org.springframework.core.env.ConfigurableEnvironment;
37 import org.springframework.core.env.MapPropertySource;
38 import org.springframework.core.env.MutablePropertySources;
39
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonParser;
42
43 import reactor.core.publisher.Flux;
44 import reactor.core.publisher.Mono;
45
46 public class ConfigBindingServiceEnvironmentPostProcessorTest extends BaseAnalyticsWebTest {
47
48     @Test
49     void postProcessEnvironment() {
50         CbsClient cbsClient = Mockito.mock(CbsClient.class);
51         ConfigBindingServiceEnvironmentPostProcessor testObject = Mockito.spy(new ConfigBindingServiceEnvironmentPostProcessor());
52         ConfigurableEnvironment configurableEnvironment = Mockito.mock(ConfigurableEnvironment.class);
53         SpringApplication springApplication = Mockito.mock(SpringApplication.class);
54         String[] activeProfiles = {AnalyticsProfile.CONFIG_BINDING_SERVICE_PROFILE_NAME};
55         Map<String, Object> filterKeyMap = Mockito.mock(Map.class);
56         String configServicePropertiesKey = "config-binding-service";
57         MutablePropertySources sources = configurableEnvironment.getPropertySources();
58
59         EnvProperties envProperties;
60         envProperties = ImmutableEnvProperties.builder() //
61                     .consulHost("consul-server.onap") //
62                     .consulPort(8500) //
63                     .cbsName("config-binding-service") //
64                     .appName("dcae-tca-analytics") //
65                     .build();
66         String strConfig =  "{\"spring.data.mongodb.uri\": \"mongodb://tcagen2-mongohost/analytics-tca\", \"streams_subscribes\": {\"tca_handle_in\": {\"type\": \"message_router\", \"dmaap_info\": {\"topic_url\": \"http://message-router.onap.svc.cluster.local:3904/events/unauthenticated.VES_MEASUREMENT3_OUTPUT\"}}}, \"tca.enable_ecomp_logging\": true, \"tca.enable_abatement\": true, \"tca.aai.password\": \"DCAE\", \"streams_subscribes.tca_handle_in.consumer_group\": \"cg1\", \"streams_subscribes.tca_handle_in.polling.auto_adjusting.step_up\": 10000, \"tca.aai.node_query_path\": \"aai/v11/search/nodes-query\", \"streams_publishes\": {\"tca_handle_out\": {\"type\": \"message_router\", \"dmaap_info\": {\"topic_url\": \"http://message-router.onap.svc.cluster.local:3904/events/unauthenticated.TCAGEN3-OUTPUT\"}}}, \"streams_subscribes.tca_handle_in.consumer_ids[1]\": \"c1\", \"tca.aai.generic_vnf_path\": \"aai/v11/network/generic-vnfs/generic-vnf\", \"streams_subscribes.tca_handle_in.polling.auto_adjusting.step_down\": 30000, \"streams_subscribes.tca_handle_in.polling.auto_adjusting.max\": 60000, \"tca.aai.username\": \"DCAE\", \"streams_subscribes.tca_handle_in.polling.auto_adjusting.min\": 30000, \"tca.aai.url\": \"http://aai.onap.svc.cluster.local\", \"streams_subscribes.tca_handle_in.timeout\": -1, \"tca.aai.enable_enrichment\": true, \"tca.policy\": \"{\\\"domain\\\":\\\"measurementsForVfScaling\\\",\\\"metricsPerEventName\\\":[{\\\"eventName\\\":\\\"vFirewallBroadcastPackets\\\",\\\"controlLoopSchemaType\\\":\\\"VM\\\",\\\"policyScope\\\":\\\"DCAE\\\",\\\"policyName\\\":\\\"DCAE.Config_tca-hi-lo\\\",\\\"policyVersion\\\":\\\"v0.0.1\\\",\\\"thresholds\\\":[{\\\"closedLoopControlName\\\":\\\"ControlLoop-vFirewall-d0a1dfc6-94f5-4fd4-a5b5-4630b438850a\\\",\\\"version\\\":\\\"1.0.3\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta\\\",\\\"thresholdValue\\\":300,\\\"direction\\\":\\\"LESS_OR_EQUAL\\\",\\\"severity\\\":\\\"MAJOR\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"},{\\\"closedLoopControlName\\\":\\\"ControlLoop-vFirewall-d0a1dfc6-94f5-4fd4-a5b5-4630b438850a\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta\\\",\\\"thresholdValue\\\":700,\\\"direction\\\":\\\"GREATER_OR_EQUAL\\\",\\\"severity\\\":\\\"CRITICAL\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"}]},{\\\"eventName\\\":\\\"vLoadBalancer\\\",\\\"controlLoopSchemaType\\\":\\\"VM\\\",\\\"policyScope\\\":\\\"DCAE\\\",\\\"policyName\\\":\\\"DCAE.Config_tca-hi-lo\\\",\\\"policyVersion\\\":\\\"v0.0.1\\\",\\\"thresholds\\\":[{\\\"closedLoopControlName\\\":\\\"ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta\\\",\\\"thresholdValue\\\":300,\\\"direction\\\":\\\"GREATER_OR_EQUAL\\\",\\\"severity\\\":\\\"CRITICAL\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"}]},{\\\"eventName\\\":\\\"Measurement_vGMUX\\\",\\\"controlLoopSchemaType\\\":\\\"VNF\\\",\\\"policyScope\\\":\\\"DCAE\\\",\\\"policyName\\\":\\\"DCAE.Config_tca-hi-lo\\\",\\\"policyVersion\\\":\\\"v0.0.1\\\",\\\"thresholds\\\":[{\\\"closedLoopControlName\\\":\\\"ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value\\\",\\\"thresholdValue\\\":0,\\\"direction\\\":\\\"EQUAL\\\",\\\"severity\\\":\\\"MAJOR\\\",\\\"closedLoopEventStatus\\\":\\\"ABATED\\\"},{\\\"closedLoopControlName\\\":\\\"ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value\\\",\\\"thresholdValue\\\":0,\\\"direction\\\":\\\"GREATER\\\",\\\"severity\\\":\\\"CRITICAL\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"}]}]}\", \"tca.processing_batch_size\": 10000, \"streams_subscribes.tca_handle_in.polling.fixed_rate\": 0, \"streams_subscribes.tca_handle_in.message_limit\": 50000, \"service_calls\": [], \"streams_subscribes.tca_handle_in.consumer_ids[0]\": \"c0\"}";
67
68         String parseConfig = "{\"config\": {\"spring.data.mongodb.uri\": \"mongodb://tcagen2-mongohost/analytics-tca\", \"streams_subscribes\": {\"tca_handle_in\": {\"type\": \"message_router\", \"dmaap_info\": {\"topic_url\": \"http://message-router.onap.svc.cluster.local:3904/events/unauthenticated.VES_MEASUREMENT3_OUTPUT\"}}}, \"tca.enable_ecomp_logging\": true, \"tca.enable_abatement\": true, \"tca.aai.password\": \"DCAE\", \"streams_subscribes.tca_handle_in.consumer_group\": \"cg1\", \"streams_subscribes.tca_handle_in.polling.auto_adjusting.step_up\": 10000, \"tca.aai.node_query_path\": \"aai/v11/search/nodes-query\", \"streams_publishes\": {\"tca_handle_out\": {\"type\": \"message_router\", \"dmaap_info\": {\"topic_url\": \"http://message-router.onap.svc.cluster.local:3904/events/unauthenticated.TCAGEN3-OUTPUT\"}}}, \"streams_subscribes.tca_handle_in.consumer_ids[1]\": \"c1\", \"tca.aai.generic_vnf_path\": \"aai/v11/network/generic-vnfs/generic-vnf\", \"streams_subscribes.tca_handle_in.polling.auto_adjusting.step_down\": 30000, \"streams_subscribes.tca_handle_in.polling.auto_adjusting.max\": 60000, \"tca.aai.username\": \"DCAE\", \"streams_subscribes.tca_handle_in.polling.auto_adjusting.min\": 30000, \"tca.aai.url\": \"http://aai.onap.svc.cluster.local\", \"streams_subscribes.tca_handle_in.timeout\": -1, \"tca.aai.enable_enrichment\": true, \"tca.policy\": \"{\\\"domain\\\":\\\"measurementsForVfScaling\\\",\\\"metricsPerEventName\\\":[{\\\"eventName\\\":\\\"vFirewallBroadcastPackets\\\",\\\"controlLoopSchemaType\\\":\\\"VM\\\",\\\"policyScope\\\":\\\"DCAE\\\",\\\"policyName\\\":\\\"DCAE.Config_tca-hi-lo\\\",\\\"policyVersion\\\":\\\"v0.0.1\\\",\\\"thresholds\\\":[{\\\"closedLoopControlName\\\":\\\"ControlLoop-vFirewall-d0a1dfc6-94f5-4fd4-a5b5-4630b438850a\\\",\\\"version\\\":\\\"1.0.3\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta\\\",\\\"thresholdValue\\\":300,\\\"direction\\\":\\\"LESS_OR_EQUAL\\\",\\\"severity\\\":\\\"MAJOR\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"},{\\\"closedLoopControlName\\\":\\\"ControlLoop-vFirewall-d0a1dfc6-94f5-4fd4-a5b5-4630b438850a\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta\\\",\\\"thresholdValue\\\":700,\\\"direction\\\":\\\"GREATER_OR_EQUAL\\\",\\\"severity\\\":\\\"CRITICAL\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"}]},{\\\"eventName\\\":\\\"vLoadBalancer\\\",\\\"controlLoopSchemaType\\\":\\\"VM\\\",\\\"policyScope\\\":\\\"DCAE\\\",\\\"policyName\\\":\\\"DCAE.Config_tca-hi-lo\\\",\\\"policyVersion\\\":\\\"v0.0.1\\\",\\\"thresholds\\\":[{\\\"closedLoopControlName\\\":\\\"ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta\\\",\\\"thresholdValue\\\":300,\\\"direction\\\":\\\"GREATER_OR_EQUAL\\\",\\\"severity\\\":\\\"CRITICAL\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"}]},{\\\"eventName\\\":\\\"Measurement_vGMUX\\\",\\\"controlLoopSchemaType\\\":\\\"VNF\\\",\\\"policyScope\\\":\\\"DCAE\\\",\\\"policyName\\\":\\\"DCAE.Config_tca-hi-lo\\\",\\\"policyVersion\\\":\\\"v0.0.1\\\",\\\"thresholds\\\":[{\\\"closedLoopControlName\\\":\\\"ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value\\\",\\\"thresholdValue\\\":0,\\\"direction\\\":\\\"EQUAL\\\",\\\"severity\\\":\\\"MAJOR\\\",\\\"closedLoopEventStatus\\\":\\\"ABATED\\\"},{\\\"closedLoopControlName\\\":\\\"ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e\\\",\\\"version\\\":\\\"1.0.2\\\",\\\"fieldPath\\\":\\\"$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value\\\",\\\"thresholdValue\\\":0,\\\"direction\\\":\\\"GREATER\\\",\\\"severity\\\":\\\"CRITICAL\\\",\\\"closedLoopEventStatus\\\":\\\"ONSET\\\"}]}]}\", \"tca.processing_batch_size\": 10000, \"streams_subscribes.tca_handle_in.polling.fixed_rate\": 0, \"streams_subscribes.tca_handle_in.message_limit\": 50000, \"service_calls\": [], \"streams_subscribes.tca_handle_in.consumer_ids[0]\": \"c0\"}}"; 
69         JsonParser parser = new JsonParser();
70         JsonObject json = parser.parse(strConfig).getAsJsonObject();
71         JsonObject parseJson = parser.parse(parseConfig).getAsJsonObject();
72
73         Mockito.when(configurableEnvironment.getActiveProfiles()).thenReturn(activeProfiles);
74         Mockito.when(testObject.readEnvironmentVariables()).thenReturn(Mono.just(envProperties));
75         Mockito.when(testObject.createCbsClient(envProperties)).thenReturn(Mono.just(cbsClient));
76         Mockito.when(testObject.periodicConfigurationUpdates(cbsClient)).thenReturn(Flux.just(json));
77         Mockito.doNothing().when(testObject).addJsonPropertySource(sources, new MapPropertySource(configServicePropertiesKey, filterKeyMap));
78         testObject.postProcessEnvironment(configurableEnvironment, springApplication);
79         assertEquals(ConfigBindingServiceEnvironmentPostProcessorTest.replaceBlank(strConfig), ConfigBindingServiceEnvironmentPostProcessorTest.replaceBlank(testObject.parseTcaConfig(parseJson)));
80     }
81
82     public static String replaceBlank(String str) {
83                 String dest = "";
84                 if (str!=null) {
85                         Pattern p = Pattern.compile("\\s*|\t|\r|\n");
86                         Matcher m = p.matcher(str);
87                         dest = m.replaceAll("");
88                 }
89                 return dest;
90         }
91 }