Skip to content

Commit

Permalink
Fail service registration if address is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Oct 23, 2024
1 parent 9eb376b commit 43d655b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
public interface ServiceRegistrar<MetadataKeyType extends Enum<MetadataKeyType> & MetadataKey> {

default Uni<Void> registerServiceInstance(String serviceName, String ipAddress, int defaultPort) {
checkAddressNotNull(ipAddress);
return registerServiceInstance(serviceName, Metadata.empty(), ipAddress, defaultPort);
}

default void checkAddressNotNull(String ipAddress) {
if (ipAddress == null || ipAddress.isEmpty() || ipAddress.isBlank()) {
throw new IllegalArgumentException("Parameter ipAddress should be provided.");
}
}

Uni<Void> registerServiceInstance(String serviceName, Metadata<MetadataKeyType> metadata, String ipAddress,
int defaultPort);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public ConsulServiceRegistrar(ConsulRegistrarConfiguration config, String servic
@Override
public Uni<Void> registerServiceInstance(String serviceName, Metadata<ConsulMetadataKey> metadata, String ipAddress,
int defaultPort) {
checkAddressNotNull(ipAddress);

String consulId = metadata.getMetadata().get(ConsulMetadataKey.META_CONSUL_SERVICE_ID).toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.time.Duration;
import java.util.Map;
Expand Down Expand Up @@ -86,4 +88,26 @@ void shouldRegisterServiceInstancesInConsul() throws InterruptedException {
assertThat(subscriber.awaitItem().getItem()).isNotNull();

}

@Test
void shouldFailIfNoIpAddressProvided() throws InterruptedException {
String serviceName = "my-service";
TestConfigProvider.addServiceConfig(serviceName, null, null, "consul",
null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5"),
Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort)));
Stork stork = StorkTestUtils.getNewStorkInstance();

ServiceRegistrar<ConsulMetadataKey> consulRegistrar = stork.getService(serviceName).getServiceRegistrar();

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
consulRegistrar.registerServiceInstance(serviceName, Metadata.of(ConsulMetadataKey.class)
.with(ConsulMetadataKey.META_CONSUL_SERVICE_ID, serviceName), null, 8406);
});

String expectedMessage = "Parameter ipAddress should be provided.";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public EurekaServiceRegistrar(EurekaRegistrarConfiguration config, String servic
public Uni<Void> registerServiceInstance(String serviceName, Metadata<EurekaMetadataKey> metadata, String ipAddress,
int defaultPort) {

checkAddressNotNull(ipAddress);

return registerApplicationInstance(client, serviceName,
metadata.getMetadata().get(EurekaMetadataKey.META_EUREKA_SERVICE_ID).toString(), ipAddress, null,
defaultPort, null, -1, "UP", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.time.Duration;
import java.util.Map;
Expand Down Expand Up @@ -104,4 +106,26 @@ public void testRegistrationServiceInstances(TestInfo info) {

}

@Test
void shouldFailIfNoIpAddressProvided() throws InterruptedException {
String serviceName = "my-service";
TestConfigProvider.addServiceConfig(serviceName, null, null, "eureka", null, null,
Map.of("eureka-host", eureka.getHost(), "eureka-port", String.valueOf(port)));

Stork stork = StorkTestUtils.getNewStorkInstance();

ServiceRegistrar<EurekaMetadataKey> eurekaServiceRegistrar = stork.getService(serviceName).getServiceRegistrar();

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
eurekaServiceRegistrar.registerServiceInstance(serviceName, Metadata.of(EurekaMetadataKey.class)
.with(EurekaMetadataKey.META_EUREKA_SERVICE_ID, serviceName), null, 8406);
});

String expectedMessage = "Parameter ipAddress should be provided.";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public StaticListServiceRegistrar(StaticRegistrarConfiguration config, String se
public Uni<Void> registerServiceInstance(String serviceName, Metadata<Metadata.DefaultMetadataKey> metadata,
String ipAddress,
int defaultPort) {
checkAddressNotNull(ipAddress);
HostAndPort hostAndPortToAdd = StorkAddressUtils.parseToHostAndPort(ipAddress, defaultPort,
"service '" + serviceName + "'");
String hostAndPortToAddString = StorkAddressUtils.parseToString(hostAndPortToAdd);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.smallrye.stork.serviceregistration.staticlist;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -89,4 +91,28 @@ void shouldRegisterServiceInstancesWithSchemeAndPath() {
assertThat(addresses).hasSize(1);
assertThat(addresses.get(0)).isEqualTo("localhost:8081/hello");
}

@Test
void shouldFailIfAddresseNull() {
TestConfigProvider.addServiceConfig("first-service", null, null, "static",
null, null, null);

stork = StorkTestUtils.getNewStorkInstance();

String serviceName = "first-service";

ServiceRegistrar<Metadata.DefaultMetadataKey> staticRegistrar = stork.getService(serviceName).getServiceRegistrar();

staticRegistrar.registerServiceInstance(serviceName, "http://localhost:8081/hello", 8080);

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
staticRegistrar.registerServiceInstance(serviceName, null, 8080);
});

String expectedMessage = "Parameter ipAddress should be provided.";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));

}
}

0 comments on commit 43d655b

Please sign in to comment.