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