78a11fb48bc7eb2ba967c6c088353d671cf397c5
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / openecomp / appc / dg / common / impl / DCAEReporterPluginImplTest.java
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.openecomp.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.*;
32 import org.openecomp.appc.adapter.message.EventSender;
33 import org.openecomp.appc.adapter.message.MessageDestination;
34 import org.openecomp.appc.adapter.message.event.EventMessage;
35 import org.openecomp.appc.exceptions.APPCException;
36 import org.openecomp.sdnc.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;
45 import java.util.Map;
46
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({DCAEReporterPluginImpl.class, FrameworkUtil.class})
50 public class DCAEReporterPluginImplTest {
51     private SvcLogicContext ctx;
52     private Map<String, String> params;
53
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);
57
58     @InjectMocks
59     private DCAEReporterPluginImpl dcaeReporterPlugin;
60     @Spy
61     private EventSenderMock eventSender = new EventSenderMock();
62
63     private String apiVer = "2.0.0";
64     private String requestId = "123";
65     private String error = "test-error";
66
67     @SuppressWarnings("unchecked")
68     @Before
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);
75     }
76
77     @Test
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);
84
85         errorReasonNullAssert();
86     }
87
88     @Test
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);
96
97         positiveAssert();
98     }
99
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");
108     }
109
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");
118     }
119 }