router-core 1.3.0 changes
[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.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.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;
44
45
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration("file:src/test/resources/spring-beans/data-router-oxm.xml")
48 public class EntityEventPolicyTest {
49   private EntityEventPolicyConfig eventPolicyConfig;
50   private EntityEventPolicy policy;
51   private String eventJson;
52   private InMemorySearchDatastore searchDb;
53
54   @Autowired
55   private SchemaVersions schemaVersions;
56   @Autowired
57   private SchemaLocationsBean schemaLocationsBean;
58
59   @SuppressWarnings("unchecked")
60   @Before
61   public void init() throws Exception {
62     eventPolicyConfig = new EntityEventPolicyConfig();
63     eventPolicyConfig.setSearchKeystorePwd("password");
64     eventPolicyConfig.setSourceDomain("JUNIT");
65
66     eventPolicyConfig.setSchemaVersions(schemaVersions);
67     eventPolicyConfig.setSchemaLocationsBean(schemaLocationsBean);
68
69     searchDb = new InMemorySearchDatastore();
70     policy = new EntityEventPolicyStubbed(eventPolicyConfig).withSearchDb(searchDb);
71
72     FileInputStream event = new FileInputStream(new File("src/test/resources/aai_event.json"));
73     eventJson = IOUtils.toString(event, "UTF-8");
74
75   }
76
77   @Test
78   public void testProcess() throws Exception {
79     policy.process(getExchangeEvent("event1", "create"));
80     policy.process(getExchangeEvent("event2", "create"));
81
82     assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event1")));
83     assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event2")));
84
85     policy.process(getExchangeEvent("event1", "update"));
86     policy.process(getExchangeEvent("event2", "update"));
87     assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event1")));
88     assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event2")));
89
90     policy.process(getExchangeEvent("event2", "delete"));
91     assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event2")));
92   }
93
94   private Exchange getExchangeEvent(String link, String action) {
95     Object obj = eventJson.replace("$LINK", link).replace("$ACTION", action);
96     Exchange exchange = PowerMockito.mock(Exchange.class);
97     Message inMessage = PowerMockito.mock(Message.class);
98     Message outMessage = PowerMockito.mock(Message.class);
99     PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
100     PowerMockito.when(inMessage.getBody()).thenReturn(obj);
101
102     PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
103     PowerMockito.doNothing().when(outMessage).setBody(anyObject());
104     PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
105
106     return exchange;
107
108   }
109
110
111
112 }