router-core 1.3.0 changes
[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.junit.runner.RunWith;
37 import org.onap.aai.datarouter.util.NodeUtils;
38 import org.onap.aai.setup.SchemaLocationsBean;
39 import org.onap.aai.setup.SchemaVersions;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @ContextConfiguration("file:src/test/resources/spring-beans/data-router-oxm.xml")
47 public class SpikeAggregateGenericVnfProcessorTest {
48   private SpikeEventPolicyConfig eventPolicyConfig;
49   private SpikeAggregateGenericVnfProcessor policy;
50   private InMemorySearchDatastore searchDb;
51
52   @Autowired
53   private SchemaVersions schemaVersions;
54   @Autowired
55   private SchemaLocationsBean schemaLocationsBean;
56   
57   @Before
58   public void init() throws Exception {
59     eventPolicyConfig = new SpikeEventPolicyConfig();
60     eventPolicyConfig.setSearchKeystorePwd("password");
61     eventPolicyConfig.setSourceDomain("JUNIT");
62     
63     eventPolicyConfig.setSchemaVersions(schemaVersions);
64     eventPolicyConfig.setSchemaLocationsBean(schemaLocationsBean);
65
66     searchDb = new InMemorySearchDatastore();
67     policy = new SpikeAggregateGenericVnfProcessorStubbed(eventPolicyConfig).withSearchDb(searchDb);
68   }
69
70   @Test
71   public void testProcess_success() throws Exception {
72
73     String genericVnfEventJsonTemplate = IOUtils.toString(
74         new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8");
75
76     policy.process(
77         getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "gvnf123"));
78
79     assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
80
81     policy.process(
82         getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "DELETE", "gvnf123"));
83
84     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
85
86     
87   }
88   /*
89    * Failure test cases - no searchable attribute for type
90    */
91
92   @Test
93   public void testProcess_failure_unknownOxmEntityType() throws Exception {
94
95     String pserverEventJsonTemplate = IOUtils.toString(
96         new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")),
97         "UTF-8");
98
99     policy.process(
100         getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123"));
101
102     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123")));
103   }
104
105   @Test
106   public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception {
107
108     String pserverEventJsonTemplate = IOUtils.toString(
109         new FileInputStream(
110             new File("src/test/resources/pserver-missing-mandtory-field-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   private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType,
120       String entityKey) {
121     Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType)
122         .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey);
123
124     Exchange exchange = PowerMockito.mock(Exchange.class);
125     Message inMessage = PowerMockito.mock(Message.class);
126     Message outMessage = PowerMockito.mock(Message.class);
127     PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
128     PowerMockito.when(inMessage.getBody()).thenReturn(obj);
129
130     PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
131     PowerMockito.doNothing().when(outMessage).setBody(anyObject());
132     PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
133
134     return exchange;
135
136   }
137
138 }