Fix Spike Event Processing with Common Format
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / policy / SpikeEntitySearchProcessorTest.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 SpikeEntitySearchProcessorTest {
40   private SpikeEntitySearchProcessor policy;
41   private String eventJson;
42   private InMemorySearchDatastore searchDb;
43
44   @Before
45   public void init() throws Exception {
46     SpikeEventPolicyConfig config = PowerMockito.mock(SpikeEventPolicyConfig.class);
47     PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password");
48     PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT");
49
50     searchDb = new InMemorySearchDatastore();
51     policy = new SpikeEntitySearchProcessorStubbed(config).withSearchDb(searchDb);
52
53   }
54
55   @Test
56   public void testProcess_success() throws Exception {
57
58     String genericVnfEventJsonTemplate = IOUtils.toString(
59         new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8");
60
61     policy.process(
62         getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "gvnf123"));
63
64     assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
65
66     policy.process(
67         getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "DELETE", "gvnf123"));
68
69     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
70
71     
72   }
73   /*
74    * Failure test cases - no searchable attribute for type
75    */
76
77   @Test
78   public void testProcess_failure_unknownOxmEntityType() throws Exception {
79
80     String pserverEventJsonTemplate = IOUtils.toString(
81         new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")),
82         "UTF-8");
83
84     policy.process(
85         getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123"));
86
87     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123")));
88   }
89
90   @Test
91   public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception {
92
93     String pserverEventJsonTemplate = IOUtils.toString(
94         new FileInputStream(
95             new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")),
96         "UTF-8");
97
98     policy.process(
99         getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
100
101     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
102   }
103
104   @Test
105   public void testProcess_failure_missingMandatoryVertexProperties() throws Exception {
106
107     String pserverEventJsonTemplate =
108         IOUtils.toString(
109             new FileInputStream(
110                 new File("src/test/resources/pserver-missing-primary-key-spike-event.json")),
111             "UTF-8");
112
113     policy.process(
114         getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
115
116     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
117   }
118
119   @Test
120   public void testProcess_failure_noSuggestibleAttributesForEntityType() throws Exception {
121
122     String pserverEventJsonTemplate = IOUtils.toString(
123         new FileInputStream(new File("src/test/resources/vserver-spike-event.json")), "UTF-8");
124
125     policy.process(
126         getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "vserver123"));
127
128     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("vserver/vserver123")));
129   }
130
131   private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType,
132       String entityKey) {
133     Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType)
134         .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey);
135
136     Exchange exchange = PowerMockito.mock(Exchange.class);
137     Message inMessage = PowerMockito.mock(Message.class);
138     Message outMessage = PowerMockito.mock(Message.class);
139     PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
140     PowerMockito.when(inMessage.getBody()).thenReturn(obj);
141
142     PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
143     PowerMockito.doNothing().when(outMessage).setBody(anyObject());
144     PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
145
146     return exchange;
147
148   }
149
150
151
152 }