2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.dg.common.impl;
25 import org.junit.runner.RunWith;
27 import org.openecomp.appc.adapter.dmaap.EventSender;
28 import org.openecomp.appc.adapter.dmaap.DmaapDestination;
29 import org.openecomp.appc.adapter.dmaap.event.EventMessage;
30 import org.openecomp.appc.dg.common.impl.Constants;
31 import org.openecomp.appc.dg.common.impl.DCAEReporterPluginImpl;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
34 import org.osgi.framework.Bundle;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.FrameworkUtil;
37 import org.osgi.framework.ServiceReference;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import java.util.HashMap;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({DCAEReporterPluginImpl.class, FrameworkUtil.class})
48 public class DCAEReporterPluginImplTest {
49 private SvcLogicContext ctx;
50 private Map<String, String> params;
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);
57 private DCAEReporterPluginImpl dcaeReporterPlugin;
58 private EventSenderMock eventSender;
60 private String apiVer = "2.0.0";
61 private String requestId = "123";
62 private String error = "test-error";
65 public void setUp() throws NoSuchFieldException, IllegalAccessException {
66 eventSender = new EventSenderMock();
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.getService(sref)).thenReturn(eventSender);
72 dcaeReporterPlugin = new DCAEReporterPluginImpl();
80 public void testReportBwcTrue() throws Exception {
82 ctx = new SvcLogicContext();
83 params = new HashMap<>();
84 ctx.setAttribute("isBwcMode", "true");
85 params.put(Constants.DG_ERROR_FIELD_NAME, error);
86 ctx.setAttribute(Constants.API_VERSION_FIELD_NAME, apiVer);
87 ctx.setAttribute(Constants.REQ_ID_FIELD_NAME, requestId);
93 public void testReportErrorDescriptionNullBwcModeTrue() throws Exception {
95 ctx = new SvcLogicContext();
96 params = new HashMap<>();
97 ctx.setAttribute("isBwcMode", "true");
98 params.put(Constants.DG_ERROR_FIELD_NAME, null);
99 ctx.setAttribute(Constants.API_VERSION_FIELD_NAME, apiVer);
100 ctx.setAttribute(Constants.REQ_ID_FIELD_NAME, requestId);
102 errorReasonNullAssert();
106 public void testReportErrorDescriptionNullBwcModeFalse() throws Exception {
108 ctx = new SvcLogicContext();
109 params = new HashMap<>();
110 ctx.setAttribute("isBwcMode", "false");
111 params.put("output.status.message", null);
112 ctx.setAttribute("input.common-header.api-ver", apiVer);
113 ctx.setAttribute("input.common-header.request-id", requestId);
115 errorReasonNullAssert();
119 private void errorReasonNullAssert() throws APPCException {
120 dcaeReporterPlugin.report(params, ctx);
121 DmaapDestination destination = eventSender.getDestination();
122 EventMessage msg = eventSender.getMsg();
123 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
124 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
125 Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason());
126 Assert.assertEquals("wrong destination", destination.name(), "DCAE");
131 private void positiveAssert() throws APPCException {
132 dcaeReporterPlugin.report(params, ctx);
133 DmaapDestination destination = eventSender.getDestination();
134 EventMessage msg = eventSender.getMsg();
135 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
136 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
137 Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason());
138 Assert.assertEquals("wrong destination", destination.name(), "DCAE");
144 public void testReportBwcFalse() throws Exception {
145 ctx = new SvcLogicContext();
146 params = new HashMap<>();
147 ctx.setAttribute("isBwcMode", "false");
148 params.put("output.status.message", error);
149 ctx.setAttribute("input.common-header.api-ver", apiVer);
150 ctx.setAttribute("input.common-header.request-id", requestId);