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