NIFI-7929: connectionTimeout and readTimeout options are not exposed in the CLI

Signed-off-by: Bryan Bende <bbende@apache.org>
This commit is contained in:
Denes Arvay 2020-10-20 16:12:04 +02:00 committed by Bryan Bende
parent 67d1b73a85
commit dc25d4c39e
No known key found for this signature in database
GPG Key ID: A0DDA9ED50711C39
4 changed files with 135 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang3.StringUtils;
import org.apache.nifi.toolkit.cli.impl.client.nifi.AccessClient;
import org.apache.nifi.toolkit.cli.impl.client.nifi.ConnectionClient;
@ -62,7 +63,8 @@ public class JerseyNiFiClient implements NiFiClient {
static final int DEFAULT_CONNECT_TIMEOUT = 10000;
static final int DEFAULT_READ_TIMEOUT = 10000;
private final Client client;
@VisibleForTesting
public final Client client;
private final WebTarget baseTarget;
private JerseyNiFiClient(final Builder builder) {

View File

@ -66,6 +66,9 @@ public abstract class AbstractCommand<R extends Result> implements Command<R> {
options.addOption(CommandOption.URL.createOption());
options.addOption(CommandOption.PROPERTIES.createOption());
options.addOption(CommandOption.CONNECTION_TIMEOUT.createOption());
options.addOption(CommandOption.READ_TIMEOUT.createOption());
options.addOption(CommandOption.KEYSTORE.createOption());
options.addOption(CommandOption.KEYSTORE_TYPE.createOption());
options.addOption(CommandOption.KEYSTORE_PASSWORD.createOption());

View File

@ -0,0 +1,129 @@
/*
* 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.toolkit.cli.impl.client;
import org.apache.nifi.registry.client.NiFiRegistryClient;
import org.apache.nifi.toolkit.cli.api.ClientFactory;
import org.apache.nifi.toolkit.cli.api.Command;
import org.apache.nifi.toolkit.cli.api.Context;
import org.apache.nifi.toolkit.cli.api.Result;
import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient;
import org.apache.nifi.toolkit.cli.impl.client.nifi.impl.JerseyNiFiClient;
import org.apache.nifi.toolkit.cli.impl.command.CommandProcessor;
import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand;
import org.apache.nifi.toolkit.cli.impl.command.registry.AbstractNiFiRegistryCommand;
import org.apache.nifi.toolkit.cli.impl.context.StandardContext;
import org.apache.nifi.toolkit.cli.impl.session.InMemorySession;
import org.glassfish.jersey.client.ClientProperties;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import javax.ws.rs.client.Client;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
public class TestClientTimeout {
private Context context;
private ClientFactory<NiFiClient> nifiClientFactory;
private ClientFactory<NiFiRegistryClient> nifiRegClientFactory;
private final Client[] client = new Client[1];
@Before
public void setUp() throws Exception {
nifiClientFactory = Mockito.spy(new NiFiClientFactory());
Mockito.doAnswer(invocationOnMock -> {
final JerseyNiFiClient nifiClient = Mockito.spy((JerseyNiFiClient) invocationOnMock.callRealMethod());
Mockito.doNothing().when(nifiClient).close(); // avoid closing the client before getting the configuration in the test method
client[0] = nifiClient.client;
return nifiClient;
}).when(nifiClientFactory).createClient(Mockito.any(Properties.class));
nifiRegClientFactory = Mockito.spy(new NiFiRegistryClientFactory());
Mockito.doAnswer(invocationOnMock -> {
final JerseyExtendedNiFiRegistryClient nifiRegistryClient = Mockito.spy((JerseyExtendedNiFiRegistryClient) invocationOnMock.callRealMethod());
Mockito.doNothing().when(nifiRegistryClient).close(); // avoid closing the client before getting the configuration in the test method
client[0] = nifiRegistryClient.client;
return nifiRegistryClient;
}).when(nifiRegClientFactory).createClient(Mockito.any(Properties.class));
context = new StandardContext.Builder()
.output(System.out)
.session(new InMemorySession())
.nifiClientFactory(nifiClientFactory)
.nifiRegistryClientFactory(nifiRegClientFactory)
.interactive(false)
.build();
}
@After
public void tearDown() {
if (client[0] != null) {
try {
client[0].close();
} catch (Throwable th) {
}
client[0] = null;
}
}
@Test
public void testNiFiClientTimeoutSettings() {
testClientTimeoutSettings(new AbstractNiFiCommand<Result>("test", Result.class) {
@Override
public Result doExecute(NiFiClient client, Properties properties) {
return null;
}
@Override
public String getDescription() {
return "";
}
});
}
@Test
public void testNiFiRegistryClientTimeoutSettings() {
testClientTimeoutSettings(new AbstractNiFiRegistryCommand<Result>("test", Result.class) {
@Override
public Result doExecute(NiFiRegistryClient client, Properties properties) {
return null;
}
@Override
public String getDescription() {
return "";
}
});
}
private void testClientTimeoutSettings(Command<?> command) {
command.initialize(context);
final CommandProcessor processor = new CommandProcessor(Collections.singletonMap("test", command), Collections.emptyMap(), context);
processor.process(new String[] { "test", "-cto", "1", "-rto", "2", "-baseUrl", "http://localhost:9999" });
Assert.assertNotNull(client[0]);
final Map<String, Object> clientProperties = client[0].getConfiguration().getProperties();
Assert.assertEquals(1, clientProperties.get(ClientProperties.CONNECT_TIMEOUT));
Assert.assertEquals(2, clientProperties.get(ClientProperties.READ_TIMEOUT));
}
}