c876f99c74a2f435ce392834476e93ca01440fab
[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.sdc.api.results.IDistributionClientResult;
46 import org.onap.sdc.impl.mock.DistributionClientStubImpl;
47 import org.onap.sdc.utils.DistributionActionResultEnum;
48
49 /**
50  * Class to perform unit test of {@link SdcReceptionHandler}.
51  *
52  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
53  */
54 @RunWith(MockitoJUnitRunner.class)
55 public class TestSdcReceptionHandler {
56
57     private static final Logger LOGGER = FlexLogger.getLogger(TestSdcReceptionHandler.class);
58
59     @Mock
60     private IDistributionClientResult successfulClientInitResult;
61     @Mock
62     private IDistributionClientResult failureClientInitResult;
63     @Mock
64     private DistributionClientStubImpl distributionClient;
65
66     private SdcReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
67     private SdcReceptionHandler sypHandler;
68
69     /**
70      * Setup for the test cases.
71      *
72      * @throws IOException if it occurs
73      */
74     @Before
75     public final void init() throws IOException {
76         final Gson gson = new GsonBuilder().create();
77         pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"),
78                 SdcReceptionHandlerConfigurationParameterGroup.class);
79         ParameterService.register(pssdConfigParameters);
80         final SdcReceptionHandler sdcHandler = new SdcReceptionHandler();
81         sypHandler = Mockito.spy(sdcHandler);
82         Mockito.when(sypHandler.createSdcDistributionClient()).thenReturn(distributionClient);
83         Mockito.when(distributionClient.init(any(), any())).thenReturn(successfulClientInitResult);
84         Mockito.when(distributionClient.start()).thenReturn(successfulClientInitResult);
85         Mockito.when(distributionClient.stop()).thenReturn(successfulClientInitResult);
86         Mockito.when(successfulClientInitResult.getDistributionActionResult())
87                 .thenReturn(DistributionActionResultEnum.SUCCESS);
88     }
89
90     @After
91     public void teardown() {
92         ParameterService.deregister(pssdConfigParameters);
93     }
94
95     @Test
96     public final void testInitializeSdcClient() {
97         try {
98             sypHandler.initializeReception(pssdConfigParameters.getName());
99         } catch (final PluginInitializationException exp) {
100             LOGGER.error(exp);
101             fail("Test should not throw any exception");
102         }
103     }
104
105     @Test
106     public final void testInitializeSdcClient_Again() throws PluginInitializationException {
107         sypHandler.initializeReception(pssdConfigParameters.getName());
108         try {
109             sypHandler.initializeReception(pssdConfigParameters.getName());
110             fail("Test must throw an exception here");
111         } catch (final Exception exp) {
112             assertTrue(exp.getMessage().startsWith("The SDC Client is already initialized"));
113         }
114     }
115
116     @Test
117     public final void testInitializeSdcClient_Failure() throws PluginInitializationException {
118
119         Mockito.when(successfulClientInitResult.getDistributionActionResult())
120                 .thenReturn(DistributionActionResultEnum.FAIL);
121         try {
122             sypHandler.initializeReception(pssdConfigParameters.getName());
123             fail("Test must throw an exception here");
124         } catch (final Exception exp) {
125             assertTrue(exp.getMessage().startsWith("SDC client initialization failed with reason"));
126         }
127     }
128
129     @Test
130     public final void testStartSdcClient_Failure() throws PluginInitializationException {
131         try {
132             Mockito.when(distributionClient.start()).thenReturn(failureClientInitResult);
133             Mockito.when(failureClientInitResult.getDistributionActionResult())
134                     .thenReturn(DistributionActionResultEnum.FAIL);
135             sypHandler.initializeReception(pssdConfigParameters.getName());
136
137             fail("Test must throw an exception here");
138         } catch (final Exception exp) {
139             assertTrue(exp.getMessage().startsWith("SDC client start failed with reason"));
140         }
141     }
142
143     @Test
144     public final void testStopSdcClient() {
145         try {
146             sypHandler.initializeReception(pssdConfigParameters.getName());
147             sypHandler.destroy();
148         } catch (final PluginInitializationException | PluginTerminationException exp) {
149             LOGGER.error(exp);
150             fail("Test should not throw any exception");
151         }
152
153     }
154
155     @Test
156     public final void testStopSdcClientWithoutStart() {
157         try {
158             sypHandler.destroy();
159         } catch (final PluginTerminationException exp) {
160             LOGGER.error(exp);
161             fail("Test should not throw any exception");
162         }
163
164     }
165
166     @Test
167     public final void testStopSdcClient_Failure() throws PluginInitializationException {
168
169         sypHandler.initializeReception(pssdConfigParameters.getName());
170         Mockito.when(successfulClientInitResult.getDistributionActionResult())
171                 .thenReturn(DistributionActionResultEnum.FAIL);
172         try {
173             sypHandler.destroy();
174             fail("Test must throw an exception here");
175         } catch (final Exception exp) {
176             assertTrue(exp.getMessage().startsWith("SDC client stop failed with reason"));
177         }
178     }
179 }