aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/reception-plugins/src/test
diff options
context:
space:
mode:
authorLianhao Lu <lianhao.lu@intel.com>2018-11-09 14:57:30 +0800
committerLianhao Lu <lianhao.lu@intel.com>2018-11-12 15:55:37 +0800
commit70f9f24e6ed39a6ddb4afef02516af624f50079d (patch)
treecf1ecb8bff087d757f41bb31f6921dd6f17dc63c /plugins/reception-plugins/src/test
parent789f09f2430440d6be44a5fad26715d002aff757 (diff)
Make FileSystemReceptionHandler more tolerant
Make FileSystemReceptionHandler tolerant of exceptions thrown when parsing tosca template and be able to handle new incoming csar. Also added the unit test for FileSystemReceptionHandler. Change-Id: I0f1647f6f952576a8e61adca4d027990706d1411 Issue-ID: POLICY-837 Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
Diffstat (limited to 'plugins/reception-plugins/src/test')
-rw-r--r--plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java145
-rw-r--r--plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandlerConfigurationParameterGroup.java102
-rw-r--r--plugins/reception-plugins/src/test/resources/handling-filesystem.json5
-rw-r--r--plugins/reception-plugins/src/test/resources/handling-filesystemInvalid.json4
4 files changed, 256 insertions, 0 deletions
diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java
new file mode 100644
index 00000000..8edca74e
--- /dev/null
+++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java
@@ -0,0 +1,145 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Intel. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.distribution.reception.handling.sdc;
+
+import static org.junit.Assert.fail;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.distribution.forwarding.PolicyForwarder;
+import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
+import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
+
+/**
+ * Class to perform unit test of {@link FileSystemReceptionHandler}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class TestFileSystemReceptionHandler {
+
+ private static final Logger LOGGER = FlexLogger.getLogger(TestFileSystemReceptionHandler.class);
+ private static final String DUMMY_SERVICE_CSAR = "dummyService.csar";
+
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ private FileSystemReceptionHandlerConfigurationParameterGroup pssdConfigParameters;
+ private FileSystemReceptionHandler fileSystemHandler;
+
+
+ /**
+ * Setup for the test cases.
+ *
+ * @throws IOException if it occurs
+ * @throws SecurityException if it occurs
+ * @throws NoSuchFieldException if it occurs
+ * @throws IllegalAccessException if it occurs
+ * @throws IllegalArgumentException if it occurs
+ */
+ @Before
+ public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
+ IllegalAccessException {
+ DistributionStatisticsManager.resetAllStatistics();
+
+ final Gson gson = new GsonBuilder().create();
+ String json = "{ \"name\": \"parameterConfig9\", \"watchPath\": \"";
+ json += tempFolder.getRoot().getAbsolutePath() + "\"}";
+ pssdConfigParameters = gson.fromJson(json,
+ FileSystemReceptionHandlerConfigurationParameterGroup.class);
+ ParameterService.register(pssdConfigParameters);
+ fileSystemHandler = new FileSystemReceptionHandler();
+ }
+
+ @After
+ public void teardown() {
+ ParameterService.deregister(pssdConfigParameters);
+ }
+
+ @Test
+ public final void testInit() {
+ final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
+ Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class));
+ sypHandler.initializeReception(pssdConfigParameters.getName());
+ Mockito.verify(sypHandler, Mockito.times(1)).main(Mockito.isA(String.class));
+ }
+
+ @Test
+ public final void testDestroy() {
+ try {
+ final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
+ Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class));
+ sypHandler.initializeReception(pssdConfigParameters.getName());
+ sypHandler.destroy();
+ } catch (final Exception exp) {
+ LOGGER.error(exp);
+ fail("Test should not throw any exception");
+ }
+
+ }
+
+ @Test
+ public void testMain() throws IOException, PolicyDecodingException {
+
+ final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler);
+ Mockito.doNothing().when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class));
+
+ final String watchPath = tempFolder.getRoot().getAbsolutePath().toString();
+ Thread th = new Thread(() -> {
+ sypHandler.main(watchPath);
+ });
+
+ th.start();
+ try {
+ // yield to main thread
+ Thread.sleep(1000);
+ Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"),
+ Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar"));
+ // wait enough time
+ Thread.sleep(1000);
+ sypHandler.destroy();
+ th.interrupt();
+ th.join();
+ } catch (final InterruptedException ex) {
+ LOGGER.error(ex);
+ }
+ Mockito.verify(sypHandler, Mockito.times(1))
+ .createPolicyInputAndCallHandler(Mockito.isA(String.class));
+
+ }
+}
+
diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandlerConfigurationParameterGroup.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandlerConfigurationParameterGroup.java
new file mode 100644
index 00000000..b4b905c5
--- /dev/null
+++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandlerConfigurationParameterGroup.java
@@ -0,0 +1,102 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Intel. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.distribution.reception.handling.sdc;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import org.onap.policy.common.parameters.GroupValidationResult;
+
+/**
+ * Class to perform unit test of {@link FileSystemReceptionHandlerConfigurationParameterGroup}.
+ *
+ */
+public class TestFileSystemReceptionHandlerConfigurationParameterGroup {
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @Test
+ public void testFileSystemConfiguration() throws IOException {
+ FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
+ try {
+ final Gson gson = new GsonBuilder().create();
+ configParameters = gson.fromJson(new FileReader("src/test/resources/handling-filesystem.json"),
+ FileSystemReceptionHandlerConfigurationParameterGroup.class);
+ } catch (final Exception e) {
+ fail("test should not thrown an exception here: " + e.getMessage());
+ }
+ final GroupValidationResult validationResult = configParameters.validate();
+ assertTrue(validationResult.isValid());
+ assertEquals("/tmp", configParameters.getWatchPath());
+ }
+
+ @Test
+ public void testInvalidFileSystemConfiguration() throws IOException {
+ FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null;
+ try {
+ final Gson gson = new GsonBuilder().create();
+ configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"),
+ FileSystemReceptionHandlerConfigurationParameterGroup.class);
+ } catch (final Exception e) {
+ fail("test should not thrown an exception here: " + e.getMessage());
+ }
+ final GroupValidationResult validationResult = configParameters.validate();
+ assertFalse(validationResult.isValid());
+ }
+
+ @Test
+ public void testFileSystemReceptionHandlerConfigurationParameterBuilder() {
+
+ final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
+ new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath("/foo/bar");
+ final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
+ new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
+
+ assertEquals("/foo/bar", configParameters.getWatchPath());
+ }
+
+ @Test
+ public void testFileSystemReceptionHandlerConfigurationParameterBuilderWithInvalidPath() throws IOException {
+ final String invalidPath = tempFolder.newFile("foobar").getAbsolutePath();
+
+ final FileSystemReceptionHandlerConfigurationParameterBuilder builder =
+ new FileSystemReceptionHandlerConfigurationParameterBuilder().setWatchPath(invalidPath);
+ final FileSystemReceptionHandlerConfigurationParameterGroup configParameters =
+ new FileSystemReceptionHandlerConfigurationParameterGroup(builder);
+
+ final GroupValidationResult validateResult = configParameters.validate();
+ assertFalse(validateResult.isValid());
+ assertTrue(validateResult.getResult().contains("must be a valid directory"));
+ }
+}
diff --git a/plugins/reception-plugins/src/test/resources/handling-filesystem.json b/plugins/reception-plugins/src/test/resources/handling-filesystem.json
new file mode 100644
index 00000000..6a402a7a
--- /dev/null
+++ b/plugins/reception-plugins/src/test/resources/handling-filesystem.json
@@ -0,0 +1,5 @@
+{
+ "name": "parameterConfig1",
+ "watchPath": "/tmp"
+}
+
diff --git a/plugins/reception-plugins/src/test/resources/handling-filesystemInvalid.json b/plugins/reception-plugins/src/test/resources/handling-filesystemInvalid.json
new file mode 100644
index 00000000..871b61c2
--- /dev/null
+++ b/plugins/reception-plugins/src/test/resources/handling-filesystemInvalid.json
@@ -0,0 +1,4 @@
+{
+ "invalidKey": "/tmp"
+}
+