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