2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.dg.common.impl;
28 import org.junit.runner.RunWith;
30 import org.openecomp.appc.adapter.message.EventSender;
31 import org.openecomp.appc.adapter.message.MessageDestination;
32 import org.openecomp.appc.adapter.message.event.EventMessage;
33 import org.openecomp.appc.dg.common.impl.DCAEReporterPluginImpl;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.sdnc.sli.SvcLogicContext;
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 java.util.HashMap;
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({DCAEReporterPluginImpl.class, FrameworkUtil.class})
49 public class DCAEReporterPluginImplTest {
50 private SvcLogicContext ctx;
51 private Map<String, String> params;
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);
58 private DCAEReporterPluginImpl dcaeReporterPlugin;
59 private EventSenderMock eventSender;
61 private String apiVer = "2.0.0";
62 private String requestId = "123";
63 private String error = "test-error";
66 public void setUp() throws NoSuchFieldException, IllegalAccessException {
67 eventSender = new EventSenderMock();
68 PowerMockito.mockStatic(FrameworkUtil.class);
69 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
70 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
71 PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
72 PowerMockito.when(bundleContext.<EventSender>getService(sref)).thenReturn(eventSender);
73 dcaeReporterPlugin = new DCAEReporterPluginImpl();
80 public void testReportErrorDescriptionNullBwcModeFalse() throws Exception {
82 ctx = new SvcLogicContext();
83 params = new HashMap<>();
84 params.put("output.status.message", null);
85 ctx.setAttribute("input.common-header.api-ver", apiVer);
86 ctx.setAttribute("input.common-header.request-id", requestId);
88 errorReasonNullAssert();
92 private void errorReasonNullAssert() throws APPCException {
93 dcaeReporterPlugin.report(params, ctx);
94 MessageDestination destination = eventSender.getDestination();
95 EventMessage msg = eventSender.getMsg();
96 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
97 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
98 Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason());
99 Assert.assertEquals("wrong destination", destination.name(), "DCAE");
104 private void positiveAssert() throws APPCException {
105 dcaeReporterPlugin.report(params, ctx);
106 MessageDestination destination = eventSender.getDestination();
107 EventMessage msg = eventSender.getMsg();
108 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
109 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
110 Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason());
111 Assert.assertEquals("wrong destination", destination.name(), "DCAE");
117 public void testReportBwcFalse() throws Exception {
118 ctx = new SvcLogicContext();
119 params = new HashMap<>();
120 ctx.setAttribute("isBwcMode", "false");
121 params.put("output.status.message", error);
122 ctx.setAttribute("input.common-header.api-ver", apiVer);
123 ctx.setAttribute("input.common-header.request-id", requestId);