MINIFI-279: Makes test classes naming consistent. This closes #82.
Signed-off-by: Aldrin Piri <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi/commit/b059afef Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi/tree/b059afef Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi/diff/b059afef Branch: refs/heads/master Commit: b059afef2fab7e59489836d1bdcac01b3b7100b2 Parents: 25298e9 Author: jzonthemtn <[email protected]> Authored: Thu Apr 27 10:22:56 2017 -0400 Committer: Aldrin Piri <[email protected]> Committed: Thu Apr 27 17:03:07 2017 -0400 ---------------------------------------------------------------------- .../ConfigurationChangeCoordinatorTest.java | 84 ++ .../TestConfigurationChangeCoordinator.java | 84 -- .../TestWholeConfigDifferentiator.java | 110 --- .../WholeConfigDifferentiatorTest.java | 110 +++ .../ingestors/FileChangeIngestorTest.java | 171 ++++ .../PullHttpChangeIngestorSSLTest.java | 84 ++ .../ingestors/PullHttpChangeIngestorTest.java | 65 ++ .../ingestors/RestChangeIngestorSSLTest.java | 150 ++++ .../ingestors/RestChangeIngestorTest.java | 57 ++ .../ingestors/TestFileChangeIngestor.java | 171 ---- .../ingestors/TestPullHttpChangeIngestor.java | 65 -- .../TestPullHttpChangeIngestorSSL.java | 84 -- .../ingestors/TestRestChangeIngestor.java | 57 -- .../ingestors/TestRestChangeIngestorSSL.java | 150 ---- .../PullHttpChangeIngestorCommonTest.java | 231 +++++ .../common/RestChangeIngestorCommonTest.java | 127 +++ .../TestPullHttpChangeIngestorCommon.java | 231 ----- .../common/TestRestChangeIngestorCommon.java | 127 --- .../status/reporters/StatusLoggerTest.java | 209 +++++ .../status/reporters/TestStatusLogger.java | 209 ----- .../bootstrap/util/ConfigTransformerTest.java | 249 +++++- .../bootstrap/util/TestConfigTransformer.java | 221 ----- .../minifi/commons/status/StatusReportTest.java | 88 ++ .../minifi/commons/status/TestStatusReport.java | 88 -- .../minifi/status/StatusConfigReporterTest.java | 875 +++++++++++++++++++ .../minifi/status/TestStatusConfigReporter.java | 875 ------------------- ...iNiFiPersistentProvenanceRepositoryTest.java | 579 ++++++++++++ ...estMiNiFiPersistentProvenanceRepository.java | 579 ------------ 28 files changed, 3051 insertions(+), 3079 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ConfigurationChangeCoordinatorTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ConfigurationChangeCoordinatorTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ConfigurationChangeCoordinatorTest.java new file mode 100644 index 0000000..ad16f72 --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ConfigurationChangeCoordinatorTest.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.minifi.bootstrap.configuration; + +import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collections; +import java.util.Properties; + +import static org.mockito.Mockito.verify; + +public class ConfigurationChangeCoordinatorTest { + + private ConfigurationChangeCoordinator coordinatorSpy; + private Properties properties = new Properties(); + + @Before + public void setUp() throws Exception { + coordinatorSpy = Mockito.spy(new ConfigurationChangeCoordinator()); + } + + @After + public void tearDown() throws Exception { + coordinatorSpy.close(); + } + + @Test + public void testInit() throws Exception { + properties.put("nifi.minifi.notifier.ingestors", "org.apache.nifi.minifi.bootstrap.configuration.ingestors.RestChangeIngestor"); + final ConfigurationChangeListener testListener = Mockito.mock(ConfigurationChangeListener.class); + coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Collections.singleton(testListener)); + } + + @Test + public void testNotifyListeners() throws Exception { + final ConfigurationChangeListener testListener = Mockito.mock(ConfigurationChangeListener.class); + coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Collections.singleton(testListener)); + + Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 1); + + coordinatorSpy.notifyListeners(ByteBuffer.allocate(1)); + + verify(testListener, Mockito.atMost(1)).handleChange(Mockito.any(InputStream.class)); + } + + @Test + public void testRegisterListener() throws Exception { + final ConfigurationChangeListener firstListener = Mockito.mock(ConfigurationChangeListener.class); + coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Collections.singleton(firstListener)); + + Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 1); + + coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Arrays.asList(firstListener, firstListener)); + Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 1); + + final ConfigurationChangeListener secondListener = Mockito.mock(ConfigurationChangeListener.class); + coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Arrays.asList(firstListener, secondListener)); + Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 2); + + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/TestConfigurationChangeCoordinator.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/TestConfigurationChangeCoordinator.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/TestConfigurationChangeCoordinator.java deleted file mode 100644 index a6882a5..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/TestConfigurationChangeCoordinator.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -package org.apache.nifi.minifi.bootstrap.configuration; - -import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.Collections; -import java.util.Properties; - -import static org.mockito.Mockito.verify; - -public class TestConfigurationChangeCoordinator { - - private ConfigurationChangeCoordinator coordinatorSpy; - private Properties properties = new Properties(); - - @Before - public void setUp() throws Exception { - coordinatorSpy = Mockito.spy(new ConfigurationChangeCoordinator()); - } - - @After - public void tearDown() throws Exception { - coordinatorSpy.close(); - } - - @Test - public void testInit() throws Exception { - properties.put("nifi.minifi.notifier.ingestors", "org.apache.nifi.minifi.bootstrap.configuration.ingestors.RestChangeIngestor"); - final ConfigurationChangeListener testListener = Mockito.mock(ConfigurationChangeListener.class); - coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Collections.singleton(testListener)); - } - - @Test - public void testNotifyListeners() throws Exception { - final ConfigurationChangeListener testListener = Mockito.mock(ConfigurationChangeListener.class); - coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Collections.singleton(testListener)); - - Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 1); - - coordinatorSpy.notifyListeners(ByteBuffer.allocate(1)); - - verify(testListener, Mockito.atMost(1)).handleChange(Mockito.any(InputStream.class)); - } - - @Test - public void testRegisterListener() throws Exception { - final ConfigurationChangeListener firstListener = Mockito.mock(ConfigurationChangeListener.class); - coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Collections.singleton(firstListener)); - - Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 1); - - coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Arrays.asList(firstListener, firstListener)); - Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 1); - - final ConfigurationChangeListener secondListener = Mockito.mock(ConfigurationChangeListener.class); - coordinatorSpy.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), Arrays.asList(firstListener, secondListener)); - Assert.assertEquals("Did not receive the correct number of registered listeners", coordinatorSpy.getChangeListeners().size(), 2); - - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/TestWholeConfigDifferentiator.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/TestWholeConfigDifferentiator.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/TestWholeConfigDifferentiator.java deleted file mode 100644 index 9dabea3..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/TestWholeConfigDifferentiator.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -package org.apache.nifi.minifi.bootstrap.configuration.differentiators; - -import okhttp3.Request; -import org.apache.commons.io.FileUtils; -import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; -import org.apache.nifi.minifi.bootstrap.configuration.differentiators.interfaces.Differentiator; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.Mockito; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Properties; -import java.util.concurrent.atomic.AtomicReference; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -public class TestWholeConfigDifferentiator { - - public static final Path newConfigPath = Paths.get("./src/test/resources/config.yml"); - public static final Path defaultConfigPath = Paths.get("./src/test/resources/default.yml"); - - public static ByteBuffer defaultConfigBuffer; - public static ByteBuffer newConfigBuffer; - public static Properties properties = new Properties(); - public static ConfigurationFileHolder configurationFileHolder; - - public static Request dummyRequest; - - @BeforeClass - public static void beforeClass() throws IOException { - dummyRequest = new Request.Builder() - .get() - .url("https://nifi.apache.org/index.html") - .build(); - - defaultConfigBuffer = ByteBuffer.wrap(FileUtils.readFileToByteArray(defaultConfigPath.toFile())); - newConfigBuffer = ByteBuffer.wrap(FileUtils.readFileToByteArray(newConfigPath.toFile())); - - configurationFileHolder = Mockito.mock(ConfigurationFileHolder.class); - - when(configurationFileHolder.getConfigFileReference()).thenReturn(new AtomicReference<>(defaultConfigBuffer)); - } - - @Before - public void beforeEach() { - } - - // InputStream differentiator methods - - @Test - public void TestSameInputStream() throws IOException { - Differentiator<InputStream> differentiator = WholeConfigDifferentiator.getInputStreamDifferentiator(); - differentiator.initialize(properties, configurationFileHolder); - - FileInputStream fileInputStream = new FileInputStream(defaultConfigPath.toFile()); - assertFalse(differentiator.isNew(fileInputStream)); - } - - @Test - public void TestNewInputStream() throws IOException { - Differentiator<InputStream> differentiator = WholeConfigDifferentiator.getInputStreamDifferentiator(); - differentiator.initialize(properties, configurationFileHolder); - - FileInputStream fileInputStream = new FileInputStream(newConfigPath.toFile()); - assertTrue(differentiator.isNew(fileInputStream)); - } - - // Bytebuffer differentiator methods - - @Test - public void TestSameByteBuffer() throws IOException { - Differentiator<ByteBuffer> differentiator = WholeConfigDifferentiator.getByteBufferDifferentiator(); - differentiator.initialize(properties, configurationFileHolder); - - assertFalse(differentiator.isNew(defaultConfigBuffer)); - } - - @Test - public void TestNewByteBuffer() throws IOException { - Differentiator<ByteBuffer> differentiator = WholeConfigDifferentiator.getByteBufferDifferentiator(); - differentiator.initialize(properties, configurationFileHolder); - - assertTrue(differentiator.isNew(newConfigBuffer)); - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/WholeConfigDifferentiatorTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/WholeConfigDifferentiatorTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/WholeConfigDifferentiatorTest.java new file mode 100644 index 0000000..d2115a2 --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/differentiators/WholeConfigDifferentiatorTest.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.minifi.bootstrap.configuration.differentiators; + +import okhttp3.Request; +import org.apache.commons.io.FileUtils; +import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; +import org.apache.nifi.minifi.bootstrap.configuration.differentiators.interfaces.Differentiator; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +public class WholeConfigDifferentiatorTest { + + public static final Path newConfigPath = Paths.get("./src/test/resources/config.yml"); + public static final Path defaultConfigPath = Paths.get("./src/test/resources/default.yml"); + + public static ByteBuffer defaultConfigBuffer; + public static ByteBuffer newConfigBuffer; + public static Properties properties = new Properties(); + public static ConfigurationFileHolder configurationFileHolder; + + public static Request dummyRequest; + + @BeforeClass + public static void beforeClass() throws IOException { + dummyRequest = new Request.Builder() + .get() + .url("https://nifi.apache.org/index.html") + .build(); + + defaultConfigBuffer = ByteBuffer.wrap(FileUtils.readFileToByteArray(defaultConfigPath.toFile())); + newConfigBuffer = ByteBuffer.wrap(FileUtils.readFileToByteArray(newConfigPath.toFile())); + + configurationFileHolder = Mockito.mock(ConfigurationFileHolder.class); + + when(configurationFileHolder.getConfigFileReference()).thenReturn(new AtomicReference<>(defaultConfigBuffer)); + } + + @Before + public void beforeEach() { + } + + // InputStream differentiator methods + + @Test + public void TestSameInputStream() throws IOException { + Differentiator<InputStream> differentiator = WholeConfigDifferentiator.getInputStreamDifferentiator(); + differentiator.initialize(properties, configurationFileHolder); + + FileInputStream fileInputStream = new FileInputStream(defaultConfigPath.toFile()); + assertFalse(differentiator.isNew(fileInputStream)); + } + + @Test + public void TestNewInputStream() throws IOException { + Differentiator<InputStream> differentiator = WholeConfigDifferentiator.getInputStreamDifferentiator(); + differentiator.initialize(properties, configurationFileHolder); + + FileInputStream fileInputStream = new FileInputStream(newConfigPath.toFile()); + assertTrue(differentiator.isNew(fileInputStream)); + } + + // Bytebuffer differentiator methods + + @Test + public void TestSameByteBuffer() throws IOException { + Differentiator<ByteBuffer> differentiator = WholeConfigDifferentiator.getByteBufferDifferentiator(); + differentiator.initialize(properties, configurationFileHolder); + + assertFalse(differentiator.isNew(defaultConfigBuffer)); + } + + @Test + public void TestNewByteBuffer() throws IOException { + Differentiator<ByteBuffer> differentiator = WholeConfigDifferentiator.getByteBufferDifferentiator(); + differentiator.initialize(properties, configurationFileHolder); + + assertTrue(differentiator.isNew(newConfigBuffer)); + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/FileChangeIngestorTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/FileChangeIngestorTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/FileChangeIngestorTest.java new file mode 100644 index 0000000..7cac488 --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/FileChangeIngestorTest.java @@ -0,0 +1,171 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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. + */ +package org.apache.nifi.minifi.bootstrap.configuration.ingestors; + +import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; +import java.nio.file.WatchService; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; + +import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier; +import org.apache.nifi.minifi.bootstrap.configuration.differentiators.interfaces.Differentiator; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class FileChangeIngestorTest { + + private static final String CONFIG_FILENAME = "config.yml"; + private static final String TEST_CONFIG_PATH = "src/test/resources/config.yml"; + + private FileChangeIngestor notifierSpy; + private WatchService mockWatchService; + private Properties testProperties; + private Differentiator<InputStream> mockDifferentiator; + private ConfigurationChangeNotifier testNotifier; + + @Before + public void setUp() throws Exception { + mockWatchService = Mockito.mock(WatchService.class); + notifierSpy = Mockito.spy(new FileChangeIngestor()); + mockDifferentiator = Mockito.mock(Differentiator.class); + testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); + + notifierSpy.setConfigFilePath(Paths.get(TEST_CONFIG_PATH)); + notifierSpy.setWatchService(mockWatchService); + notifierSpy.setDifferentiator(mockDifferentiator); + notifierSpy.setConfigurationChangeNotifier(testNotifier); + + testProperties = new Properties(); + testProperties.put(FileChangeIngestor.CONFIG_FILE_PATH_KEY, TEST_CONFIG_PATH); + testProperties.put(FileChangeIngestor.POLLING_PERIOD_INTERVAL_KEY, FileChangeIngestor.DEFAULT_POLLING_PERIOD_INTERVAL); + } + + @After + public void tearDown() throws Exception { + notifierSpy.close(); + } + + @Test(expected = IllegalStateException.class) + public void testInitialize_invalidFile() throws Exception { + testProperties.put(FileChangeIngestor.CONFIG_FILE_PATH_KEY, "/land/of/make/believe"); + notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); + } + + @Test + public void testInitialize_validFile() throws Exception { + notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); + } + + @Test(expected = IllegalStateException.class) + public void testInitialize_invalidPollingPeriod() throws Exception { + testProperties.put(FileChangeIngestor.POLLING_PERIOD_INTERVAL_KEY, "abc"); + notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); + } + + @Test + public void testInitialize_useDefaultPolling() throws Exception { + testProperties.remove(FileChangeIngestor.POLLING_PERIOD_INTERVAL_KEY); + notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); + } + + /* Verify handleChange events */ + @Test + public void testTargetChangedNoModification() throws Exception { + when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false); + final ConfigurationChangeNotifier testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); + + // In this case the WatchKey is null because there were no events found + establishMockEnvironmentForChangeTests(testNotifier, null); + + verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any(ByteBuffer.class)); + } + + @Test + public void testTargetChangedWithModificationEvent_nonConfigFile() throws Exception { + when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false); + final ConfigurationChangeNotifier testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); + + // In this case, we receive a trigger event for the directory monitored, but it was another file not being monitored + final WatchKey mockWatchKey = createMockWatchKeyForPath("footage_not_found.yml"); + + establishMockEnvironmentForChangeTests(testNotifier, mockWatchKey); + + notifierSpy.targetChanged(); + + verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any(ByteBuffer.class)); + } + + @Test + public void testTargetChangedWithModificationEvent() throws Exception { + when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(true); + + final WatchKey mockWatchKey = createMockWatchKeyForPath(CONFIG_FILENAME); + // Provided as a spy to allow injection of mock objects for some tests when dealing with the finalized FileSystems class + establishMockEnvironmentForChangeTests(testNotifier, mockWatchKey); + + // Invoke the method of interest + notifierSpy.run(); + + verify(mockWatchService, Mockito.atLeastOnce()).poll(); + verify(testNotifier, Mockito.atLeastOnce()).notifyListeners(Mockito.any(ByteBuffer.class)); + } + + /* Helper methods to establish mock environment */ + private WatchKey createMockWatchKeyForPath(String configFilePath) { + final WatchKey mockWatchKey = Mockito.mock(WatchKey.class); + final List<WatchEvent<?>> mockWatchEvents = (List<WatchEvent<?>>) Mockito.mock(List.class); + when(mockWatchKey.pollEvents()).thenReturn(mockWatchEvents); + when(mockWatchKey.reset()).thenReturn(true); + + final Iterator mockIterator = Mockito.mock(Iterator.class); + when(mockWatchEvents.iterator()).thenReturn(mockIterator); + + final WatchEvent mockWatchEvent = Mockito.mock(WatchEvent.class); + when(mockIterator.hasNext()).thenReturn(true, false); + when(mockIterator.next()).thenReturn(mockWatchEvent); + + // In this case, we receive a trigger event for the directory monitored, and it was the file monitored + when(mockWatchEvent.context()).thenReturn(Paths.get(configFilePath)); + when(mockWatchEvent.kind()).thenReturn(ENTRY_MODIFY); + + return mockWatchKey; + } + + private void establishMockEnvironmentForChangeTests(ConfigurationChangeNotifier configurationChangeNotifier, final WatchKey watchKey) throws Exception { + // Establish the file mock and its parent directory + final Path mockConfigFilePath = Mockito.mock(Path.class); + final Path mockConfigFileParentPath = Mockito.mock(Path.class); + + // When getting the parent of the file, get the directory + when(mockConfigFilePath.getParent()).thenReturn(mockConfigFileParentPath); + + when(mockWatchService.poll()).thenReturn(watchKey); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorSSLTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorSSLTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorSSLTest.java new file mode 100644 index 0000000..195cf60 --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorSSLTest.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.minifi.bootstrap.configuration.ingestors; + +import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; +import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.PullHttpChangeIngestorCommonTest; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.junit.BeforeClass; +import org.mockito.Mockito; + +import java.util.Properties; + +public class PullHttpChangeIngestorSSLTest extends PullHttpChangeIngestorCommonTest { + + @BeforeClass + public static void setUp() throws Exception { + PullHttpChangeIngestorCommonTest.init(); + + SslContextFactory ssl = new SslContextFactory(); + + ssl.setKeyStorePath("./src/test/resources/localhost-ks.jks"); + ssl.setKeyStorePassword("localtest"); + ssl.setKeyStoreType("JKS"); + ssl.setTrustStorePath("./src/test/resources/localhost-ts.jks"); + ssl.setTrustStorePassword("localtest"); + ssl.setTrustStoreType("JKS"); + ssl.setNeedClientAuth(true); + + // build the connector + final ServerConnector https = new ServerConnector(jetty, ssl); + + // set host and port + https.setPort(0); + https.setHost("localhost"); + + // Severely taxed environments may have significant delays when executing. + https.setIdleTimeout(30000L); + + // add the connector + jetty.addConnector(https); + + jetty.start(); + + Thread.sleep(1000); + + if (!jetty.isStarted()) { + throw new IllegalStateException("Jetty server not started"); + } + } + + @Override + public void pullHttpChangeIngestorInit(Properties properties) { + properties.setProperty(PullHttpChangeIngestor.TRUSTSTORE_LOCATION_KEY, "./src/test/resources/localhost-ts.jks"); + properties.setProperty(PullHttpChangeIngestor.TRUSTSTORE_PASSWORD_KEY, "localtest"); + properties.setProperty(PullHttpChangeIngestor.TRUSTSTORE_TYPE_KEY, "JKS"); + properties.setProperty(PullHttpChangeIngestor.KEYSTORE_LOCATION_KEY, "./src/test/resources/localhost-ks.jks"); + properties.setProperty(PullHttpChangeIngestor.KEYSTORE_PASSWORD_KEY, "localtest"); + properties.setProperty(PullHttpChangeIngestor.KEYSTORE_TYPE_KEY, "JKS"); + port = ((ServerConnector) jetty.getConnectors()[0]).getLocalPort(); + properties.put(PullHttpChangeIngestor.PORT_KEY, String.valueOf(port)); + properties.put(PullHttpChangeIngestor.HOST_KEY, "localhost"); + + pullHttpChangeIngestor = new PullHttpChangeIngestor(); + + pullHttpChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); + pullHttpChangeIngestor.setDifferentiator(mockDifferentiator); + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorTest.java new file mode 100644 index 0000000..f39fbdf --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestorTest.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.minifi.bootstrap.configuration.ingestors; + +import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; +import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.PullHttpChangeIngestorCommonTest; +import org.eclipse.jetty.server.ServerConnector; +import org.junit.BeforeClass; +import org.mockito.Mockito; + +import java.util.Properties; + +public class PullHttpChangeIngestorTest extends PullHttpChangeIngestorCommonTest { + + @BeforeClass + public static void setUp() throws Exception { + PullHttpChangeIngestorCommonTest.init(); + + final ServerConnector http = new ServerConnector(jetty); + + http.setPort(0); + http.setHost("localhost"); + + http.setIdleTimeout(3000L); + jetty.addConnector(http); + + jetty.start(); + + Thread.sleep(1000); + + if (!jetty.isStarted()) { + throw new IllegalStateException("Jetty server not started"); + } + } + + + @Override + public void pullHttpChangeIngestorInit(Properties properties) { + port = ((ServerConnector) jetty.getConnectors()[0]).getLocalPort(); + properties.put(PullHttpChangeIngestor.PORT_KEY, String.valueOf(port)); + properties.put(PullHttpChangeIngestor.HOST_KEY, "localhost"); + properties.put(PullHttpChangeIngestor.PULL_HTTP_POLLING_PERIOD_KEY, "30000"); + + pullHttpChangeIngestor = new PullHttpChangeIngestor(); + + + pullHttpChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); + pullHttpChangeIngestor.setDifferentiator(mockDifferentiator); + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorSSLTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorSSLTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorSSLTest.java new file mode 100644 index 0000000..dada8a5 --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorSSLTest.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.minifi.bootstrap.configuration.ingestors; + + +import okhttp3.OkHttpClient; +import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeListener; +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier; +import org.apache.nifi.minifi.bootstrap.configuration.ListenerHandleResult; +import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.RestChangeIngestorCommonTest; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.mockito.Mockito; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.util.Collections; +import java.util.Properties; + +import static org.mockito.Mockito.when; + + +public class RestChangeIngestorSSLTest extends RestChangeIngestorCommonTest { + + + @BeforeClass + public static void setUpHttps() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, UnrecoverableKeyException, KeyManagementException, InterruptedException { + Properties properties = new Properties(); + properties.setProperty(RestChangeIngestor.TRUSTSTORE_LOCATION_KEY, "./src/test/resources/localhost-ts.jks"); + properties.setProperty(RestChangeIngestor.TRUSTSTORE_PASSWORD_KEY, "localtest"); + properties.setProperty(RestChangeIngestor.TRUSTSTORE_TYPE_KEY, "JKS"); + properties.setProperty(RestChangeIngestor.KEYSTORE_LOCATION_KEY, "./src/test/resources/localhost-ks.jks"); + properties.setProperty(RestChangeIngestor.KEYSTORE_PASSWORD_KEY, "localtest"); + properties.setProperty(RestChangeIngestor.KEYSTORE_TYPE_KEY, "JKS"); + properties.setProperty(RestChangeIngestor.NEED_CLIENT_AUTH_KEY, "false"); + + restChangeIngestor = new RestChangeIngestor(); + + testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); + ConfigurationChangeListener testListener = Mockito.mock(ConfigurationChangeListener.class); + when(testListener.getDescriptor()).thenReturn("MockChangeListener"); + when(testNotifier.notifyListeners(Mockito.any())).thenReturn(Collections.singleton(new ListenerHandleResult(testListener))); + + restChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); + restChangeIngestor.setDifferentiator(mockDifferentiator); + restChangeIngestor.start(); + + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + final String keystoreLocation = "./src/test/resources/localhost-ks.jks"; + final String keystorePass = "localtest"; + final String keystoreType = "JKS"; + + // prepare the keystore + final KeyStore keyStore = KeyStore.getInstance(keystoreType); + + try (FileInputStream keyStoreStream = new FileInputStream(keystoreLocation)) { + keyStore.load(keyStoreStream, keystorePass.toCharArray()); + } + + final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + keyManagerFactory.init(keyStore, keystorePass.toCharArray()); + + // load truststore + final String truststoreLocation = "./src/test/resources/localhost-ts.jks"; + final String truststorePass = "localtest"; + final String truststoreType = "JKS"; + + KeyStore truststore = KeyStore.getInstance(truststoreType); + final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509"); + truststore.load(new FileInputStream(truststoreLocation), truststorePass.toCharArray()); + trustManagerFactory.init(truststore); + + final X509TrustManager x509TrustManager; + TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); + if (trustManagers[0] != null) { + x509TrustManager = (X509TrustManager) trustManagers[0]; + } else { + throw new IllegalStateException("List of trust managers is null"); + } + + SSLContext tempSslContext; + try { + tempSslContext = SSLContext.getInstance("TLS"); + } catch (NoSuchAlgorithmException e) { + tempSslContext = SSLContext.getDefault(); + } + + final SSLContext sslContext = tempSslContext; + sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); + + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + clientBuilder.sslSocketFactory(sslSocketFactory, x509TrustManager); + + Thread.sleep(1000); + url = restChangeIngestor.getURI().toURL().toString(); + client = clientBuilder.build(); + } + + @AfterClass + public static void stop() throws Exception { + restChangeIngestor.close(); + client = null; + } + + private static KeyStore readKeyStore(String path) throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException { + KeyStore ks = KeyStore.getInstance("jks"); + + char[] password = "localtest".toCharArray(); + + java.io.FileInputStream fis = null; + try { + fis = new java.io.FileInputStream(path); + ks.load(fis, password); + } finally { + if (fis != null) { + fis.close(); + } + } + return ks; + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorTest.java new file mode 100644 index 0000000..5af2d33 --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/RestChangeIngestorTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.minifi.bootstrap.configuration.ingestors; + + +import okhttp3.OkHttpClient; +import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier; +import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.RestChangeIngestorCommonTest; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.mockito.Mockito; + +import java.net.MalformedURLException; +import java.util.Properties; + + +public class RestChangeIngestorTest extends RestChangeIngestorCommonTest { + + @BeforeClass + public static void setUp() throws InterruptedException, MalformedURLException { + Properties properties = new Properties(); + restChangeIngestor = new RestChangeIngestor(); + + testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); + + restChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); + restChangeIngestor.setDifferentiator(mockDifferentiator); + restChangeIngestor.start(); + + client = new OkHttpClient(); + + url = restChangeIngestor.getURI().toURL().toString(); + Thread.sleep(1000); + } + + @AfterClass + public static void stop() throws Exception { + restChangeIngestor.close(); + client = null; + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestFileChangeIngestor.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestFileChangeIngestor.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestFileChangeIngestor.java deleted file mode 100644 index 9817e7e..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestFileChangeIngestor.java +++ /dev/null @@ -1,171 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * 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. - */ -package org.apache.nifi.minifi.bootstrap.configuration.ingestors; - -import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.WatchEvent; -import java.nio.file.WatchKey; -import java.nio.file.WatchService; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; - -import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; -import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier; -import org.apache.nifi.minifi.bootstrap.configuration.differentiators.interfaces.Differentiator; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -public class TestFileChangeIngestor { - - private static final String CONFIG_FILENAME = "config.yml"; - private static final String TEST_CONFIG_PATH = "src/test/resources/config.yml"; - - private FileChangeIngestor notifierSpy; - private WatchService mockWatchService; - private Properties testProperties; - private Differentiator<InputStream> mockDifferentiator; - private ConfigurationChangeNotifier testNotifier; - - @Before - public void setUp() throws Exception { - mockWatchService = Mockito.mock(WatchService.class); - notifierSpy = Mockito.spy(new FileChangeIngestor()); - mockDifferentiator = Mockito.mock(Differentiator.class); - testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); - - notifierSpy.setConfigFilePath(Paths.get(TEST_CONFIG_PATH)); - notifierSpy.setWatchService(mockWatchService); - notifierSpy.setDifferentiator(mockDifferentiator); - notifierSpy.setConfigurationChangeNotifier(testNotifier); - - testProperties = new Properties(); - testProperties.put(FileChangeIngestor.CONFIG_FILE_PATH_KEY, TEST_CONFIG_PATH); - testProperties.put(FileChangeIngestor.POLLING_PERIOD_INTERVAL_KEY, FileChangeIngestor.DEFAULT_POLLING_PERIOD_INTERVAL); - } - - @After - public void tearDown() throws Exception { - notifierSpy.close(); - } - - @Test(expected = IllegalStateException.class) - public void testInitialize_invalidFile() throws Exception { - testProperties.put(FileChangeIngestor.CONFIG_FILE_PATH_KEY, "/land/of/make/believe"); - notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); - } - - @Test - public void testInitialize_validFile() throws Exception { - notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); - } - - @Test(expected = IllegalStateException.class) - public void testInitialize_invalidPollingPeriod() throws Exception { - testProperties.put(FileChangeIngestor.POLLING_PERIOD_INTERVAL_KEY, "abc"); - notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); - } - - @Test - public void testInitialize_useDefaultPolling() throws Exception { - testProperties.remove(FileChangeIngestor.POLLING_PERIOD_INTERVAL_KEY); - notifierSpy.initialize(testProperties, Mockito.mock(ConfigurationFileHolder.class), Mockito.mock(ConfigurationChangeNotifier.class)); - } - - /* Verify handleChange events */ - @Test - public void testTargetChangedNoModification() throws Exception { - when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false); - final ConfigurationChangeNotifier testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); - - // In this case the WatchKey is null because there were no events found - establishMockEnvironmentForChangeTests(testNotifier, null); - - verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any(ByteBuffer.class)); - } - - @Test - public void testTargetChangedWithModificationEvent_nonConfigFile() throws Exception { - when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false); - final ConfigurationChangeNotifier testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); - - // In this case, we receive a trigger event for the directory monitored, but it was another file not being monitored - final WatchKey mockWatchKey = createMockWatchKeyForPath("footage_not_found.yml"); - - establishMockEnvironmentForChangeTests(testNotifier, mockWatchKey); - - notifierSpy.targetChanged(); - - verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any(ByteBuffer.class)); - } - - @Test - public void testTargetChangedWithModificationEvent() throws Exception { - when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(true); - - final WatchKey mockWatchKey = createMockWatchKeyForPath(CONFIG_FILENAME); - // Provided as a spy to allow injection of mock objects for some tests when dealing with the finalized FileSystems class - establishMockEnvironmentForChangeTests(testNotifier, mockWatchKey); - - // Invoke the method of interest - notifierSpy.run(); - - verify(mockWatchService, Mockito.atLeastOnce()).poll(); - verify(testNotifier, Mockito.atLeastOnce()).notifyListeners(Mockito.any(ByteBuffer.class)); - } - - /* Helper methods to establish mock environment */ - private WatchKey createMockWatchKeyForPath(String configFilePath) { - final WatchKey mockWatchKey = Mockito.mock(WatchKey.class); - final List<WatchEvent<?>> mockWatchEvents = (List<WatchEvent<?>>) Mockito.mock(List.class); - when(mockWatchKey.pollEvents()).thenReturn(mockWatchEvents); - when(mockWatchKey.reset()).thenReturn(true); - - final Iterator mockIterator = Mockito.mock(Iterator.class); - when(mockWatchEvents.iterator()).thenReturn(mockIterator); - - final WatchEvent mockWatchEvent = Mockito.mock(WatchEvent.class); - when(mockIterator.hasNext()).thenReturn(true, false); - when(mockIterator.next()).thenReturn(mockWatchEvent); - - // In this case, we receive a trigger event for the directory monitored, and it was the file monitored - when(mockWatchEvent.context()).thenReturn(Paths.get(configFilePath)); - when(mockWatchEvent.kind()).thenReturn(ENTRY_MODIFY); - - return mockWatchKey; - } - - private void establishMockEnvironmentForChangeTests(ConfigurationChangeNotifier configurationChangeNotifier, final WatchKey watchKey) throws Exception { - // Establish the file mock and its parent directory - final Path mockConfigFilePath = Mockito.mock(Path.class); - final Path mockConfigFileParentPath = Mockito.mock(Path.class); - - // When getting the parent of the file, get the directory - when(mockConfigFilePath.getParent()).thenReturn(mockConfigFileParentPath); - - when(mockWatchService.poll()).thenReturn(watchKey); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestor.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestor.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestor.java deleted file mode 100644 index f5c6806..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestor.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -package org.apache.nifi.minifi.bootstrap.configuration.ingestors; - -import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; -import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.TestPullHttpChangeIngestorCommon; -import org.eclipse.jetty.server.ServerConnector; -import org.junit.BeforeClass; -import org.mockito.Mockito; - -import java.util.Properties; - -public class TestPullHttpChangeIngestor extends TestPullHttpChangeIngestorCommon { - - @BeforeClass - public static void setUp() throws Exception { - TestPullHttpChangeIngestorCommon.init(); - - final ServerConnector http = new ServerConnector(jetty); - - http.setPort(0); - http.setHost("localhost"); - - http.setIdleTimeout(3000L); - jetty.addConnector(http); - - jetty.start(); - - Thread.sleep(1000); - - if (!jetty.isStarted()) { - throw new IllegalStateException("Jetty server not started"); - } - } - - - @Override - public void pullHttpChangeIngestorInit(Properties properties) { - port = ((ServerConnector) jetty.getConnectors()[0]).getLocalPort(); - properties.put(PullHttpChangeIngestor.PORT_KEY, String.valueOf(port)); - properties.put(PullHttpChangeIngestor.HOST_KEY, "localhost"); - properties.put(PullHttpChangeIngestor.PULL_HTTP_POLLING_PERIOD_KEY, "30000"); - - pullHttpChangeIngestor = new PullHttpChangeIngestor(); - - - pullHttpChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); - pullHttpChangeIngestor.setDifferentiator(mockDifferentiator); - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestorSSL.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestorSSL.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestorSSL.java deleted file mode 100644 index 340a131..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestPullHttpChangeIngestorSSL.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -package org.apache.nifi.minifi.bootstrap.configuration.ingestors; - -import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; -import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.TestPullHttpChangeIngestorCommon; -import org.eclipse.jetty.server.ServerConnector; -import org.eclipse.jetty.util.ssl.SslContextFactory; -import org.junit.BeforeClass; -import org.mockito.Mockito; - -import java.util.Properties; - -public class TestPullHttpChangeIngestorSSL extends TestPullHttpChangeIngestorCommon { - - @BeforeClass - public static void setUp() throws Exception { - TestPullHttpChangeIngestorCommon.init(); - - SslContextFactory ssl = new SslContextFactory(); - - ssl.setKeyStorePath("./src/test/resources/localhost-ks.jks"); - ssl.setKeyStorePassword("localtest"); - ssl.setKeyStoreType("JKS"); - ssl.setTrustStorePath("./src/test/resources/localhost-ts.jks"); - ssl.setTrustStorePassword("localtest"); - ssl.setTrustStoreType("JKS"); - ssl.setNeedClientAuth(true); - - // build the connector - final ServerConnector https = new ServerConnector(jetty, ssl); - - // set host and port - https.setPort(0); - https.setHost("localhost"); - - // Severely taxed environments may have significant delays when executing. - https.setIdleTimeout(30000L); - - // add the connector - jetty.addConnector(https); - - jetty.start(); - - Thread.sleep(1000); - - if (!jetty.isStarted()) { - throw new IllegalStateException("Jetty server not started"); - } - } - - @Override - public void pullHttpChangeIngestorInit(Properties properties) { - properties.setProperty(PullHttpChangeIngestor.TRUSTSTORE_LOCATION_KEY, "./src/test/resources/localhost-ts.jks"); - properties.setProperty(PullHttpChangeIngestor.TRUSTSTORE_PASSWORD_KEY, "localtest"); - properties.setProperty(PullHttpChangeIngestor.TRUSTSTORE_TYPE_KEY, "JKS"); - properties.setProperty(PullHttpChangeIngestor.KEYSTORE_LOCATION_KEY, "./src/test/resources/localhost-ks.jks"); - properties.setProperty(PullHttpChangeIngestor.KEYSTORE_PASSWORD_KEY, "localtest"); - properties.setProperty(PullHttpChangeIngestor.KEYSTORE_TYPE_KEY, "JKS"); - port = ((ServerConnector) jetty.getConnectors()[0]).getLocalPort(); - properties.put(PullHttpChangeIngestor.PORT_KEY, String.valueOf(port)); - properties.put(PullHttpChangeIngestor.HOST_KEY, "localhost"); - - pullHttpChangeIngestor = new PullHttpChangeIngestor(); - - pullHttpChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); - pullHttpChangeIngestor.setDifferentiator(mockDifferentiator); - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestor.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestor.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestor.java deleted file mode 100644 index 86a6b4e..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestor.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -package org.apache.nifi.minifi.bootstrap.configuration.ingestors; - - -import okhttp3.OkHttpClient; -import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; -import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier; -import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.TestRestChangeIngestorCommon; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.mockito.Mockito; - -import java.net.MalformedURLException; -import java.util.Properties; - - -public class TestRestChangeIngestor extends TestRestChangeIngestorCommon { - - @BeforeClass - public static void setUp() throws InterruptedException, MalformedURLException { - Properties properties = new Properties(); - restChangeIngestor = new RestChangeIngestor(); - - testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); - - restChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); - restChangeIngestor.setDifferentiator(mockDifferentiator); - restChangeIngestor.start(); - - client = new OkHttpClient(); - - url = restChangeIngestor.getURI().toURL().toString(); - Thread.sleep(1000); - } - - @AfterClass - public static void stop() throws Exception { - restChangeIngestor.close(); - client = null; - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestorSSL.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestorSSL.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestorSSL.java deleted file mode 100644 index debe772..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/TestRestChangeIngestorSSL.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -package org.apache.nifi.minifi.bootstrap.configuration.ingestors; - - -import okhttp3.OkHttpClient; -import org.apache.nifi.minifi.bootstrap.ConfigurationFileHolder; -import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeListener; -import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier; -import org.apache.nifi.minifi.bootstrap.configuration.ListenerHandleResult; -import org.apache.nifi.minifi.bootstrap.configuration.ingestors.common.TestRestChangeIngestorCommon; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.mockito.Mockito; - -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509TrustManager; -import java.io.FileInputStream; -import java.io.IOException; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.util.Collections; -import java.util.Properties; - -import static org.mockito.Mockito.when; - - -public class TestRestChangeIngestorSSL extends TestRestChangeIngestorCommon { - - - @BeforeClass - public static void setUpHttps() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, UnrecoverableKeyException, KeyManagementException, InterruptedException { - Properties properties = new Properties(); - properties.setProperty(RestChangeIngestor.TRUSTSTORE_LOCATION_KEY, "./src/test/resources/localhost-ts.jks"); - properties.setProperty(RestChangeIngestor.TRUSTSTORE_PASSWORD_KEY, "localtest"); - properties.setProperty(RestChangeIngestor.TRUSTSTORE_TYPE_KEY, "JKS"); - properties.setProperty(RestChangeIngestor.KEYSTORE_LOCATION_KEY, "./src/test/resources/localhost-ks.jks"); - properties.setProperty(RestChangeIngestor.KEYSTORE_PASSWORD_KEY, "localtest"); - properties.setProperty(RestChangeIngestor.KEYSTORE_TYPE_KEY, "JKS"); - properties.setProperty(RestChangeIngestor.NEED_CLIENT_AUTH_KEY, "false"); - - restChangeIngestor = new RestChangeIngestor(); - - testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); - ConfigurationChangeListener testListener = Mockito.mock(ConfigurationChangeListener.class); - when(testListener.getDescriptor()).thenReturn("MockChangeListener"); - when(testNotifier.notifyListeners(Mockito.any())).thenReturn(Collections.singleton(new ListenerHandleResult(testListener))); - - restChangeIngestor.initialize(properties, Mockito.mock(ConfigurationFileHolder.class), testNotifier); - restChangeIngestor.setDifferentiator(mockDifferentiator); - restChangeIngestor.start(); - - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - - final String keystoreLocation = "./src/test/resources/localhost-ks.jks"; - final String keystorePass = "localtest"; - final String keystoreType = "JKS"; - - // prepare the keystore - final KeyStore keyStore = KeyStore.getInstance(keystoreType); - - try (FileInputStream keyStoreStream = new FileInputStream(keystoreLocation)) { - keyStore.load(keyStoreStream, keystorePass.toCharArray()); - } - - final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); - keyManagerFactory.init(keyStore, keystorePass.toCharArray()); - - // load truststore - final String truststoreLocation = "./src/test/resources/localhost-ts.jks"; - final String truststorePass = "localtest"; - final String truststoreType = "JKS"; - - KeyStore truststore = KeyStore.getInstance(truststoreType); - final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509"); - truststore.load(new FileInputStream(truststoreLocation), truststorePass.toCharArray()); - trustManagerFactory.init(truststore); - - final X509TrustManager x509TrustManager; - TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); - if (trustManagers[0] != null) { - x509TrustManager = (X509TrustManager) trustManagers[0]; - } else { - throw new IllegalStateException("List of trust managers is null"); - } - - SSLContext tempSslContext; - try { - tempSslContext = SSLContext.getInstance("TLS"); - } catch (NoSuchAlgorithmException e) { - tempSslContext = SSLContext.getDefault(); - } - - final SSLContext sslContext = tempSslContext; - sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); - - final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); - clientBuilder.sslSocketFactory(sslSocketFactory, x509TrustManager); - - Thread.sleep(1000); - url = restChangeIngestor.getURI().toURL().toString(); - client = clientBuilder.build(); - } - - @AfterClass - public static void stop() throws Exception { - restChangeIngestor.close(); - client = null; - } - - private static KeyStore readKeyStore(String path) throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException { - KeyStore ks = KeyStore.getInstance("jks"); - - char[] password = "localtest".toCharArray(); - - java.io.FileInputStream fis = null; - try { - fis = new java.io.FileInputStream(path); - ks.load(fis, password); - } finally { - if (fis != null) { - fis.close(); - } - } - return ks; - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/PullHttpChangeIngestorCommonTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/PullHttpChangeIngestorCommonTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/PullHttpChangeIngestorCommonTest.java new file mode 100644 index 0000000..229e33d --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/PullHttpChangeIngestorCommonTest.java @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.minifi.bootstrap.configuration.ingestors.common; + +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeListener; +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier; +import org.apache.nifi.minifi.bootstrap.configuration.ListenerHandleResult; +import org.apache.nifi.minifi.bootstrap.configuration.differentiators.interfaces.Differentiator; +import org.apache.nifi.minifi.bootstrap.configuration.ingestors.PullHttpChangeIngestor; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.Properties; + +import static org.apache.nifi.minifi.bootstrap.configuration.ingestors.PullHttpChangeIngestor.PATH_KEY; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public abstract class PullHttpChangeIngestorCommonTest { + + public static volatile Server jetty; + public static volatile int port; + public static volatile PullHttpChangeIngestor pullHttpChangeIngestor; + public static ConfigurationChangeNotifier testNotifier = Mockito.mock(ConfigurationChangeNotifier.class); + public static Differentiator<ByteBuffer> mockDifferentiator = Mockito.mock(Differentiator.class); + public static final String RESPONSE_STRING = "test"; + public static final String PATH_RESPONSE_STRING = "path"; + public static ByteBuffer configBuffer= ByteBuffer.wrap(RESPONSE_STRING.getBytes()); + public static ByteBuffer pathConfigBuffer= ByteBuffer.wrap(PATH_RESPONSE_STRING.getBytes()); + public static final String ETAG = "testEtag"; + public static final String QUOTED_ETAG = "\"testEtag\""; + + public static void init() { + QueuedThreadPool queuedThreadPool = new QueuedThreadPool(); + queuedThreadPool.setDaemon(true); + jetty = new Server(queuedThreadPool); + + HandlerCollection handlerCollection = new HandlerCollection(true); + handlerCollection.addHandler(new JettyHandler(RESPONSE_STRING, PATH_RESPONSE_STRING)); + jetty.setHandler(handlerCollection); + } + + public abstract void pullHttpChangeIngestorInit(Properties properties); + + @Before + public void before() { + Mockito.reset(testNotifier); + ConfigurationChangeListener testListener = Mockito.mock(ConfigurationChangeListener.class); + when(testListener.getDescriptor()).thenReturn("MockChangeListener"); + Mockito.when(testNotifier.notifyListeners(Mockito.any())).thenReturn(Collections.singleton(new ListenerHandleResult(testListener))); + } + + @AfterClass + public static void shutdown() throws Exception { + jetty.stop(); + } + + @Test + public void testNewUpdate() throws IOException { + Properties properties = new Properties(); + pullHttpChangeIngestorInit(properties); + pullHttpChangeIngestor.setUseEtag(false); + when(mockDifferentiator.isNew(Mockito.any(ByteBuffer.class))).thenReturn(true); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.eq(configBuffer.asReadOnlyBuffer())); + } + + + @Test + public void testNoUpdate() throws IOException { + Properties properties = new Properties(); + pullHttpChangeIngestorInit(properties); + pullHttpChangeIngestor.setUseEtag(false); + when(mockDifferentiator.isNew(Mockito.any(ByteBuffer.class))).thenReturn(false); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any()); + } + + @Test + public void testUseEtag() throws IOException { + Properties properties = new Properties(); + pullHttpChangeIngestorInit(properties); + pullHttpChangeIngestor.setLastEtag(""); + + pullHttpChangeIngestor.setUseEtag(true); + + when(mockDifferentiator.isNew(Mockito.any(ByteBuffer.class))).thenReturn(true); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.eq(configBuffer)); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.any()); + + } + + @Test + public void testNewUpdateWithPath() throws IOException { + Properties properties = new Properties(); + properties.put(PATH_KEY, "/config.yml"); + pullHttpChangeIngestorInit(properties); + pullHttpChangeIngestor.setUseEtag(false); + when(mockDifferentiator.isNew(Mockito.any(ByteBuffer.class))).thenReturn(true); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.eq(pathConfigBuffer.asReadOnlyBuffer())); + } + + @Test + public void testNoUpdateWithPath() throws IOException { + Properties properties = new Properties(); + properties.put(PATH_KEY, "/config.yml"); + pullHttpChangeIngestorInit(properties); + pullHttpChangeIngestor.setUseEtag(false); + when(mockDifferentiator.isNew(Mockito.any(ByteBuffer.class))).thenReturn(false); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any()); + } + + @Test + public void testUseEtagWithPath() throws IOException { + Properties properties = new Properties(); + properties.put(PATH_KEY, "/config.yml"); + pullHttpChangeIngestorInit(properties); + pullHttpChangeIngestor.setLastEtag(""); + + pullHttpChangeIngestor.setUseEtag(true); + + when(mockDifferentiator.isNew(Mockito.any(ByteBuffer.class))).thenReturn(true); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.eq(pathConfigBuffer.asReadOnlyBuffer())); + + pullHttpChangeIngestor.run(); + + verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.any()); + + } + + static class JettyHandler extends AbstractHandler { + volatile String configResponse; + volatile String pathResponse; + + public JettyHandler(String configResponse, String pathResponse){ + this.configResponse = configResponse; + this.pathResponse = pathResponse; + } + + + @Override + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException { + + baseRequest.setHandled(true); + + if ("GET".equals(request.getMethod())) { + + if (QUOTED_ETAG.equals(baseRequest.getHeader("If-None-Match"))){ + writeOutput(response, null, 304); + } else { + + if ("/config.yml".equals(baseRequest.getPathInfo())) { + writeOutput(response, pathResponse, 200); + } else { + writeOutput(response, configResponse, 200); + } + } + + + } else { + writeOutput(response, "not a GET request", 404); + } + } + + private void writeOutput(HttpServletResponse response, String responseBuffer, int responseCode) throws IOException { + response.setStatus(responseCode); + response.setHeader("ETag", ETAG); + if (responseBuffer != null) { + response.setContentType("text/plain"); + response.setContentLength(responseBuffer.length()); + response.setCharacterEncoding(StandardCharsets.UTF_8.displayName()); + try (PrintWriter writer = response.getWriter()) { + writer.print(responseBuffer); + writer.flush(); + } + } + } + + } +}
