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.onap.aai.datarouter.util.NodeUtils;
37 import org.powermock.api.mockito.PowerMockito;
39 public class SpikeAggregateGenericVnfProcessorTest {
40 private SpikeAggregateGenericVnfProcessor policy;
41 private InMemorySearchDatastore searchDb;
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");
49 searchDb = new InMemorySearchDatastore();
50 policy = new SpikeAggregateGenericVnfProcessorStubbed(config).withSearchDb(searchDb);
54 public void testProcess_success() throws Exception {
56 String genericVnfEventJsonTemplate = IOUtils.toString(
57 new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8");
60 getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "gvnf123"));
62 assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
65 getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "DELETE", "gvnf123"));
67 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123")));
72 * Failure test cases - no searchable attribute for type
76 public void testProcess_failure_unknownOxmEntityType() throws Exception {
78 String pserverEventJsonTemplate = IOUtils.toString(
79 new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")),
83 getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123"));
85 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123")));
89 public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception {
91 String pserverEventJsonTemplate = IOUtils.toString(
93 new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")),
97 getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
99 assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
102 private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType,
104 Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType)
105 .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey);
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);
113 PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
114 PowerMockito.doNothing().when(outMessage).setBody(anyObject());
115 PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());