Fix Spike Event Processing with Common Format
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / policy / SpikeAggregateGenericVnfProcessorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.datarouter.policy;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.Matchers.anyObject;
26 import static org.mockito.Matchers.anyString;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30
31 import org.apache.camel.Exchange;
32 import org.apache.camel.Message;
33 import org.apache.commons.io.IOUtils;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.aai.datarouter.util.NodeUtils;
37 import org.powermock.api.mockito.PowerMockito;
38
39 public class SpikeAggregateGenericVnfProcessorTest {
40   private SpikeAggregateGenericVnfProcessor policy;
41   private InMemorySearchDatastore searchDb;
42
43   @Before
44   public void init() throws Exception {
45     SpikeEventPolicyConfig config = PowerMockito.mock(SpikeEventPolicyConfig.class);
46     PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password");
47     PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT");
48
49     searchDb = new InMemorySearchDatastore();
50     policy = new SpikeAggregateGenericVnfProcessorStubbed(config).withSearchDb(searchDb);
51   }
52
53   @Test
54   public void testProcess_success() throws Exception {
55
56     String genericVnfEventJsonTemplate = IOUtils.toString(
57         new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8");
58
59     policy.process(
60         getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "gvnf123"));
61
62     assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
63
64     policy.process(
65         getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "DELETE", "gvnf123"));
66
67     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
68
69     
70   }
71   /*
72    * Failure test cases - no searchable attribute for type
73    */
74
75   @Test
76   public void testProcess_failure_unknownOxmEntityType() throws Exception {
77
78     String pserverEventJsonTemplate = IOUtils.toString(
79         new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")),
80         "UTF-8");
81
82     policy.process(
83         getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123"));
84
85     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123")));
86   }
87
88   @Test
89   public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception {
90
91     String pserverEventJsonTemplate = IOUtils.toString(
92         new FileInputStream(
93             new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")),
94         "UTF-8");
95
96     policy.process(
97         getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
98
99     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
100   }
101
102   private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType,
103       String entityKey) {
104     Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType)
105         .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey);
106
107     Exchange exchange = PowerMockito.mock(Exchange.class);
108     Message inMessage = PowerMockito.mock(Message.class);
109     Message outMessage = PowerMockito.mock(Message.class);
110     PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
111     PowerMockito.when(inMessage.getBody()).thenReturn(obj);
112
113     PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
114     PowerMockito.doNothing().when(outMessage).setBody(anyObject());
115     PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
116
117     return exchange;
118
119   }
120
121 }