1d32b1918303eac781fc0ce2b7ea15568cfc6007
[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
32 import java.io.FileReader;
33 import java.io.IOException;
34
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38 import org.onap.policy.common.parameters.GroupValidationResult;
39
40 /**
41  * Class to perform unit test of {@link FileSystemReceptionHandlerConfigurationParameterGroup}.
42  *
43  */
44 public class TestFileSystemReceptionHandlerConfigurationParameterGroup {
45     @Rule
46     public TemporaryFolder tempFolder = new TemporaryFolder();
47
48     @Test
49     public void testFileSystemConfiguration() throws IOException {
50         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
51         String validPath = null;
52         try {
53             validPath = tempFolder.getRoot().getAbsolutePath();
54
55             final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
56                 new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath(validPath).setMaxThread(2);
57             configParameters = new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
58         } catch (final Exception e) {
59             fail("test should not thrown an exception here: " + e.getMessage());
60         }
61         final GroupValidationResult validationResult = configParameters.validate();
62         assertTrue(validationResult.isValid());
63         assertEquals(validPath, configParameters.getWatchPath());
64         assertEquals(2, configParameters.getMaxThread());
65     }
66
67     @Test
68     public void testInvalidFileSystemConfiguration() throws IOException {
69         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
70         try {
71             final Gson gson = new GsonBuilder().create();
72             configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"),
73                     FileSystemReceptionHandlerConfigurationParameterGroup.class);
74         } catch (final Exception e) {
75             fail("test should not thrown an exception here: " + e.getMessage());
76         }
77         final GroupValidationResult validationResult = configParameters.validate();
78         assertFalse(validationResult.isValid());
79     }
80
81     @Test
82     public void testFileSystemReceptionHandlerConfigurationParameterBuilder() {
83
84         final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
85                 new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath("/foo/bar");
86         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
87                 new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
88
89         assertEquals("/foo/bar", configParameters.getWatchPath());
90     }
91
92     @Test
93     public void testFileSystemReceptionHandlerConfigurationParameterBuilderWithInvalidPath() throws IOException {
94         final String invalidPath = tempFolder.newFile("foobar").getAbsolutePath();
95
96         final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
97                 new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath(invalidPath);
98         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
99                 new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
100
101         final GroupValidationResult validateResult = configParameters.validate();
102         assertFalse(validateResult.isValid());
103         assertTrue(validateResult.getResult().contains("must be a valid directory"));
104     }
105 }