02b83849266ae3c9ec8d03f8128cb76fb437e784
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Intel. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling.sdc;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Matchers.any;
26
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29
30 import java.io.FileReader;
31 import java.io.IOException;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.common.parameters.ParameterService;
43 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
44 import org.onap.policy.distribution.reception.decoding.PluginTerminationException;
45 import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup;
46 import org.onap.sdc.api.results.IDistributionClientResult;
47 import org.onap.sdc.impl.mock.DistributionClientStubImpl;
48 import org.onap.sdc.utils.DistributionActionResultEnum;
49
50 /**
51  * Class to perform unit test of {@link SdcReceptionHandler}.
52  *
53  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
54  */
55 @RunWith(MockitoJUnitRunner.class)
56 public class TestSdcReceptionHandler {
57
58     private static final Logger LOGGER = FlexLogger.getLogger(TestSdcReceptionHandler.class);
59
60     @Mock
61     private IDistributionClientResult successfulClientInitResult;
62     @Mock
63     private IDistributionClientResult failureClientInitResult;
64     @Mock
65     private DistributionClientStubImpl distributionClient;
66
67     private PssdConfigurationParametersGroup pssdConfigParameters;
68     private SdcReceptionHandler sypHandler;
69
70     /**
71      * Setup for the test cases.
72      *
73      * @throws IOException if it occurs
74      */
75     @Before
76     public final void init() throws IOException {
77         final Gson gson = new GsonBuilder().create();
78         pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"),
79                 PssdConfigurationParametersGroup.class);
80         ParameterService.register(pssdConfigParameters);
81         final SdcReceptionHandler sdcHandler = new SdcReceptionHandler();
82         sypHandler = Mockito.spy(sdcHandler);
83         Mockito.when(sypHandler.createSdcDistributionClient()).thenReturn(distributionClient);
84         Mockito.when(distributionClient.init(any(), any())).thenReturn(successfulClientInitResult);
85         Mockito.when(distributionClient.start()).thenReturn(successfulClientInitResult);
86         Mockito.when(distributionClient.stop()).thenReturn(successfulClientInitResult);
87         Mockito.when(successfulClientInitResult.getDistributionActionResult())
88                 .thenReturn(DistributionActionResultEnum.SUCCESS);
89     }
90
91     @After
92     public void teardown() {
93         ParameterService.deregister(pssdConfigParameters);
94     }
95
96     @Test
97     public final void testInitializeSdcClient() {
98         try {
99             sypHandler.initializeReception(pssdConfigParameters.getName());
100         } catch (final PluginInitializationException exp) {
101             LOGGER.error(exp);
102             fail("Test should not throw any exception");
103         }
104     }
105
106     @Test
107     public final void testInitializeSdcClient_Again() throws PluginInitializationException {
108         sypHandler.initializeReception(pssdConfigParameters.getName());
109         try {
110             sypHandler.initializeReception(pssdConfigParameters.getName());
111             fail("Test must throw an exception here");
112         } catch (final Exception exp) {
113             assertTrue(exp.getMessage().startsWith("The SDC Client is already initialized"));
114         }
115     }
116
117     @Test
118     public final void testInitializeSdcClient_Failure() throws PluginInitializationException {
119
120         Mockito.when(successfulClientInitResult.getDistributionActionResult())
121                 .thenReturn(DistributionActionResultEnum.FAIL);
122         try {
123             sypHandler.initializeReception(pssdConfigParameters.getName());
124             fail("Test must throw an exception here");
125         } catch (final Exception exp) {
126             assertTrue(exp.getMessage().startsWith("SDC client initialization failed with reason"));
127         }
128     }
129
130     @Test
131     public final void testStartSdcClient_Failure() throws PluginInitializationException {
132         try {
133             Mockito.when(distributionClient.start()).thenReturn(failureClientInitResult);
134             Mockito.when(failureClientInitResult.getDistributionActionResult())
135                     .thenReturn(DistributionActionResultEnum.FAIL);
136             sypHandler.initializeReception(pssdConfigParameters.getName());
137
138             fail("Test must throw an exception here");
139         } catch (final Exception exp) {
140             assertTrue(exp.getMessage().startsWith("SDC client start failed with reason"));
141         }
142     }
143
144     @Test
145     public final void testStopSdcClient() {
146         try {
147             sypHandler.initializeReception(pssdConfigParameters.getName());
148             sypHandler.destroy();
149         } catch (final PluginInitializationException | PluginTerminationException exp) {
150             LOGGER.error(exp);
151             fail("Test should not throw any exception");
152         }
153
154     }
155
156     @Test
157     public final void testStopSdcClientWithoutStart() {
158         try {
159             sypHandler.destroy();
160         } catch (final PluginTerminationException exp) {
161             LOGGER.error(exp);
162             fail("Test should not throw any exception");
163         }
164
165     }
166
167     @Test
168     public final void testStopSdcClient_Failure() throws PluginInitializationException {
169
170         sypHandler.initializeReception(pssdConfigParameters.getName());
171         Mockito.when(successfulClientInitResult.getDistributionActionResult())
172                 .thenReturn(DistributionActionResultEnum.FAIL);
173         try {
174             sypHandler.destroy();
175             fail("Test must throw an exception here");
176         } catch (final Exception exp) {
177             assertTrue(exp.getMessage().startsWith("SDC client stop failed with reason"));
178         }
179     }
180 }