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/RestChangeIngestorCommonTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/RestChangeIngestorCommonTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/RestChangeIngestorCommonTest.java new file mode 100644 index 0000000..2f4f7a3 --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/RestChangeIngestorCommonTest.java @@ -0,0 +1,127 @@ +/* + * 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 okhttp3.Headers; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +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.RestChangeIngestor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public abstract class RestChangeIngestorCommonTest { + + private static String testString = "This is a test string."; + + public static OkHttpClient client; + public static RestChangeIngestor restChangeIngestor; + public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); + public static String url; + public static ConfigurationChangeNotifier testNotifier; + public static Differentiator<InputStream> mockDifferentiator = Mockito.mock(Differentiator.class); + + + @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))); + } + + @Test + public void testGet() throws Exception { + Request request = new Request.Builder() + .url(url) + .build(); + + Response response = client.newCall(request).execute(); + if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); + + Headers responseHeaders = response.headers(); + for (int i = 0; i < responseHeaders.size(); i++) { + System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); + } + + assertEquals(RestChangeIngestor.GET_TEXT, response.body().string()); + verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any(ByteBuffer.class)); + } + + @Test + public void testFileUploadNewConfig() throws Exception { + when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(true); + + Request request = new Request.Builder() + .url(url) + .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, testString)) + .addHeader("charset","UTF-8") + .build(); + + Response response = client.newCall(request).execute(); + if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); + + Headers responseHeaders = response.headers(); + for (int i = 0; i < responseHeaders.size(); i++) { + System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); + } + + assertEquals("The result of notifying listeners:\nMockChangeListener successfully handled the configuration change\n", response.body().string()); + + verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.eq(ByteBuffer.wrap(testString.getBytes()))); + } + + @Test + public void testFileUploadSameConfig() throws Exception { + when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false); + + Request request = new Request.Builder() + .url(url) + .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, testString)) + .addHeader("charset","UTF-8") + .build(); + + Response response = client.newCall(request).execute(); + if (response.isSuccessful()) throw new IOException("Unexpected code " + response); + + Headers responseHeaders = response.headers(); + for (int i = 0; i < responseHeaders.size(); i++) { + System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); + } + + assertEquals("Request received but instance is already running this config.", response.body().string()); + + verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any()); + } +}
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/TestPullHttpChangeIngestorCommon.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/TestPullHttpChangeIngestorCommon.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/TestPullHttpChangeIngestorCommon.java deleted file mode 100644 index 53e66d7..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/TestPullHttpChangeIngestorCommon.java +++ /dev/null @@ -1,231 +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.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 TestPullHttpChangeIngestorCommon { - - 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(); - } - } - } - - } -} 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/TestRestChangeIngestorCommon.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/TestRestChangeIngestorCommon.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/TestRestChangeIngestorCommon.java deleted file mode 100644 index 72e768a..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/common/TestRestChangeIngestorCommon.java +++ /dev/null @@ -1,127 +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.common; - -import okhttp3.Headers; -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; -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.RestChangeIngestor; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.util.Collections; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public abstract class TestRestChangeIngestorCommon { - - private static String testString = "This is a test string."; - - public static OkHttpClient client; - public static RestChangeIngestor restChangeIngestor; - public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); - public static String url; - public static ConfigurationChangeNotifier testNotifier; - public static Differentiator<InputStream> mockDifferentiator = Mockito.mock(Differentiator.class); - - - @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))); - } - - @Test - public void testGet() throws Exception { - Request request = new Request.Builder() - .url(url) - .build(); - - Response response = client.newCall(request).execute(); - if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); - - Headers responseHeaders = response.headers(); - for (int i = 0; i < responseHeaders.size(); i++) { - System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); - } - - assertEquals(RestChangeIngestor.GET_TEXT, response.body().string()); - verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any(ByteBuffer.class)); - } - - @Test - public void testFileUploadNewConfig() throws Exception { - when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(true); - - Request request = new Request.Builder() - .url(url) - .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, testString)) - .addHeader("charset","UTF-8") - .build(); - - Response response = client.newCall(request).execute(); - if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); - - Headers responseHeaders = response.headers(); - for (int i = 0; i < responseHeaders.size(); i++) { - System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); - } - - assertEquals("The result of notifying listeners:\nMockChangeListener successfully handled the configuration change\n", response.body().string()); - - verify(testNotifier, Mockito.times(1)).notifyListeners(Mockito.eq(ByteBuffer.wrap(testString.getBytes()))); - } - - @Test - public void testFileUploadSameConfig() throws Exception { - when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false); - - Request request = new Request.Builder() - .url(url) - .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, testString)) - .addHeader("charset","UTF-8") - .build(); - - Response response = client.newCall(request).execute(); - if (response.isSuccessful()) throw new IOException("Unexpected code " + response); - - Headers responseHeaders = response.headers(); - for (int i = 0; i < responseHeaders.size(); i++) { - System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); - } - - assertEquals("Request received but instance is already running this config.", response.body().string()); - - verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any()); - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/StatusLoggerTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/StatusLoggerTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/StatusLoggerTest.java new file mode 100644 index 0000000..576dc4e --- /dev/null +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/StatusLoggerTest.java @@ -0,0 +1,209 @@ +/* + * 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.status.reporters; + +import org.apache.nifi.logging.LogLevel; +import org.apache.nifi.minifi.bootstrap.QueryableStatusAggregator; +import org.apache.nifi.minifi.commons.status.FlowStatusReport; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Properties; + +import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.ENCOUNTERED_IO_EXCEPTION; +import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.LOGGING_LEVEL_KEY; +import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.QUERY_KEY; +import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.REPORT_PERIOD_KEY; +import static org.mockito.Mockito.verify; + +public class StatusLoggerTest { + + private static final String MOCK_STATUS = "FlowStatusReport{controllerServiceStatusList=null, processorStatusList=[{name='TailFile', processorHealth={runStatus='Running', hasBulletins=false, " + + "validationErrorList=[]}, processorStats=null, bulletinList=null}], connectionStatusList=null, remoteProcessingGroupStatusList=null, instanceStatus=null, systemDiagnosticsStatus=null," + + " reportingTaskStatusList=null, errorsGeneratingReport=[]}"; + + private static final String MOCK_QUERY = "processor:all:health"; + + private StatusLogger statusLogger; + private Logger logger; + private QueryableStatusAggregator queryableStatusAggregator; + private FlowStatusReport flowStatusReport; + + @Before + public void init() throws IOException, NoSuchFieldException, IllegalAccessException { + statusLogger = Mockito.spy(new StatusLogger()); + + logger = Mockito.mock(Logger.class); + queryableStatusAggregator = Mockito.mock(QueryableStatusAggregator.class); + flowStatusReport = Mockito.mock(FlowStatusReport.class); + + Mockito.when(flowStatusReport.toString()).thenReturn(MOCK_STATUS); + + Field field = StatusLogger.class.getDeclaredField("logger"); + field.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(null, logger); + + + Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenReturn(flowStatusReport); + } + + @Test(expected = IllegalStateException.class) + public void testFailedInitDueToFatalLogLevel(){ + Properties properties = new Properties(); + properties.setProperty(REPORT_PERIOD_KEY, "100"); + properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.FATAL.name()); + properties.setProperty(QUERY_KEY, MOCK_QUERY); + + statusLogger.initialize(properties, queryableStatusAggregator); + } + + @Test(expected = IllegalStateException.class) + public void testFailedInitDueToNoPeriod(){ + Properties properties = new Properties(); + properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.INFO.name()); + properties.setProperty(QUERY_KEY, MOCK_QUERY); + + statusLogger.initialize(properties, queryableStatusAggregator); + } + + @Test(expected = IllegalStateException.class) + public void testFailedInitDueToNoQuery(){ + Properties properties = new Properties(); + properties.setProperty(REPORT_PERIOD_KEY, "100"); + properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.INFO.name()); + + statusLogger.initialize(properties, queryableStatusAggregator); + } + + @Test + public void TestTrace() { + statusLogger.initialize(getProperties(LogLevel.TRACE), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).info(MOCK_STATUS, (Throwable) null); + } + + @Test + public void TestDebug() { + statusLogger.initialize(getProperties(LogLevel.DEBUG), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).debug(MOCK_STATUS, (Throwable) null); + } + + @Test + public void TestInfo() { + statusLogger.initialize(getProperties(LogLevel.INFO), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).info(MOCK_STATUS, (Throwable) null); + } + + @Test + public void TestWarn() { + statusLogger.initialize(getProperties(LogLevel.WARN), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).warn(MOCK_STATUS, (Throwable) null); + } + + @Test + public void TestError() { + statusLogger.initialize(getProperties(LogLevel.ERROR), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).error(MOCK_STATUS, (Throwable) null); + } + + // Exception testing + @Test + public void TestTraceException() throws IOException { + Properties properties = new Properties(); + properties.setProperty(REPORT_PERIOD_KEY, "100"); + properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.TRACE.name()); + properties.setProperty(QUERY_KEY, MOCK_QUERY); + + IOException ioException = new IOException("This is an expected test exception"); + Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); + + statusLogger.initialize(properties, queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).trace(ENCOUNTERED_IO_EXCEPTION, ioException); + } + + @Test + public void TestDebugException() throws IOException { + IOException ioException = new IOException("This is an expected test exception"); + Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); + + statusLogger.initialize(getProperties(LogLevel.DEBUG), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).debug(ENCOUNTERED_IO_EXCEPTION, ioException); + } + + @Test + public void TestInfoException() throws IOException { + IOException ioException = new IOException("This is an expected test exception"); + Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); + + statusLogger.initialize(getProperties(LogLevel.INFO), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).info(ENCOUNTERED_IO_EXCEPTION, ioException); + } + + @Test + public void TestWarnException() throws IOException { + IOException ioException = new IOException("This is an expected test exception"); + Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); + + statusLogger.initialize(getProperties(LogLevel.WARN), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).warn(ENCOUNTERED_IO_EXCEPTION, ioException); + } + + @Test + public void TestErrorException() throws IOException { + IOException ioException = new IOException("This is an expected test exception"); + Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); + + statusLogger.initialize(getProperties(LogLevel.ERROR), queryableStatusAggregator); + statusLogger.start(); + + verify(logger, Mockito.timeout(300).atLeastOnce()).error(ENCOUNTERED_IO_EXCEPTION, ioException); + } + + private static Properties getProperties(LogLevel logLevel){ + Properties properties = new Properties(); + properties.setProperty(REPORT_PERIOD_KEY, "100"); + properties.setProperty(LOGGING_LEVEL_KEY, logLevel.name()); + properties.setProperty(QUERY_KEY, MOCK_QUERY); + return properties; + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/TestStatusLogger.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/TestStatusLogger.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/TestStatusLogger.java deleted file mode 100644 index c7fa78f..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/status/reporters/TestStatusLogger.java +++ /dev/null @@ -1,209 +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.status.reporters; - -import org.apache.nifi.logging.LogLevel; -import org.apache.nifi.minifi.bootstrap.QueryableStatusAggregator; -import org.apache.nifi.minifi.commons.status.FlowStatusReport; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.slf4j.Logger; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.Properties; - -import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.ENCOUNTERED_IO_EXCEPTION; -import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.LOGGING_LEVEL_KEY; -import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.QUERY_KEY; -import static org.apache.nifi.minifi.bootstrap.status.reporters.StatusLogger.REPORT_PERIOD_KEY; -import static org.mockito.Mockito.verify; - -public class TestStatusLogger { - - private static final String MOCK_STATUS = "FlowStatusReport{controllerServiceStatusList=null, processorStatusList=[{name='TailFile', processorHealth={runStatus='Running', hasBulletins=false, " + - "validationErrorList=[]}, processorStats=null, bulletinList=null}], connectionStatusList=null, remoteProcessingGroupStatusList=null, instanceStatus=null, systemDiagnosticsStatus=null," + - " reportingTaskStatusList=null, errorsGeneratingReport=[]}"; - - private static final String MOCK_QUERY = "processor:all:health"; - - private StatusLogger statusLogger; - private Logger logger; - private QueryableStatusAggregator queryableStatusAggregator; - private FlowStatusReport flowStatusReport; - - @Before - public void init() throws IOException, NoSuchFieldException, IllegalAccessException { - statusLogger = Mockito.spy(new StatusLogger()); - - logger = Mockito.mock(Logger.class); - queryableStatusAggregator = Mockito.mock(QueryableStatusAggregator.class); - flowStatusReport = Mockito.mock(FlowStatusReport.class); - - Mockito.when(flowStatusReport.toString()).thenReturn(MOCK_STATUS); - - Field field = StatusLogger.class.getDeclaredField("logger"); - field.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); - field.set(null, logger); - - - Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenReturn(flowStatusReport); - } - - @Test(expected = IllegalStateException.class) - public void testFailedInitDueToFatalLogLevel(){ - Properties properties = new Properties(); - properties.setProperty(REPORT_PERIOD_KEY, "100"); - properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.FATAL.name()); - properties.setProperty(QUERY_KEY, MOCK_QUERY); - - statusLogger.initialize(properties, queryableStatusAggregator); - } - - @Test(expected = IllegalStateException.class) - public void testFailedInitDueToNoPeriod(){ - Properties properties = new Properties(); - properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.INFO.name()); - properties.setProperty(QUERY_KEY, MOCK_QUERY); - - statusLogger.initialize(properties, queryableStatusAggregator); - } - - @Test(expected = IllegalStateException.class) - public void testFailedInitDueToNoQuery(){ - Properties properties = new Properties(); - properties.setProperty(REPORT_PERIOD_KEY, "100"); - properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.INFO.name()); - - statusLogger.initialize(properties, queryableStatusAggregator); - } - - @Test - public void TestTrace() { - statusLogger.initialize(getProperties(LogLevel.TRACE), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).info(MOCK_STATUS, (Throwable) null); - } - - @Test - public void TestDebug() { - statusLogger.initialize(getProperties(LogLevel.DEBUG), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).debug(MOCK_STATUS, (Throwable) null); - } - - @Test - public void TestInfo() { - statusLogger.initialize(getProperties(LogLevel.INFO), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).info(MOCK_STATUS, (Throwable) null); - } - - @Test - public void TestWarn() { - statusLogger.initialize(getProperties(LogLevel.WARN), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).warn(MOCK_STATUS, (Throwable) null); - } - - @Test - public void TestError() { - statusLogger.initialize(getProperties(LogLevel.ERROR), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).error(MOCK_STATUS, (Throwable) null); - } - - // Exception testing - @Test - public void TestTraceException() throws IOException { - Properties properties = new Properties(); - properties.setProperty(REPORT_PERIOD_KEY, "100"); - properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.TRACE.name()); - properties.setProperty(QUERY_KEY, MOCK_QUERY); - - IOException ioException = new IOException("This is an expected test exception"); - Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); - - statusLogger.initialize(properties, queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).trace(ENCOUNTERED_IO_EXCEPTION, ioException); - } - - @Test - public void TestDebugException() throws IOException { - IOException ioException = new IOException("This is an expected test exception"); - Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); - - statusLogger.initialize(getProperties(LogLevel.DEBUG), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).debug(ENCOUNTERED_IO_EXCEPTION, ioException); - } - - @Test - public void TestInfoException() throws IOException { - IOException ioException = new IOException("This is an expected test exception"); - Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); - - statusLogger.initialize(getProperties(LogLevel.INFO), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).info(ENCOUNTERED_IO_EXCEPTION, ioException); - } - - @Test - public void TestWarnException() throws IOException { - IOException ioException = new IOException("This is an expected test exception"); - Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); - - statusLogger.initialize(getProperties(LogLevel.WARN), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).warn(ENCOUNTERED_IO_EXCEPTION, ioException); - } - - @Test - public void TestErrorException() throws IOException { - IOException ioException = new IOException("This is an expected test exception"); - Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); - - statusLogger.initialize(getProperties(LogLevel.ERROR), queryableStatusAggregator); - statusLogger.start(); - - verify(logger, Mockito.timeout(300).atLeastOnce()).error(ENCOUNTERED_IO_EXCEPTION, ioException); - } - - private static Properties getProperties(LogLevel logLevel){ - Properties properties = new Properties(); - properties.setProperty(REPORT_PERIOD_KEY, "100"); - properties.setProperty(LOGGING_LEVEL_KEY, logLevel.name()); - properties.setProperty(QUERY_KEY, MOCK_QUERY); - return properties; - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformerTest.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformerTest.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformerTest.java index ecd9001..67c5916 100644 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformerTest.java +++ b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformerTest.java @@ -17,7 +17,38 @@ package org.apache.nifi.minifi.bootstrap.util; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException; +import org.apache.nifi.minifi.bootstrap.exception.InvalidConfigurationException; import org.apache.nifi.minifi.commons.schema.ConfigSchema; import org.apache.nifi.minifi.commons.schema.ConnectionSchema; import org.apache.nifi.minifi.commons.schema.ControllerServiceSchema; @@ -36,33 +67,6 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.stream.Collectors; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - public class ConfigTransformerTest { public static final Map<String, Integer> PG_ELEMENT_ORDER_MAP = generateOrderMap( Arrays.asList("processor", "inputPort", "outputPort", "funnel", "processGroup", "remoteProcessGroup", "connection")); @@ -189,6 +193,195 @@ public class ConfigTransformerTest { } } + @Test + public void doesTransformFile() throws Exception { + ConfigTransformer.transformConfigFile("./src/test/resources/config.yml", "./target/"); + File nifiPropertiesFile = new File("./target/nifi.properties"); + + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void doesTransformV1File() throws Exception { + ConfigTransformer.transformConfigFile("./src/test/resources/config-v1.yml", "./target/"); + File nifiPropertiesFile = new File("./target/nifi.properties"); + + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void doesTransformInputStream() throws Exception { + File inputFile = new File("./src/test/resources/config.yml"); + ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/"); + + File nifiPropertiesFile = new File("./target/nifi.properties"); + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void doesTransformOnDefaultFile() throws Exception { + ConfigTransformer.transformConfigFile("./src/test/resources/default.yml", "./target/"); + File nifiPropertiesFile = new File("./target/nifi.properties"); + + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void doesTransformOnMultipleProcessors() throws Exception { + ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-processors.yml", "./target/"); + File nifiPropertiesFile = new File("./target/nifi.properties"); + + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void doesTransformOnMultipleRemoteProcessGroups() throws Exception { + ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-RPGs.yml", "./target/"); + File nifiPropertiesFile = new File("./target/nifi.properties"); + + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void doesTransformOnMultipleInputPorts() throws Exception { + ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-input-ports.yml", "./target/"); + File nifiPropertiesFile = new File("./target/nifi.properties"); + + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void doesTransformOnMinimal() throws Exception { + ConfigTransformer.transformConfigFile("./src/test/resources/config-minimal.yml", "./target/"); + File nifiPropertiesFile = new File("./target/nifi.properties"); + + assertTrue(nifiPropertiesFile.exists()); + assertTrue(nifiPropertiesFile.canRead()); + + nifiPropertiesFile.deleteOnExit(); + + File flowXml = new File("./target/flow.xml.gz"); + assertTrue(flowXml.exists()); + assertTrue(flowXml.canRead()); + + flowXml.deleteOnExit(); + } + + @Test + public void handleTransformInvalidFile() throws Exception { + try { + ConfigTransformer.transformConfigFile("./src/test/resources/config-invalid.yml", "./target/"); + fail("Invalid configuration file was not detected."); + } catch (SchemaLoaderException e){ + assertEquals("Provided YAML configuration is not a Map", e.getMessage()); + } + } + + @Test + public void handleTransformMalformedField() throws Exception { + try { + ConfigTransformer.transformConfigFile("./src/test/resources/config-malformed-field.yml", "./target/"); + fail("Invalid configuration file was not detected."); + } catch (InvalidConfigurationException e){ + assertEquals("Failed to transform config file due to:['threshold' in section 'Swap' because it is found but could not be parsed as a Number]", e.getMessage()); + } + } + + @Test + public void handleTransformEmptyFile() throws Exception { + try { + ConfigTransformer.transformConfigFile("./src/test/resources/config-empty.yml", "./target/"); + fail("Invalid configuration file was not detected."); + } catch (SchemaLoaderException e){ + assertEquals("Provided YAML configuration is not a Map", e.getMessage()); + } + } + + @Test + public void handleTransformFileMissingRequiredField() throws Exception { + try { + ConfigTransformer.transformConfigFile("./src/test/resources/config-missing-required-field.yml", "./target/"); + fail("Invalid configuration file was not detected."); + } catch (InvalidConfigurationException e){ + assertEquals("Failed to transform config file due to:['class' in section 'Processors' because it was not found and it is required]", e.getMessage()); + } + } + + @Test + public void handleTransformFileMultipleProblems() throws Exception { + try { + ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-problems.yml", "./target/"); + fail("Invalid configuration file was not detected."); + } catch (InvalidConfigurationException e){ + assertEquals("Failed to transform config file due to:['class' in section 'Processors' because it was not found and it is required], " + + "['scheduling strategy' in section 'Provenance Reporting' because it is not a valid scheduling strategy], " + + "['source name' in section 'Connections' because it was not found and it is required]", e.getMessage()); + } + } + public void testConfigFileTransform(String configFile) throws Exception { ConfigSchema configSchema = SchemaLoader.loadConfigSchemaFromYaml(ConfigTransformerTest.class.getClassLoader().getResourceAsStream(configFile)); @@ -354,7 +547,7 @@ public class ConfigTransformerTest { Element item = (Element) propertyElements.item(i); properties.put(getText(item, "name"), getText(item, "value")); } - assertEquals(expected.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> nullToEmpty(e.getValue()))), properties); + assertEquals(expected.entrySet().stream().collect(Collectors.toMap(Map.Entry<String, Object>::getKey, e -> nullToEmpty(e.getValue()))), properties); } private String getText(Element element, String path) throws XPathExpressionException { http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/TestConfigTransformer.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/TestConfigTransformer.java b/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/TestConfigTransformer.java deleted file mode 100644 index d644ed3..0000000 --- a/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/util/TestConfigTransformer.java +++ /dev/null @@ -1,221 +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.util; - -import org.apache.nifi.minifi.bootstrap.exception.InvalidConfigurationException; -import org.apache.nifi.minifi.commons.schema.exception.SchemaLoaderException; -import org.junit.Test; - -import java.io.File; -import java.io.FileInputStream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -public class TestConfigTransformer { - - @Test - public void doesTransformFile() throws Exception { - ConfigTransformer.transformConfigFile("./src/test/resources/config.yml", "./target/"); - File nifiPropertiesFile = new File("./target/nifi.properties"); - - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void doesTransformV1File() throws Exception { - ConfigTransformer.transformConfigFile("./src/test/resources/config-v1.yml", "./target/"); - File nifiPropertiesFile = new File("./target/nifi.properties"); - - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void doesTransformInputStream() throws Exception { - File inputFile = new File("./src/test/resources/config.yml"); - ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/"); - - File nifiPropertiesFile = new File("./target/nifi.properties"); - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void doesTransformOnDefaultFile() throws Exception { - ConfigTransformer.transformConfigFile("./src/test/resources/default.yml", "./target/"); - File nifiPropertiesFile = new File("./target/nifi.properties"); - - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void doesTransformOnMultipleProcessors() throws Exception { - ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-processors.yml", "./target/"); - File nifiPropertiesFile = new File("./target/nifi.properties"); - - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void doesTransformOnMultipleRemoteProcessGroups() throws Exception { - ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-RPGs.yml", "./target/"); - File nifiPropertiesFile = new File("./target/nifi.properties"); - - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void doesTransformOnMultipleInputPorts() throws Exception { - ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-input-ports.yml", "./target/"); - File nifiPropertiesFile = new File("./target/nifi.properties"); - - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void doesTransformOnMinimal() throws Exception { - ConfigTransformer.transformConfigFile("./src/test/resources/config-minimal.yml", "./target/"); - File nifiPropertiesFile = new File("./target/nifi.properties"); - - assertTrue(nifiPropertiesFile.exists()); - assertTrue(nifiPropertiesFile.canRead()); - - nifiPropertiesFile.deleteOnExit(); - - File flowXml = new File("./target/flow.xml.gz"); - assertTrue(flowXml.exists()); - assertTrue(flowXml.canRead()); - - flowXml.deleteOnExit(); - } - - @Test - public void handleTransformInvalidFile() throws Exception { - try { - ConfigTransformer.transformConfigFile("./src/test/resources/config-invalid.yml", "./target/"); - fail("Invalid configuration file was not detected."); - } catch (SchemaLoaderException e){ - assertEquals("Provided YAML configuration is not a Map", e.getMessage()); - } - } - - @Test - public void handleTransformMalformedField() throws Exception { - try { - ConfigTransformer.transformConfigFile("./src/test/resources/config-malformed-field.yml", "./target/"); - fail("Invalid configuration file was not detected."); - } catch (InvalidConfigurationException e){ - assertEquals("Failed to transform config file due to:['threshold' in section 'Swap' because it is found but could not be parsed as a Number]", e.getMessage()); - } - } - - @Test - public void handleTransformEmptyFile() throws Exception { - try { - ConfigTransformer.transformConfigFile("./src/test/resources/config-empty.yml", "./target/"); - fail("Invalid configuration file was not detected."); - } catch (SchemaLoaderException e){ - assertEquals("Provided YAML configuration is not a Map", e.getMessage()); - } - } - - @Test - public void handleTransformFileMissingRequiredField() throws Exception { - try { - ConfigTransformer.transformConfigFile("./src/test/resources/config-missing-required-field.yml", "./target/"); - fail("Invalid configuration file was not detected."); - } catch (InvalidConfigurationException e){ - assertEquals("Failed to transform config file due to:['class' in section 'Processors' because it was not found and it is required]", e.getMessage()); - } - } - - @Test - public void handleTransformFileMultipleProblems() throws Exception { - try { - ConfigTransformer.transformConfigFile("./src/test/resources/config-multiple-problems.yml", "./target/"); - fail("Invalid configuration file was not detected."); - } catch (InvalidConfigurationException e){ - assertEquals("Failed to transform config file due to:['class' in section 'Processors' because it was not found and it is required], " + - "['scheduling strategy' in section 'Provenance Reporting' because it is not a valid scheduling strategy], " + - "['source name' in section 'Connections' because it was not found and it is required]", e.getMessage()); - } - } -} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/StatusReportTest.java ---------------------------------------------------------------------- diff --git a/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/StatusReportTest.java b/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/StatusReportTest.java new file mode 100644 index 0000000..8fc442f --- /dev/null +++ b/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/StatusReportTest.java @@ -0,0 +1,88 @@ +/* + * 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.commons.status; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addConnectionStatus; +import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addControllerServiceStatus; +import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addExpectedRemoteProcessGroupStatus; +import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addInstanceStatus; +import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addProcessorStatus; +import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addReportingTaskStatus; +import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addSystemDiagnosticStatus; +import static org.junit.Assert.assertEquals; + +public class StatusReportTest { + + @Test + public void verifySerializableFullyPopulated() throws IOException, ClassNotFoundException { + FlowStatusReport original = new FlowStatusReport(); + + addControllerServiceStatus(original, true, true, true, true); + addInstanceStatus(original, true, true, true, true); + addSystemDiagnosticStatus(original, true, true, true, true, true); + addReportingTaskStatus(original, true, true, true, true); + addConnectionStatus(original, true, true); + addProcessorStatus(original, true, true, true, true, true); + addExpectedRemoteProcessGroupStatus(original, true, true, true, true, true, true); + + byte[] byteArrayCopy = serialize(original); + FlowStatusReport copy = unSerialize(byteArrayCopy, FlowStatusReport.class); + + assertEquals(original, copy); + } + + @Test + public void verifySerializableSomeNull() throws IOException, ClassNotFoundException { + FlowStatusReport original = new FlowStatusReport(); + + addControllerServiceStatus(original, true, true, true, true); + addInstanceStatus(original, true, true, true, true); + addSystemDiagnosticStatus(original, true, true, true, true, true); + addProcessorStatus(original, true, true, true, true, true); + addExpectedRemoteProcessGroupStatus(original, true, true, true, true, true, true); + + byte[] byteArrayCopy = serialize(original); + FlowStatusReport copy = unSerialize(byteArrayCopy, FlowStatusReport.class); + + assertEquals(original, copy); + } + + private static <T extends Serializable> byte[] serialize(T obj) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(obj); + oos.close(); + return baos.toByteArray(); + } + + private static <T extends Serializable> T unSerialize(byte[] b, Class<T> cl) throws IOException, ClassNotFoundException { + ByteArrayInputStream bais = new ByteArrayInputStream(b); + ObjectInputStream ois = new ObjectInputStream(bais); + Object o = ois.readObject(); + return cl.cast(o); + } +} http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/b059afef/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/TestStatusReport.java ---------------------------------------------------------------------- diff --git a/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/TestStatusReport.java b/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/TestStatusReport.java deleted file mode 100644 index fc697e3..0000000 --- a/minifi-commons/minifi-utils/src/test/java/org/apache/nifi/minifi/commons/status/TestStatusReport.java +++ /dev/null @@ -1,88 +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.commons.status; - -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; - -import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addConnectionStatus; -import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addControllerServiceStatus; -import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addExpectedRemoteProcessGroupStatus; -import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addInstanceStatus; -import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addProcessorStatus; -import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addReportingTaskStatus; -import static org.apache.nifi.minifi.commons.status.util.StatusReportPopulator.addSystemDiagnosticStatus; -import static org.junit.Assert.assertEquals; - -public class TestStatusReport { - - @Test - public void verifySerializableFullyPopulated() throws IOException, ClassNotFoundException { - FlowStatusReport original = new FlowStatusReport(); - - addControllerServiceStatus(original, true, true, true, true); - addInstanceStatus(original, true, true, true, true); - addSystemDiagnosticStatus(original, true, true, true, true, true); - addReportingTaskStatus(original, true, true, true, true); - addConnectionStatus(original, true, true); - addProcessorStatus(original, true, true, true, true, true); - addExpectedRemoteProcessGroupStatus(original, true, true, true, true, true, true); - - byte[] byteArrayCopy = serialize(original); - FlowStatusReport copy = unSerialize(byteArrayCopy, FlowStatusReport.class); - - assertEquals(original, copy); - } - - @Test - public void verifySerializableSomeNull() throws IOException, ClassNotFoundException { - FlowStatusReport original = new FlowStatusReport(); - - addControllerServiceStatus(original, true, true, true, true); - addInstanceStatus(original, true, true, true, true); - addSystemDiagnosticStatus(original, true, true, true, true, true); - addProcessorStatus(original, true, true, true, true, true); - addExpectedRemoteProcessGroupStatus(original, true, true, true, true, true, true); - - byte[] byteArrayCopy = serialize(original); - FlowStatusReport copy = unSerialize(byteArrayCopy, FlowStatusReport.class); - - assertEquals(original, copy); - } - - private static <T extends Serializable> byte[] serialize(T obj) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(obj); - oos.close(); - return baos.toByteArray(); - } - - private static <T extends Serializable> T unSerialize(byte[] b, Class<T> cl) throws IOException, ClassNotFoundException { - ByteArrayInputStream bais = new ByteArrayInputStream(b); - ObjectInputStream ois = new ObjectInputStream(bais); - Object o = ois.readObject(); - return cl.cast(o); - } -}
