Feature for micro service communication
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / onap / appc / dg / common / impl / IntermediateMessageSenderImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.dg.common.impl;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Matchers;
32 import org.mockito.Mockito;
33 import org.mockito.Spy;
34 import org.onap.appc.adapter.message.MessageAdapterFactory;
35 import org.onap.appc.adapter.message.Producer;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.osgi.framework.Bundle;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.osgi.framework.ServiceReference;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest(FrameworkUtil.class)
47 public class IntermediateMessageSenderImplTest {
48
49     private SvcLogicContext ctx;
50     private Map<String, String> params;
51
52     private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
53     private final Bundle bundleService = Mockito.mock(Bundle.class);
54     private final ServiceReference sref = Mockito.mock(ServiceReference.class);
55     private final MessageAdapterFactory mockFactory = Mockito.mock(MessageAdapterFactory.class);
56     private final Producer mockProducer = Mockito.mock(Producer.class);
57
58     @InjectMocks
59     private IntermediateMessageSenderImpl intermediateMessageSenderImpl;
60
61     @Spy
62     private EventSenderMock eventSender = new EventSenderMock();
63
64     @SuppressWarnings("unchecked")
65     @Before
66     public void setUp() throws NoSuchFieldException, IllegalAccessException {
67         PowerMockito.mockStatic(FrameworkUtil.class);
68         PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
69         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
70         PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
71         PowerMockito.when(bundleContext.<MessageAdapterFactory>getService(sref)).thenReturn(mockFactory);
72         PowerMockito.when(mockFactory.createProducer(Matchers.anyCollection(), Mockito.anyString(), Mockito.anyString(),
73                 Mockito.anyString())).thenReturn(mockProducer);
74     }
75
76     @Test
77     public void testSendEmptyMessage() {
78         intermediateMessageSenderImpl.init();
79         ctx = new SvcLogicContext();
80         params = new HashMap<>();
81         intermediateMessageSenderImpl.sendMessage(params, ctx);
82         Assert.assertEquals("FAILURE", ctx.getAttribute("STATUS"));
83     }
84
85     @Test
86     public void testSendMessage() {
87         intermediateMessageSenderImpl.init();
88         ctx = new SvcLogicContext();
89         ctx.setAttribute("input.common-header.request-id", "REQUEST-ID");
90         params = new HashMap<>();
91         params.put("message", "TEST MESSAGE");
92         params.put("code", "TEST CODE");
93         intermediateMessageSenderImpl.sendMessage(params, ctx);
94         Assert.assertEquals("SUCCESS", ctx.getAttribute("STATUS"));
95     }
96 }