Test coverage in MessageAdapterImpl
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / test / java / org / onap / appc / messageadapter / impl / MessageAdapterImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 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.messageadapter.impl;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mockito;
30 import org.mockito.internal.util.reflection.Whitebox;
31 import org.onap.appc.adapter.message.MessageAdapterFactory;
32 import org.onap.appc.adapter.message.Producer;
33 import org.onap.appc.configuration.Configuration;
34 import org.onap.appc.configuration.ConfigurationFactory;
35 import org.onap.appc.requesthandler.conv.Converter;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.att.eelf.configuration.EELFLogger.Level;
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({FrameworkUtil.class, Converter.class})
50 public class MessageAdapterImplTest {
51
52     private Configuration mockConfig = ConfigurationFactory.getConfiguration();
53     private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
54     private final Bundle bundleService = Mockito.mock(Bundle.class);
55     private final ServiceReference sref = Mockito.mock(ServiceReference.class);
56     private final Producer producer = Mockito.mock(Producer.class);
57     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterImpl.class);
58     private MessageAdapterImpl impl;
59
60     @Before
61     public void setUp() throws Exception {
62         logger.setLevel(Level.TRACE);
63         impl = PowerMockito.spy(new MessageAdapterImpl());
64         //originalLogger = (EELFLogger) Whitebox.getInternalState(MessageAdapterImpl.class, "logger");
65         Whitebox.setInternalState(impl, "configuration", mockConfig);
66         PowerMockito.mockStatic(FrameworkUtil.class);
67         PowerMockito.when(FrameworkUtil.getBundle(MessageAdapterImpl.class)).thenReturn(bundleService);
68         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
69         PowerMockito.when(bundleContext.getServiceReference(MessageAdapterFactory.class.getName())).thenReturn(sref);
70         PowerMockito.when(bundleContext.getService(sref)).thenReturn(producer);
71         PowerMockito.mockStatic(Converter.class);
72         PowerMockito.when(Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(
73                 Mockito.any(), Mockito.any(), Mockito.any())).thenReturn("{}");
74     }
75
76     @Test
77     public void testSuccess() throws JsonProcessingException {
78         PowerMockito.when(Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(
79                 Mockito.any(), Mockito.any(), Mockito.any())).thenReturn("{}");
80         Mockito.when(producer.post(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
81         Whitebox.setInternalState(impl, "producer", producer);
82         Whitebox.setInternalState(impl, "partition", "PARTITION");
83         assertTrue(impl.post(null, null, null));
84     }
85
86     @Test
87     public void testJsonException() throws JsonProcessingException {
88         PowerMockito.when(Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(
89                 Mockito.any(), Mockito.any(), Mockito.any())).thenThrow(new JsonProcessingException("TEST") {});
90         Whitebox.setInternalState(impl, "partition", "PARTITION");
91         assertFalse(impl.post(null, null, null));
92     }
93
94     @Test
95     public void testException() throws JsonProcessingException {
96         PowerMockito.when(Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(
97                 Mockito.any(), Mockito.any(), Mockito.any())).thenThrow(new RuntimeException());
98         Whitebox.setInternalState(impl, "partition", "PARTITION");
99         assertFalse(impl.post(null, null, null));
100     }
101 }