Change code to use dmaap microservice
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / onap / appc / dg / common / impl / DCAEReporterPluginImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.common.impl;
27
28 import java.util.HashMap;
29 import java.util.Map;
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;
41
42
43 public class DCAEReporterPluginImplTest {
44     private SvcLogicContext ctx;
45     private Map<String, String> params;
46
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);
50
51     private DCAEReporterPluginImpl dcaeReporterPlugin;
52     private MockEventSender eventSender;
53
54     private String apiVer = "2.0.0";
55     private String requestId = "123";
56     private String error = "test-error";
57
58     @SuppressWarnings("unchecked")
59     @Before
60     public void setUp() throws NoSuchFieldException, IllegalAccessException {
61         eventSender = new MockEventSender();
62         dcaeReporterPlugin = new DCAEReporterPluginImpl();
63         dcaeReporterPlugin.setEventSender(eventSender);
64         
65     }
66
67     @Test
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);
74
75         errorReasonNullAssert();
76     }
77
78     @Test
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);
86
87         positiveAssert();
88     }
89
90     @Test
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());
101     }
102
103     @Test
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());
111     }
112
113     private void errorReasonNullAssert() throws APPCException {
114         eventSender.reset();
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");
122     }
123
124     private void positiveAssert() throws APPCException {
125         eventSender.reset();
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");
133     }
134 }