6c57a132cbb50ee4c89474bbcca76bf13482ae7b
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Intel. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.reception.handling.file;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.TemporaryFolder;
36 import org.onap.policy.common.parameters.GroupValidationResult;
37
38 /**
39  * Class to perform unit test of {@link FileSystemReceptionHandlerConfigurationParameterGroup}.
40  *
41  */
42 public class TestFileSystemReceptionHandlerConfigurationParameterGroup {
43     @Rule
44     public TemporaryFolder tempFolder = new TemporaryFolder();
45
46     @Test
47     public void testFileSystemConfiguration() throws IOException {
48         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
49         String validPath = null;
50         try {
51             validPath = tempFolder.getRoot().getAbsolutePath();
52
53             configParameters = new FileSystemReceptionHandlerConfigurationParameterGroup();
54             configParameters.setWatchPath(validPath);
55             configParameters.setMaxThread(2);
56         } catch (final Exception e) {
57             fail("test should not thrown an exception here: " + e.getMessage());
58         }
59         final GroupValidationResult validationResult = configParameters.validate();
60         assertTrue(validationResult.isValid());
61         assertEquals(validPath, configParameters.getWatchPath());
62         assertEquals(2, configParameters.getMaxThread());
63     }
64
65     @Test
66     public void testInvalidFileSystemConfiguration() throws IOException {
67         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
68         try {
69             final Gson gson = new GsonBuilder().create();
70             configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"),
71                     FileSystemReceptionHandlerConfigurationParameterGroup.class);
72         } catch (final Exception e) {
73             fail("test should not thrown an exception here: " + e.getMessage());
74         }
75         final GroupValidationResult validationResult = configParameters.validate();
76         assertFalse(validationResult.isValid());
77     }
78
79     @Test
80     public void testFileSystemReceptionHandlerConfigurationParameterBuilderWithInvalidPath() throws IOException {
81         final String invalidPath = tempFolder.newFile("foobar").getAbsolutePath();
82
83         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
84                 new FileSystemReceptionHandlerConfigurationParameterGroup();
85         configParameters.setWatchPath(invalidPath);
86
87         final GroupValidationResult validateResult = configParameters.validate();
88         assertFalse(validateResult.isValid());
89         assertTrue(validateResult.getResult().contains("must be a valid directory"));
90     }
91 }