Update Data Router with new schema ingest lib
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / policy / SpikeAutosuggestProcessorTest.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.Application;
38 import org.onap.aai.datarouter.util.NodeUtils;
39 import org.onap.aai.setup.SchemaLocationsBean;
40 import org.onap.aai.setup.SchemaVersions;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration(classes = {Application.class})
48 public class SpikeAutosuggestProcessorTest {
49   private SpikeEventPolicyConfig eventPolicyConfig;
50   private SpikeAutosuggestIndexProcessor policy;
51   private InMemorySearchDatastore searchDb;
52
53   @Autowired
54   private SchemaVersions schemaVersions;
55   @Autowired
56   private SchemaLocationsBean schemaLocationsBean;
57
58   @Before
59   public void init() throws Exception {
60
61     eventPolicyConfig = new SpikeEventPolicyConfig();
62     eventPolicyConfig.setSearchKeystorePwd("password");
63     eventPolicyConfig.setSourceDomain("JUNIT");
64   
65     eventPolicyConfig.setSchemaVersions(schemaVersions);
66     eventPolicyConfig.setSchemaLocationsBean(schemaLocationsBean);
67
68     searchDb = new InMemorySearchDatastore();
69     policy = new SpikeAutosuggestProcessorStubbed(eventPolicyConfig).withSearchDb(searchDb); 
70
71   }
72
73   @Test
74   public void testProcess_success() throws Exception {
75     
76     String genericVnfEventJsonTemplate = IOUtils.toString(
77         new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8");
78   
79     policy.process(getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "vserver123"));
80     
81     assertNotNull(
82         searchDb.get(NodeUtils.generateUniqueShaDigest("junk and Running VNFs")));
83     assertNotNull(
84         searchDb.get(NodeUtils.generateUniqueShaDigest("junk VNFs")));
85     assertNotNull(
86         searchDb.get(NodeUtils.generateUniqueShaDigest("Running VNFs")));
87    
88   }
89   
90   /*
91    * Failure test cases
92    * - no searchable attribute for type
93    */
94   
95   @Test
96   public void testProcess_failure_unknownOxmEntityType() throws Exception {
97     
98     String pserverEventJsonTemplate = IOUtils.toString(
99         new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")), "UTF-8");
100     
101     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123"));
102
103     assertNull(
104         searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123")));
105   }
106   
107   @Test
108   public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception {
109     
110     String pserverEventJsonTemplate = IOUtils.toString(
111         new FileInputStream(new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")), "UTF-8");
112     
113     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
114
115     assertNull(
116         searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
117   }
118   
119   @Test
120   public void testProcess_failure_missingMandatoryVertexProperties() throws Exception {
121     
122     String pserverEventJsonTemplate = IOUtils.toString(
123         new FileInputStream(new File("src/test/resources/pserver-missing-primary-key-spike-event.json")), "UTF-8");
124     
125     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
126
127     assertNull(
128         searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
129   }
130   
131   @Test
132   public void testProcess_failure_noSuggestibleAttributesForEntityType() throws Exception {
133     
134     String pserverEventJsonTemplate = IOUtils.toString(
135         new FileInputStream(new File("src/test/resources/vserver-spike-event.json")), "UTF-8");
136     
137     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "vserver123"));
138
139     assertNull(
140         searchDb.get(NodeUtils.generateUniqueShaDigest("vserver/vserver123")));
141   }
142     
143   private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType,
144       String entityKey) {
145     Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType)
146         .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey);
147
148     Exchange exchange = PowerMockito.mock(Exchange.class);
149     Message inMessage = PowerMockito.mock(Message.class);
150     Message outMessage = PowerMockito.mock(Message.class);
151     PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
152     PowerMockito.when(inMessage.getBody()).thenReturn(obj);
153
154     PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
155     PowerMockito.doNothing().when(outMessage).setBody(anyObject());
156     PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
157
158     return exchange;
159
160   }
161
162
163 }