ChrisSamo632 commented on code in PR #6487: URL: https://github.com/apache/nifi/pull/6487#discussion_r989120691
########## nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-client-service/src/test/java/org/apache/nifi/elasticsearch/integration/ElasticSearchClientService_IT.java: ########## @@ -0,0 +1,741 @@ +/* + * 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.elasticsearch.integration; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.nifi.elasticsearch.DeleteOperationResponse; +import org.apache.nifi.elasticsearch.ElasticSearchClientService; +import org.apache.nifi.elasticsearch.ElasticSearchClientServiceImpl; +import org.apache.nifi.elasticsearch.ElasticsearchException; +import org.apache.nifi.elasticsearch.IndexOperationRequest; +import org.apache.nifi.elasticsearch.IndexOperationResponse; +import org.apache.nifi.elasticsearch.SearchResponse; +import org.apache.nifi.elasticsearch.TestControllerServiceProcessor; +import org.apache.nifi.elasticsearch.UpdateOperationResponse; +import org.apache.nifi.security.util.StandardTlsConfiguration; +import org.apache.nifi.security.util.TemporaryKeyStoreBuilder; +import org.apache.nifi.security.util.TlsConfiguration; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +public class ElasticSearchClientService_IT extends AbstractElasticsearch_IT { + private TestRunner runner; + private ElasticSearchClientServiceImpl service; + + static final String INDEX = "messages"; + public static String TYPE; + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static TlsConfiguration generatedTlsConfiguration; + private static TlsConfiguration truststoreTlsConfiguration; + + @BeforeAll + static void beforeAll() throws IOException { + startTestcontainer(); + TYPE = getElasticMajorVersion() == 6 ? "_doc" : ""; + System.out.println( + String.format("%n%n%n%n%n%n%n%n%n%n%n%n%n%n%nTYPE: %s%n%s%s%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n", + TYPE, IMAGE.getRepository(), IMAGE.getVersionPart()) + ); + + generatedTlsConfiguration = new TemporaryKeyStoreBuilder().build(); + truststoreTlsConfiguration = new StandardTlsConfiguration( + null, + null, + null, + generatedTlsConfiguration.getTruststorePath(), + generatedTlsConfiguration.getTruststorePassword(), + generatedTlsConfiguration.getTruststoreType() + ); + + setupTestData(); + } + + @AfterAll + public static void afterAll() throws IOException { + tearDownTestData(); + stopTestcontainer(); + } + + @BeforeEach + void before() throws Exception { + runner = TestRunners.newTestRunner(TestControllerServiceProcessor.class); + service = new ElasticSearchClientServiceImpl(); + runner.addControllerService("Client Service", service); + runner.setProperty(service, ElasticSearchClientService.HTTP_HOSTS, ELASTIC_HOST); + runner.setProperty(service, ElasticSearchClientService.CONNECT_TIMEOUT, "10000"); + runner.setProperty(service, ElasticSearchClientService.SOCKET_TIMEOUT, "60000"); + runner.setProperty(service, ElasticSearchClientService.RETRY_TIMEOUT, "60000"); + runner.setProperty(service, ElasticSearchClientService.SUPPRESS_NULLS, ElasticSearchClientService.ALWAYS_SUPPRESS.getValue()); + runner.setProperty(service, ElasticSearchClientService.USERNAME, "elastic"); + runner.setProperty(service, ElasticSearchClientService.PASSWORD, ELASTIC_USER_PASSWORD); + + try { + runner.enableControllerService(service); + } catch (Exception ex) { + throw ex; + } + + service.refresh(null, null); + } + + @AfterEach + void after() throws Exception { + service.onDisabled(); + } + + private String prettyJson(Object o) throws JsonProcessingException { + return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(o); + } + + private class MapBuilder { Review Comment: Like this as a solution while we're stuck with Java 8, but I wonder whether it's better to have an `of` method instead of `put`, which would make it simpler to migrate to the Java 11 `Map.of` API in future? Could also change `build` to wrap the Map in a `Collections.unmodifiableMap` to again be closer to `Map.of` implementation and ensure the constructed Map isn't changed unintentionally by the tests? Is it also worth making this a higher-level class somewhere in a test NAR so that others could use it (particularly when replaceing Groovy tests)? Happy for you to ignore any/all of these suggestions -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
