2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.datarouter.policy;
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;
29 import java.io.FileInputStream;
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;
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @ContextConfiguration("file:src/test/resources/spring-beans/data-router-oxm.xml")
47 public class SpikeEntitySearchProcessorTest {
48 private SpikeEventPolicyConfig eventPolicyConfig;
49 private SpikeEntitySearchProcessor policy;
50 private String eventJson;
51 private InMemorySearchDatastore searchDb;
54 private SchemaVersions schemaVersions;
56 private SchemaLocationsBean schemaLocationsBean;
59 public void init() throws Exception {
60 eventPolicyConfig = new SpikeEventPolicyConfig();
61 eventPolicyConfig.setSearchKeystorePwd("password");
62 eventPolicyConfig.setSourceDomain("JUNIT");
64 eventPolicyConfig.setSchemaVersions(schemaVersions);
65 eventPolicyConfig.setSchemaLocationsBean(schemaLocationsBean);
67 searchDb = new InMemorySearchDatastore();
68 policy = new SpikeEntitySearchProcessorStubbed(eventPolicyConfig).withSearchDb(searchDb);
73 public void testProcess_success() throws Exception {
75 String genericVnfEventJsonTemplate = IOUtils.toString(
76 new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8");
79 getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "gvnf123"));
81 assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
84 getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "DELETE", "gvnf123"));
86 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
91 * Failure test cases - no searchable attribute for type
95 public void testProcess_failure_unknownOxmEntityType() throws Exception {
97 String pserverEventJsonTemplate = IOUtils.toString(
98 new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")),
102 getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123"));
104 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123")));
108 public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception {
110 String pserverEventJsonTemplate = IOUtils.toString(
112 new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")),
116 getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
118 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
122 public void testProcess_failure_missingMandatoryVertexProperties() throws Exception {
124 String pserverEventJsonTemplate =
127 new File("src/test/resources/pserver-missing-primary-key-spike-event.json")),
131 getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
133 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
137 public void testProcess_failure_noSuggestibleAttributesForEntityType() throws Exception {
139 String pserverEventJsonTemplate = IOUtils.toString(
140 new FileInputStream(new File("src/test/resources/vserver-spike-event.json")), "UTF-8");
143 getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "vserver123"));
145 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("vserver/vserver123")));
148 private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType,
150 Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType)
151 .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey);
153 Exchange exchange = PowerMockito.mock(Exchange.class);
154 Message inMessage = PowerMockito.mock(Message.class);
155 Message outMessage = PowerMockito.mock(Message.class);
156 PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
157 PowerMockito.when(inMessage.getBody()).thenReturn(obj);
159 PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
160 PowerMockito.doNothing().when(outMessage).setBody(anyObject());
161 PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());