Update license header in appc-dg-common files
[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-2018 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dg.common.impl;
25
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Matchers;
32 import org.mockito.Mockito;
33 import org.mockito.Spy;
34 import org.onap.appc.adapter.message.EventSender;
35 import org.onap.appc.adapter.message.MessageDestination;
36 import org.onap.appc.adapter.message.event.EventMessage;
37 import org.onap.appc.exceptions.APPCException;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.osgi.framework.Bundle;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.osgi.framework.ServiceReference;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 import java.util.HashMap;
47 import java.util.Map;
48
49
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest({DCAEReporterPluginImpl.class, FrameworkUtil.class})
52 public class DCAEReporterPluginImplTest {
53     private SvcLogicContext ctx;
54     private Map<String, String> params;
55
56     private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
57     private final Bundle bundleService = Mockito.mock(Bundle.class);
58     private final ServiceReference sref = Mockito.mock(ServiceReference.class);
59
60     @InjectMocks
61     private DCAEReporterPluginImpl dcaeReporterPlugin;
62     @Spy
63     private EventSenderMock eventSender = new EventSenderMock();
64
65     private String apiVer = "2.0.0";
66     private String requestId = "123";
67     private String error = "test-error";
68
69     @SuppressWarnings("unchecked")
70     @Before
71     public void setUp() throws NoSuchFieldException, IllegalAccessException {
72         PowerMockito.mockStatic(FrameworkUtil.class);
73         PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
74         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
75         PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
76         PowerMockito.when(bundleContext.<EventSender>getService(sref)).thenReturn(eventSender);
77     }
78
79     @Test
80     public void testReportErrorDescriptionNullBwcModeFalse() throws Exception {
81         ctx = new SvcLogicContext();
82         params = new HashMap<>();
83         params.put("output.status.message", null);
84         ctx.setAttribute("input.common-header.api-ver", apiVer);
85         ctx.setAttribute("input.common-header.request-id", requestId);
86
87         errorReasonNullAssert();
88     }
89
90     @Test
91     public void testReportBwcFalse() throws Exception {
92         ctx = new SvcLogicContext();
93         params = new HashMap<>();
94         ctx.setAttribute("isBwcMode", "false");
95         params.put("output.status.message", error);
96         ctx.setAttribute("input.common-header.api-ver", apiVer);
97         ctx.setAttribute("input.common-header.request-id", requestId);
98
99         positiveAssert();
100     }
101
102     private void errorReasonNullAssert() throws APPCException {
103         dcaeReporterPlugin.report(params, ctx);
104         MessageDestination destination = eventSender.getDestination();
105         EventMessage msg = eventSender.getMsg();
106         Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
107         Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
108         Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason());
109         Assert.assertEquals("wrong destination", destination.name(), "DCAE");
110     }
111
112     private void positiveAssert() throws APPCException {
113         dcaeReporterPlugin.report(params, ctx);
114         MessageDestination destination = eventSender.getDestination();
115         EventMessage msg = eventSender.getMsg();
116         Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
117         Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
118         Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason());
119         Assert.assertEquals("wrong destination", destination.name(), "DCAE");
120     }
121 }