2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.mockito.Mockito;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.appc.srvcomm.messaging.MessageDestination;
36 import org.onap.appc.srvcomm.messaging.event.EventMessage;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.osgi.framework.Bundle;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceReference;
43 public class DCAEReporterPluginImplTest {
44 private SvcLogicContext ctx;
45 private Map<String, String> params;
47 private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
48 private final Bundle bundleService = Mockito.mock(Bundle.class);
49 private final ServiceReference sref = Mockito.mock(ServiceReference.class);
51 private DCAEReporterPluginImpl dcaeReporterPlugin;
52 private MockEventSender eventSender;
54 private String apiVer = "2.0.0";
55 private String requestId = "123";
56 private String error = "test-error";
58 @SuppressWarnings("unchecked")
60 public void setUp() throws NoSuchFieldException, IllegalAccessException {
61 eventSender = new MockEventSender();
62 dcaeReporterPlugin = new DCAEReporterPluginImpl();
63 dcaeReporterPlugin.setEventSender(eventSender);
68 public void testReportErrorDescriptionNullBwcModeFalse() throws Exception {
69 ctx = new SvcLogicContext();
70 params = new HashMap<>();
71 params.put("output.status.message", null);
72 ctx.setAttribute("input.common-header.api-ver", apiVer);
73 ctx.setAttribute("input.common-header.request-id", requestId);
75 errorReasonNullAssert();
79 public void testReportBwcFalse() throws Exception {
80 ctx = new SvcLogicContext();
81 params = new HashMap<>();
82 ctx.setAttribute("isBwcMode", "false");
83 params.put("output.status.message", error);
84 ctx.setAttribute("input.common-header.api-ver", apiVer);
85 ctx.setAttribute("input.common-header.request-id", requestId);
91 public void testReportErrorDefinition() throws APPCException {
92 ctx = new SvcLogicContext();
93 params = new HashMap<>();
94 params.put(Constants.EVENT_MESSAGE, "ERROR DESCRIPTION");
95 params.put(Constants.DG_ERROR_CODE, "200");
96 ctx.setAttribute("input.common-header.api-ver", apiVer);
97 ctx.setAttribute("input.common-header.request-id", requestId);
98 dcaeReporterPlugin.report(params, ctx);
99 EventMessage msg = eventSender.getMessage();
100 Assert.assertEquals("ERROR DESCRIPTION", msg.getEventStatus().getReason());
104 public void testReportSuccess() throws APPCException {
105 ctx = new SvcLogicContext();
106 params = new HashMap<>();
107 params.put(Constants.DG_ERROR_CODE, "200");
108 dcaeReporterPlugin.reportSuccess(params, ctx);
109 EventMessage msg = eventSender.getMessage();
110 Assert.assertEquals(new Integer(500), msg.getEventStatus().getCode());
113 private void errorReasonNullAssert() throws APPCException {
115 dcaeReporterPlugin.report(params, ctx);
116 MessageDestination destination = eventSender.getDestination();
117 EventMessage msg = eventSender.getMessage();
118 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
119 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
120 Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason());
121 Assert.assertEquals("wrong destination", destination.name(), "DCAE");
124 private void positiveAssert() throws APPCException {
126 dcaeReporterPlugin.report(params, ctx);
127 MessageDestination destination = eventSender.getDestination();
128 EventMessage msg = eventSender.getMessage();
129 Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
130 Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
131 Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason());
132 Assert.assertEquals("wrong destination", destination.name(), "DCAE");