Merge of new rebased code
[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  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.dg.common.impl;
23
24 import org.junit.*;
25 import org.junit.runner.RunWith;
26 import org.mockito.*;
27 import org.openecomp.appc.adapter.message.EventSender;
28 import org.openecomp.appc.adapter.message.MessageDestination;
29 import org.openecomp.appc.adapter.message.event.EventMessage;
30 import org.openecomp.appc.dg.common.impl.DCAEReporterPluginImpl;
31 import org.openecomp.appc.exceptions.APPCException;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import java.util.HashMap;
41 import java.util.Map;
42
43
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest({DCAEReporterPluginImpl.class, FrameworkUtil.class})
46 public class DCAEReporterPluginImplTest {
47     private SvcLogicContext ctx;
48     private Map<String, String> params;
49
50     private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
51     private final Bundle bundleService=Mockito.mock(Bundle.class);
52     private final ServiceReference sref=Mockito.mock(ServiceReference.class);
53
54
55     private DCAEReporterPluginImpl dcaeReporterPlugin;
56     private EventSenderMock eventSender;
57
58     private String apiVer = "2.0.0";
59     private String requestId = "123";
60     private String error = "test-error";
61
62     @Before
63     public void setUp() throws NoSuchFieldException, IllegalAccessException {
64         eventSender = new EventSenderMock();
65         PowerMockito.mockStatic(FrameworkUtil.class);
66         PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
67         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
68         PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
69         PowerMockito.when(bundleContext.<EventSender>getService(sref)).thenReturn(eventSender);
70         dcaeReporterPlugin = new DCAEReporterPluginImpl();
71
72     }
73
74
75
76     @Test
77     public void testReportErrorDescriptionNullBwcModeFalse() throws Exception {
78
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
89     private void errorReasonNullAssert() throws APPCException {
90         dcaeReporterPlugin.report(params, ctx);
91         MessageDestination destination = eventSender.getDestination();
92         EventMessage msg = eventSender.getMsg();
93         Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
94         Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
95         Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason());
96         Assert.assertEquals("wrong destination", destination.name(), "DCAE");
97
98     }
99
100
101     private void positiveAssert() throws APPCException {
102         dcaeReporterPlugin.report(params, ctx);
103         MessageDestination destination = eventSender.getDestination();
104         EventMessage msg = eventSender.getMsg();
105         Assert.assertEquals("wrong API version", apiVer, msg.getEventHeader().getApiVer());
106         Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId());
107         Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason());
108         Assert.assertEquals("wrong destination", destination.name(), "DCAE");
109
110     }
111
112
113     @Test
114             public void testReportBwcFalse() throws Exception {
115                 ctx = new SvcLogicContext();
116                 params = new HashMap<>();
117                 ctx.setAttribute("isBwcMode", "false");
118                 params.put("output.status.message", error);
119                 ctx.setAttribute("input.common-header.api-ver", apiVer);
120                 ctx.setAttribute("input.common-header.request-id", requestId);
121
122                 positiveAssert();
123
124             }
125         }