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.onap.appc.dg.common.impl;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
32 import org.onap.appc.adapter.message.EventSender;
33 import org.onap.appc.adapter.message.MessageDestination;
34 import org.onap.appc.adapter.message.event.EventMessage;
35 import org.onap.appc.exceptions.APPCException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.osgi.framework.Bundle;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.osgi.framework.ServiceReference;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import java.util.HashMap;
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({DCAEReporterPluginImpl.class, FrameworkUtil.class})
50 public class DCAEReporterPluginImplTest {
51 private SvcLogicContext ctx;
52 private Map<String, String> params;
54 private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
55 private final Bundle bundleService = Mockito.mock(Bundle.class);
56 private final ServiceReference sref = Mockito.mock(ServiceReference.class);
59 private DCAEReporterPluginImpl dcaeReporterPlugin;
61 private EventSenderMock eventSender = new EventSenderMock();
63 private String apiVer = "2.0.0";
64 private String requestId = "123";
65 private String error = "test-error";
67 @SuppressWarnings("unchecked")
69 public void setUp() throws NoSuchFieldException, IllegalAccessException {
70 PowerMockito.mockStatic(FrameworkUtil.class);
71 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
72 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
73 PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
74 PowerMockito.when(bundleContext.<EventSender>getService(sref)).thenReturn(eventSender);
78 public void testReportErrorDescriptionNullBwcModeFalse() throws Exception {
79 ctx = new SvcLogicContext();
80 params = new HashMap<>();
81 params.put("output.status.message", null);
82 ctx.setAttribute("input.common-header.api-ver", apiVer);
83 ctx.setAttribute("input.common-header.request-id", requestId);
85 errorReasonNullAssert();
89 public void testReportBwcFalse() throws Exception {
90 ctx = new SvcLogicContext();
91 params = new HashMap<>();
92 ctx.setAttribute("isBwcMode", "false");
93 params.put("output.status.message", error);
94 ctx.setAttribute("input.common-header.api-ver", apiVer);
95 ctx.setAttribute("input.common-header.request-id", requestId);
100 private void errorReasonNullAssert() throws APPCException {
101 dcaeReporterPlugin.report(params, ctx);
102 MessageDestination destination = eventSender.getDestination();
103 EventMessage msg = eventSender.getMsg();
104 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
105 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
106 Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason());
107 Assert.assertEquals("wrong destination", destination.name(), "DCAE");
110 private void positiveAssert() throws APPCException {
111 dcaeReporterPlugin.report(params, ctx);
112 MessageDestination destination = eventSender.getDestination();
113 EventMessage msg = eventSender.getMsg();
114 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
115 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
116 Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason());
117 Assert.assertEquals("wrong destination", destination.name(), "DCAE");