d1c01af44f18295f980e8b260a87bc6c48802a9f
[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.onap.aai.datarouter.util.NodeUtils;
37 import org.onap.aai.datarouter.util.SearchServiceAgent;
38 import org.powermock.api.mockito.PowerMockito;
39
40
41
42 public class SpikeAggregateGenericVnfProcessorTest {
43   SpikeAggregateGenericVnfProcessor policy;
44   String eventJson;
45
46
47   @SuppressWarnings("unchecked")
48   @Before
49   public void init() throws Exception {
50     SpikeEventPolicyConfig config = PowerMockito.mock(SpikeEventPolicyConfig.class);
51     PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password");
52     PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT");
53
54
55     SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class);
56     PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments()
57         .thenReturn(searchServiceAgent);
58
59
60     policy = new SpikeAggregateGenericVnfProcessorStubbed(config);
61     FileInputStream event = new FileInputStream(new File("src/test/resources/spike_event.json"));
62     eventJson = IOUtils.toString(event, "UTF-8");  
63
64   }
65
66   @Test
67   public void testProcess_success() throws Exception {
68     policy.process(getExchangeEvent("12345", "create", "generic-vnf"));
69     policy.process(getExchangeEvent("23456", "create", "generic-vnf"));
70     
71     assertNotNull(
72         InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/12345")));
73     assertNotNull(
74         InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/23456")));
75
76    
77     policy.process(getExchangeEvent("23456", "delete", "generic-vnf"));
78     assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("23456")));
79   }
80   @Test
81   public void testProcess_fail() throws Exception {
82     policy.process(getExchangeEvent("666666", "create", "NotValid"));
83     assertNull(
84         InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("NotValid/666666")));
85     
86     policy.process(getExchangeEvent("", "create", "generic-vnf"));
87     assertNull(
88         InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/")));
89
90   }
91
92   
93   private Exchange getExchangeEvent(String key, String action, String type) {
94     Object obj = eventJson.replace("$KEY", key).replace("$ACTION", action).replace("$TYPE", type);
95     Exchange exchange = PowerMockito.mock(Exchange.class);
96     Message inMessage = PowerMockito.mock(Message.class);
97     Message outMessage = PowerMockito.mock(Message.class);
98     PowerMockito.when(exchange.getIn()).thenReturn(inMessage); 
99     PowerMockito.when(inMessage.getBody()).thenReturn(obj);
100
101     PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
102     PowerMockito.doNothing().when(outMessage).setBody(anyObject());
103     PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
104
105     return exchange;
106
107   }
108
109
110
111 }