2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.dg.common.impl;
28 import java.util.HashMap;
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;
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest(FrameworkUtil.class)
54 public class DCAEReporterPluginImplTest {
55 private SvcLogicContext ctx;
56 private Map<String, String> params;
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);
63 private DCAEReporterPluginImpl dcaeReporterPlugin;
65 private EventSenderMock eventSender = new EventSenderMock();
67 private String apiVer = "2.0.0";
68 private String requestId = "123";
69 private String error = "test-error";
71 @SuppressWarnings("unchecked")
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);
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);
89 errorReasonNullAssert();
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);
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());
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());
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");
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");