Feature for micro service communication
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / onap / appc / oam / messageadapter / MessageAdapterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2018 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.oam.messageadapter;
27
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.onap.appc.adapter.message.MessageAdapterFactory;
33 import org.onap.appc.adapter.message.Producer;
34 import org.onap.appc.configuration.Configuration;
35 import org.mockito.Mockito;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeaderBuilder;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
39 import org.osgi.framework.Bundle;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.osgi.framework.ServiceReference;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 import org.powermock.reflect.Whitebox;
47 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFLogger.Level;
49 import com.fasterxml.jackson.core.JsonProcessingException;
50 import org.onap.appc.configuration.ConfigurationFactory;
51 import org.onap.appc.oam.AppcOam.RPC;
52 import java.util.HashSet;
53 import java.util.Properties;
54 import java.util.Set;
55 import static org.junit.Assert.assertEquals;
56 import static org.mockito.Mockito.mock;
57 import static org.powermock.api.mockito.PowerMockito.mockStatic;
58
59
60 @RunWith(PowerMockRunner.class)
61 @PrepareForTest({FrameworkUtil.class, ConfigurationFactory.class, Converter.class})
62 public class MessageAdapterTest {
63
64     private Producer fakeProducer;
65
66     private MessageAdapter messageAdapter;
67
68
69     @Before
70     public final void setup() throws Exception {
71         fakeProducer = mock(Producer.class);
72         messageAdapter = new MessageAdapter();
73     }
74
75     @Test
76     public void testGetProducerReturnsNull() throws Exception {
77         MessageAdapter maSpy = Mockito.spy(messageAdapter);
78         Mockito.doNothing().when(maSpy).createProducer();
79
80         Producer producer = maSpy.getProducer();
81         Assert.assertTrue("getProducer() did not return null", producer == null);
82         Producer mySpyProducer = Whitebox.getInternalState(maSpy, "producer");
83         Assert.assertTrue("MessageAdapter producer is not null", mySpyProducer == null);
84         Mockito.verify(maSpy, Mockito.times(1)).createProducer();
85     }
86
87     @Test
88     public void testGetProducerWithExistingProducer() throws Exception {
89         MessageAdapter maSpy = Mockito.spy(messageAdapter);
90         Whitebox.setInternalState(maSpy, "producer", fakeProducer);
91
92         Producer producer = maSpy.getProducer();
93         Assert.assertTrue("getProducer() returned null", producer == fakeProducer);
94         Mockito.verify(maSpy, Mockito.times(0)).createProducer();
95     }
96
97     @Test
98     public void testGetProducerWithCreateProducer() throws Exception {
99         MessageAdapter maSpy = Mockito.spy(messageAdapter);
100         Whitebox.setInternalState(maSpy, "producer", (Object) null);
101         HashSet<String> pool = new HashSet<>();
102         pool.add("NOT_HTTPS");
103         pool.add("https");
104         Whitebox.setInternalState(maSpy, "pool", pool);
105
106         // Prepare all mocks
107         mockStatic(FrameworkUtil.class);
108         Bundle maBundle = mock(Bundle.class);
109         PowerMockito.when(FrameworkUtil.getBundle(MessageAdapter.class)).thenReturn(maBundle);
110
111         BundleContext maBundleContext = mock(BundleContext.class);
112         Mockito.when(maBundle.getBundleContext()).thenReturn(maBundleContext);
113
114         ServiceReference svcRef = mock(ServiceReference.class);
115         Mockito.when(maBundleContext.getServiceReference(MessageAdapterFactory.class.getName())).thenReturn(svcRef);
116
117         MessageAdapterFactory maFactory = mock(MessageAdapterFactory.class);
118         Mockito.when(maBundleContext.getService(svcRef)).thenReturn(maFactory);
119         Mockito.when(maFactory.createProducer(pool, (String) null, null, null)).thenReturn(fakeProducer);
120
121         Producer producer = maSpy.getProducer();
122         Assert.assertTrue("getProducer() result does not match", producer == fakeProducer);
123         Producer mySpyProducer = Whitebox.getInternalState(maSpy, "producer");
124         Assert.assertTrue("MessageAdapter producer does not match",mySpyProducer == fakeProducer);
125         Mockito.verify(maSpy, Mockito.times(1)).createProducer();
126     }
127
128     @Test
129     public void testUpdateProperties() {
130         MessageAdapter maSpy = Mockito.spy(messageAdapter);
131         Mockito.doNothing().when(maSpy).createProducer();
132         Whitebox.setInternalState(maSpy, "isDisabled", false);
133         PowerMockito.mockStatic(ConfigurationFactory.class);
134         Configuration mockConfig = Mockito.mock(Configuration.class);
135         Properties properties = new Properties();
136         properties.setProperty("appc.OAM.poolMembers", "hostname1,hostname2");
137         Mockito.when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfig);
138         Mockito.doReturn(properties).when(mockConfig).getProperties();
139         maSpy.init();
140         assertEquals(2, ((Set<String>)Whitebox.getInternalState(maSpy, "pool")).size());
141     }
142
143     @Test
144     public void testPost() throws JsonProcessingException {
145         MessageAdapter maSpy = Mockito.spy(messageAdapter);
146         OAMContext oamContext = new OAMContext();
147         oamContext.setRpcName(RPC.maintenance_mode);
148         CommonHeader mockCommonHeader = Mockito.mock(CommonHeader.class);
149         Mockito.doReturn("TEST REQUEST ID").when(mockCommonHeader).getRequestId();
150         oamContext.setCommonHeader(mockCommonHeader);
151         Status mockStatus = Mockito.mock(Status.class);
152         oamContext.setStatus(mockStatus);
153         EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
154         Mockito.doReturn(true).when(mockLogger).isTraceEnabled();
155         Mockito.doReturn(true).when(mockLogger).isDebugEnabled();
156         Whitebox.setInternalState(maSpy, "logger", mockLogger);
157         PowerMockito.mockStatic(Converter.class);
158         Mockito.when(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext)).thenReturn("{cambriaPartition='MSO', rpcName='maintenance_mode',"
159                 + " body=Body{output=MaintenanceModeOutput [_commonHeader=CommonHeader, hashCode: 14584991,"
160                 + " _status=Status, hashCode: 24801521, augmentation=[]]}}");
161         Mockito.doNothing().when(maSpy).createProducer();
162         maSpy.post(oamContext);
163         Mockito.verify(mockLogger).trace(Mockito.contains("Entering to post"));
164         Mockito.verify(mockLogger).trace("Exiting from post with (success = false)");
165     }
166
167     @Test
168     public void testPostExceptionFlow() throws JsonProcessingException {
169         MessageAdapter maSpy = Mockito.spy(messageAdapter);
170         OAMContext oamContext = new OAMContext();
171         oamContext.setRpcName(RPC.maintenance_mode);
172         CommonHeader mockCommonHeader = Mockito.mock(CommonHeader.class);
173         Mockito.doReturn("TEST REQUEST ID").when(mockCommonHeader).getRequestId();
174         oamContext.setCommonHeader(mockCommonHeader);
175         Status mockStatus = Mockito.mock(Status.class);
176         oamContext.setStatus(mockStatus);
177         EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
178         Mockito.doReturn(false).when(mockLogger).isTraceEnabled();
179         Mockito.doReturn(false).when(mockLogger).isDebugEnabled();
180         Whitebox.setInternalState(maSpy, "logger", mockLogger);
181         PowerMockito.mockStatic(Converter.class);
182         Mockito.when(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext)).thenThrow(new JsonProcessingException("ERROR") {});
183         Mockito.doNothing().when(maSpy).createProducer();
184         maSpy.post(oamContext);
185         Mockito.verify(mockLogger).error(Mockito.contains("Error generating Json from UEB message"));
186     }
187
188     @Test
189     public void testPostExceptionFlow2() throws JsonProcessingException {
190         MessageAdapter maSpy = Mockito.spy(messageAdapter);
191         OAMContext oamContext = new OAMContext();
192         oamContext.setRpcName(RPC.maintenance_mode);
193         CommonHeader mockCommonHeader = Mockito.mock(CommonHeader.class);
194         Mockito.doReturn("TEST REQUEST ID").when(mockCommonHeader).getRequestId();
195         oamContext.setCommonHeader(mockCommonHeader);
196         Status mockStatus = Mockito.mock(Status.class);
197         oamContext.setStatus(mockStatus);
198         EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
199         Mockito.doReturn(false).when(mockLogger).isTraceEnabled();
200         Mockito.doReturn(false).when(mockLogger).isDebugEnabled();
201         Whitebox.setInternalState(maSpy, "logger", mockLogger);
202         PowerMockito.mockStatic(Converter.class);
203         Mockito.when(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext)).thenThrow(new RuntimeException("ERROR"));
204         Mockito.doNothing().when(maSpy).createProducer();
205         maSpy.post(oamContext);
206         Mockito.verify(mockLogger).error(Mockito.contains("Error sending message to UEB ERROR"), Mockito.any(RuntimeException.class));
207     }
208 }