Skip to content

Commit

Permalink
test: add tests for org.apache.pulsar.proxy.server.ProxyServiceStarte…
Browse files Browse the repository at this point in the history
…r#start
  • Loading branch information
equanz committed Aug 1, 2024
1 parent 3cb0a2e commit 04df95b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public class ProxyServiceStarter {

private ProxyConfiguration config;

@Getter
private Authentication proxyClientAuthentication;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import lombok.Cleanup;
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.websocket.data.ProducerMessage;
import org.eclipse.jetty.client.HttpClient;
Expand Down Expand Up @@ -160,4 +166,89 @@ public String getResponse() throws InterruptedException {
}
}

@Test
public void testProxyClientAuthentication() throws Exception {
final Consumer<ProxyConfiguration> initConfig = (proxyConfig) -> {
proxyConfig.setBrokerServiceURL(pulsar.getBrokerServiceUrl());
proxyConfig.setBrokerWebServiceURL(pulsar.getWebServiceAddress());
proxyConfig.setWebServicePort(Optional.of(0));
proxyConfig.setServicePort(Optional.of(0));
proxyConfig.setWebSocketServiceEnabled(true);
proxyConfig.setBrokerProxyAllowedTargetPorts("*");
proxyConfig.setClusterName(configClusterName);
};



ProxyServiceStarter serviceStarter = new ProxyServiceStarter(ARGS, null, true);
initConfig.accept(serviceStarter.getConfig());
// ProxyServiceStarter will throw an exception when Authentication#start is failed
serviceStarter.getConfig().setBrokerClientAuthenticationPlugin(ExceptionAuthentication1.class.getName());
try {
serviceStarter.start();
fail("ProxyServiceStarter should throw an exception when Authentication#start is failed");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("ExceptionAuthentication1#start"));
assertTrue(serviceStarter.getProxyClientAuthentication() instanceof ExceptionAuthentication1);
}

serviceStarter = new ProxyServiceStarter(ARGS, null, true);
initConfig.accept(serviceStarter.getConfig());
// ProxyServiceStarter will throw an exception when Authentication#start and Authentication#close are failed
serviceStarter.getConfig().setBrokerClientAuthenticationPlugin(ExceptionAuthentication2.class.getName());
try {
serviceStarter.start();
fail("ProxyServiceStarter should throw an exception when Authentication#start and Authentication#close are failed");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("ExceptionAuthentication2#start"));
assertTrue(serviceStarter.getProxyClientAuthentication() instanceof ExceptionAuthentication2);
}
}

public static class ExceptionAuthentication1 implements Authentication {

@Override
public String getAuthMethodName() {
return "org.apache.pulsar.proxy.server.ProxyConfigurationTest.ExceptionAuthentication1";
}

@Override
public void configure(Map<String, String> authParams) {
// no-op
}

@Override
public void start() throws PulsarClientException {
throw new PulsarClientException("ExceptionAuthentication1#start");
}

@Override
public void close() throws IOException {
// no-op
}
}

public static class ExceptionAuthentication2 implements Authentication {

@Override
public String getAuthMethodName() {
return "org.apache.pulsar.proxy.server.ProxyConfigurationTest.ExceptionAuthentication2";
}

@Override
public void configure(Map<String, String> authParams) {
// no-op
}

@Override
public void start() throws PulsarClientException {
throw new PulsarClientException("ExceptionAuthentication2#start");
}

@Override
public void close() throws IOException {
throw new IOException("ExceptionAuthentication2#close");
}
}

}

0 comments on commit 04df95b

Please sign in to comment.