Fix Spike Event Processing with Common Format
[aai/data-router.git] / src / test / java / org / onap / aai / datarouter / policy / SpikeEntityEventPolicyTest.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.util.NodeUtils;
36 import org.powermock.api.mockito.PowerMockito;
37
38 public class SpikeEntityEventPolicyTest {
39   private SpikeEntityEventPolicyConfig eventPolicyConfig;
40   private SpikeEntityEventPolicy policy;
41   private InMemorySearchDatastore searchDb;
42   
43   
44   @Before
45   public void init() throws Exception {
46     
47     eventPolicyConfig = new SpikeEntityEventPolicyConfig();
48     eventPolicyConfig.setSearchKeystorePwd("password");
49     eventPolicyConfig.setSourceDomain("JUNIT");
50
51     searchDb = new InMemorySearchDatastore();
52     policy = new SpikeEntityEventPolicyStubbed(eventPolicyConfig).withSearchDb(searchDb);
53
54   }
55
56   @Test
57   public void testProcess_success() throws Exception {
58     
59     String pserverEventJsonTemplate = IOUtils.toString(
60         new FileInputStream(new File("src/test/resources/pserver-spike-event.json")), "UTF-8");
61     
62     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
63
64     assertNotNull(
65         searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
66   }
67   
68   @Test
69   public void testProcess_failure_unknownOxmEntityType() throws Exception {
70     
71     String pserverEventJsonTemplate = IOUtils.toString(
72         new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")), "UTF-8");
73     
74     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123"));
75
76     assertNull(
77         searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123")));
78   }
79   
80   @Test
81   public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception {
82     
83     String pserverEventJsonTemplate = IOUtils.toString(
84         new FileInputStream(new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")), "UTF-8");
85     
86     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
87
88     assertNull(
89         searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
90   }
91   
92   @Test
93   public void testProcess_failure_missingMandatoryVertexProperties() throws Exception {
94     
95     String pserverEventJsonTemplate = IOUtils.toString(
96         new FileInputStream(new File("src/test/resources/pserver-missing-primary-key-spike-event.json")), "UTF-8");
97     
98     policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123"));
99
100     assertNull(
101         searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123")));
102   }
103   
104   private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType,
105       String entityKey) {
106     Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType)
107         .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey);
108
109     Exchange exchange = PowerMockito.mock(Exchange.class);
110     Message inMessage = PowerMockito.mock(Message.class);
111     Message outMessage = PowerMockito.mock(Message.class);
112     PowerMockito.when(exchange.getIn()).thenReturn(inMessage);
113     PowerMockito.when(inMessage.getBody()).thenReturn(obj);
114
115     PowerMockito.when(exchange.getOut()).thenReturn(outMessage);
116     PowerMockito.doNothing().when(outMessage).setBody(anyObject());
117     PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject());
118
119     return exchange;
120
121   }
122
123
124
125 }