95f7ba7559a81b759628fa5b516ed392f5f231a6
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / onap / appc / dg / common / impl / DCAEReporterPluginImplTest.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.dg.common.impl;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Matchers;
36 import org.mockito.Mockito;
37 import org.mockito.Spy;
38 import org.onap.appc.adapter.message.EventSender;
39 import org.onap.appc.adapter.message.MessageDestination;
40 import org.onap.appc.adapter.message.event.EventMessage;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.osgi.framework.Bundle;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.FrameworkUtil;
46 import org.osgi.framework.ServiceReference;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50
51
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest(FrameworkUtil.class)
54 public class DCAEReporterPluginImplTest {
55     private SvcLogicContext ctx;
56     private Map<String, String> params;
57
58     private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
59     private final Bundle bundleService = Mockito.mock(Bundle.class);
60     private final ServiceReference sref = Mockito.mock(ServiceReference.class);
61
62     @InjectMocks
63     private DCAEReporterPluginImpl dcaeReporterPlugin;
64     @Spy
65     private EventSenderMock eventSender = new EventSenderMock();
66
67     private String apiVer = "2.0.0";
68     private String requestId = "123";
69     private String error = "test-error";
70
71     @SuppressWarnings("unchecked")
72     @Before
73     public void setUp() throws NoSuchFieldException, IllegalAccessException {
74         PowerMockito.mockStatic(FrameworkUtil.class);
75         PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
76         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
77         PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
78         PowerMockito.when(bundleContext.<EventSender>getService(sref)).thenReturn(eventSender);
79     }
80
81     @Test
82     public void testReportErrorDescriptionNullBwcModeFalse() throws Exception {
83         ctx = new SvcLogicContext();
84         params = new HashMap<>();
85         params.put("output.status.message", null);
86         ctx.setAttribute("input.common-header.api-ver", apiVer);
87         ctx.setAttribute("input.common-header.request-id", requestId);
88
89         errorReasonNullAssert();
90     }
91
92     @Test
93     public void testReportBwcFalse() throws Exception {
94         ctx = new SvcLogicContext();
95         params = new HashMap<>();
96         ctx.setAttribute("isBwcMode", "false");
97         params.put("output.status.message", error);
98         ctx.setAttribute("input.common-header.api-ver", apiVer);
99         ctx.setAttribute("input.common-header.request-id", requestId);
100
101         positiveAssert();
102     }
103
104     @Test
105     public void testReportErrorDefinition() throws APPCException {
106         ctx = new SvcLogicContext();
107         params = new HashMap<>();
108         params.put(Constants.EVENT_MESSAGE, "ERROR DESCRIPTION");
109         params.put(Constants.DG_ERROR_CODE, "200");
110         ctx.setAttribute("input.common-header.api-ver", apiVer);
111         ctx.setAttribute("input.common-header.request-id", requestId);
112         dcaeReporterPlugin.report(params, ctx);
113         EventMessage msg = eventSender.getMsg();
114         Assert.assertEquals("ERROR DESCRIPTION", msg.getEventStatus().getReason());
115     }
116
117     @Test
118     public void testReportSuccess() throws APPCException {
119         ctx = new SvcLogicContext();
120         params = new HashMap<>();
121         params.put(Constants.DG_ERROR_CODE, "200");
122         dcaeReporterPlugin.reportSuccess(params, ctx);
123         EventMessage msg = eventSender.getMsg();
124         Assert.assertEquals(new Integer(500), msg.getEventStatus().getCode());
125     }
126
127     private void errorReasonNullAssert() throws APPCException {
128         dcaeReporterPlugin.report(params, ctx);
129         MessageDestination destination = eventSender.getDestination();
130         EventMessage msg = eventSender.getMsg();
131         Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
132         Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
133         Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason());
134         Assert.assertEquals("wrong destination", destination.name(), "DCAE");
135     }
136
137     private void positiveAssert() throws APPCException {
138         dcaeReporterPlugin.report(params, ctx);
139         MessageDestination destination = eventSender.getDestination();
140         EventMessage msg = eventSender.getMsg();
141         Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
142         Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
143         Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason());
144         Assert.assertEquals("wrong destination", destination.name(), "DCAE");
145     }
146 }