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.onap.aai.datarouter.util.SearchServiceAgent;
38 import org.powermock.api.mockito.PowerMockito;
42 public class SpikeEntitySearchProcessorTest {
43 SpikeEntitySearchProcessor policy;
46 @SuppressWarnings("unchecked")
48 public void init() throws Exception {
49 SpikeEventPolicyConfig config = PowerMockito.mock(SpikeEventPolicyConfig.class);
50 PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password");
51 PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT");
54 SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class);
55 PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments()
56 .thenReturn(searchServiceAgent);
59 policy = new SpikeEntitySearchProcessorStubbed(config);
60 FileInputStream event = new FileInputStream(new File("src/test/resources/spike_event.json"));
61 eventJson = IOUtils.toString(event, "UTF-8");
66 public void testProcess_success() throws Exception {
67 policy.process(getExchangeEvent("12345", "create", "generic-vnf"));
68 policy.process(getExchangeEvent("23456", "create", "generic-vnf"));
71 InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/12345")));
73 InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/23456")));
76 policy.process(getExchangeEvent("23456", "delete", "generic-vnf"));
77 assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/23456")));
79 policy.process(getExchangeEvent("333333", "", "generic-vnf"));
80 assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/333333")));
83 public void testProcess_fail() throws Exception {
84 policy.process(getExchangeEvent("xxxxx", "create", "NotValid"));
86 InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("NotValid/xxxxx")));
88 policy.process(getExchangeEvent("", "create", "generic-vnf"));
90 InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/")));
92 policy.process(getExchangeEvent("yyyy", "create", ""));
94 InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("/yyyy")));
95 policy.process(getExchangeEvent("", "create", ""));
97 InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("/")));
100 public void testProcess_null() throws Exception {
101 policy.process(getExchangeEvent());
104 private Exchange getExchangeEvent(String key, String action, String type) {
105 Object obj = eventJson.replace("$KEY", key).replace("$ACTION", action).replace("$TYPE", type);
106 Exchange exchange = PowerMockito.mock(Exchange.class);
107 Message inMessage = PowerMockito.mock(Message.class);
108 Message outMessage = PowerMockito.mock(Message.class);
109 PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
110 PowerMockito.when(inMessage.getBody()).thenReturn(obj);
112 PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
113 PowerMockito.doNothing().when(outMessage).setBody(anyObject());
114 PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
120 private Exchange getExchangeEvent() {
122 Exchange exchange = PowerMockito.mock(Exchange.class);
123 Message inMessage = PowerMockito.mock(Message.class);
124 Message outMessage = PowerMockito.mock(Message.class);
125 PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
126 PowerMockito.when(inMessage.getBody()).thenReturn(obj);
128 PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
129 PowerMockito.doNothing().when(outMessage).setBody(anyObject());
130 PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());