Update license date and text
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / policy / EntityEventPolicyTest.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.*;
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Matchers.anyString;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29
30 import org.apache.camel.Exchange;
31 import org.apache.camel.Message;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.aai.datarouter.policy.EntityEventPolicy;
36 import org.onap.aai.datarouter.policy.EntityEventPolicyConfig;
37 import org.onap.aai.datarouter.util.NodeUtils;
38 import org.onap.aai.datarouter.util.SearchServiceAgent;
39 import org.powermock.api.mockito.PowerMockito;
40
41
42
43 public class EntityEventPolicyTest {
44         EntityEventPolicy policy;
45         String eventJson;
46         
47         @SuppressWarnings("unchecked")
48     @Before
49     public void init() throws Exception {
50                 EntityEventPolicyConfig config = PowerMockito.mock(EntityEventPolicyConfig.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                 
57                 PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments().thenReturn(searchServiceAgent);
58                 
59                 
60                 policy = new EntityEventPolicyStubbed(config);
61                 FileInputStream event = new FileInputStream( new File("src/test/resources/aai_event.json"));
62                 eventJson = IOUtils.toString(event, "UTF-8");
63
64         }
65
66         @Test
67          public void testProcess() throws Exception {
68                 policy.process(getExchangeEvent("event1","create"));
69                 policy.process(getExchangeEvent("event2","create"));
70                 
71                 assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event1")));
72                 assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2")));
73                 
74                 policy.process(getExchangeEvent("event1","update"));
75                 policy.process(getExchangeEvent("event2","update"));
76                 assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event1")));
77                 assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2")));
78                 
79                 policy.process(getExchangeEvent("event2","delete"));
80                 assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2")));
81         }
82         
83         private Exchange getExchangeEvent(String link,String action){
84                 Object obj = eventJson.replace("$LINK",link ).replace("$ACTION",action) ;
85                 Exchange exchange = PowerMockito.mock(Exchange.class); 
86                 Message inMessage = PowerMockito.mock(Message.class);
87                 Message outMessage = PowerMockito.mock(Message.class);
88                 PowerMockito.when(exchange.getIn()).thenReturn(inMessage);              
89                 PowerMockito.when(inMessage.getBody()).thenReturn(obj);
90                 
91                 PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
92                 PowerMockito.doNothing().when(outMessage).setBody(anyObject());
93                 PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
94                 
95                 return exchange;
96                 
97         }
98
99         
100
101 }