8edca74e5790b6ff53498190b9a36479ee3b543f
[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.fail;
24
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.runners.MockitoJUnitRunner;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.common.parameters.ParameterService;
45 import org.onap.policy.distribution.forwarding.PolicyForwarder;
46 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
47 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
48
49 /**
50  * Class to perform unit test of {@link FileSystemReceptionHandler}.
51  */
52 @RunWith(MockitoJUnitRunner.class)
53 public class TestFileSystemReceptionHandler {
54
55     private static final Logger LOGGER = FlexLogger.getLogger(TestFileSystemReceptionHandler.class);
56     private static final String DUMMY_SERVICE_CSAR = "dummyService.csar";
57
58     @Rule
59     public TemporaryFolder tempFolder = new TemporaryFolder();
60
61     private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
62     private FileSystemReceptionHandler fileSystemHandler;
63
64
65     /**
66      * Setup for the test cases.
67      *
68      * @throws IOException if it occurs
69      * @throws SecurityException if it occurs
70      * @throws NoSuchFieldException if it occurs
71      * @throws IllegalAccessException if it occurs
72      * @throws IllegalArgumentException if it occurs
73      */
74     @Before
75     public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
76             IllegalAccessException {
77         DistributionStatisticsManager.resetAllStatistics();
78
79         final Gson gson = new GsonBuilder().create();
80         String json = "{ \"name\": \"parameterConfig9\", \"watchPath\": \"";
81         json += tempFolder.getRoot().getAbsolutePath() + "\"}";
82         pssdConfigParameters = gson.fromJson(json,
83                 FileSystemReceptionHandlerConfigurationParameterGroup.class);
84         ParameterService.register(pssdConfigParameters);
85         fileSystemHandler = new FileSystemReceptionHandler();
86     }
87
88     @After
89     public void teardown() {
90         ParameterService.deregister(pssdConfigParameters);
91     }
92     
93     @Test
94     public final void testInit() {
95         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
96         Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class));
97         sypHandler.initializeReception(pssdConfigParameters.getName());
98         Mockito.verify(sypHandler, Mockito.times(1)).main(Mockito.isA(String.class));
99     }
100
101     @Test
102     public final void testDestroy() {
103         try {
104             final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
105             Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class));
106             sypHandler.initializeReception(pssdConfigParameters.getName());
107             sypHandler.destroy();
108         } catch (final Exception exp) {
109             LOGGER.error(exp);
110             fail("Test should not throw any exception");
111         }
112
113     }
114
115     @Test
116     public void testMain() throws IOException, PolicyDecodingException {
117
118         final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
119         Mockito.doNothing().when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
120
121         final String watchPath = tempFolder.getRoot().getAbsolutePath().toString();
122         Thread th = new Thread(() -> {
123             sypHandler.main(watchPath);
124         });
125
126         th.start();
127         try {
128             // yield to main thread
129             Thread.sleep(1000);
130             Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
131                 Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
132             // wait enough time 
133             Thread.sleep(1000);
134             sypHandler.destroy();
135             th.interrupt();
136             th.join();
137         } catch (final InterruptedException ex) {
138             LOGGER.error(ex);
139         }
140         Mockito.verify(sypHandler, Mockito.times(1))
141             .createPolicyInputAndCallHandler(Mockito.isA(String.class));
142
143     }
144 }
145