3039560af0de7ca45834812b30b8ee6c29ea1e01
[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.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30
31 import java.io.FileReader;
32 import java.io.IOException;
33
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.TemporaryFolder;
37
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);
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     }
65
66     @Test
67     public void testInvalidFileSystemConfiguration() throws IOException {
68         FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
69         try {
70             final Gson gson = new GsonBuilder().create();
71             configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"),
72                     FileSystemReceptionHandlerConfigurationParameterGroup.class);
73         } catch (final Exception e) {
74             fail("test should not thrown an exception here: " + e.getMessage());
75         }
76         final GroupValidationResult validationResult = configParameters.validate();
77         assertFalse(validationResult.isValid());
78     }
79
80     @Test
81     public void testFileSystemReceptionHandlerConfigurationParameterBuilder() {
82
83         final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
84                 new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath("/foo/bar");
85         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
86                 new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
87
88         assertEquals("/foo/bar", configParameters.getWatchPath());
89     }
90
91     @Test
92     public void testFileSystemReceptionHandlerConfigurationParameterBuilderWithInvalidPath() throws IOException {
93         final String invalidPath = tempFolder.newFile("foobar").getAbsolutePath();
94
95         final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
96                 new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath(invalidPath);
97         final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
98                 new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
99
100         final GroupValidationResult validateResult = configParameters.validate();
101         assertFalse(validateResult.isValid());
102         assertTrue(validateResult.getResult().contains("must be a valid directory"));
103     }
104 }