Skip to content

Commit

Permalink
refactor: delete unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
levinkerschberger committed Nov 14, 2024
1 parent 41b94a0 commit d6f7c86
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public AgentBuilder extend(AgentBuilder agentBuilder, ConfigProperties config) {

// Notify configuration server about this agent
NotificationManager notificationManager = NotificationManager.create();
// TODO: Delete all the notification stuff.
// notificationManager.sendStartNotification();
// Set up shutdown notification to configuration server
notificationManager.setUpShutdownNotification();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package rocks.inspectit.gepard.agent.configuration.http;

import java.io.IOException;
import java.util.Objects;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.Header;
Expand Down Expand Up @@ -47,23 +48,26 @@ public void cancelled() {
}

private void logStatus(SimpleHttpResponse response) {
log.info("Received status code {}", response.getCode());
if (response.getCode() == 404) {
log.error("Configuration not found on configuration server");
}
if (response.getCode() == 200) {
int statusCode = response.getCode();
log.info("Received status code {}", statusCode);

if (statusCode == 404) {
log.error("Configuration not found on configuration server");
} else if (statusCode == 200) {
try {
Header responseHeader = response.getHeader("x-gepard-service-registered");
if (responseHeader.getValue().equals("true")) {
log.info("Connection to configuration server established successfully.");
Header registrationHeader = response.getHeader("x-gepard-service-registered");
if (Objects.isNull(registrationHeader)) {
log.error("Configuration server did not return registration header!");
} else if (registrationHeader.getValue().equals("true")) {
log.info("Connection to configuration server was successfully established.");
} else {
log.debug("Connection to configuration server reused.");
}
} catch (ProtocolException e) {
log.error("Configuration Server did not send a registration header in its response.", e);
log.error(
"Error reading response header. There might be an issue with the config-server!", e);
}
log.info("Configuration fetched successfully");
}
log.info("Configuration fetched successfully");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ public class NotificationManager {

private final String serverBaseUrl;

private final StartNotifier startNotifier;

private final ShutdownNotifier shutdownNotifier;

private NotificationManager(String serverBaseUrl) {
this.serverBaseUrl = serverBaseUrl;
this.startNotifier = new StartNotifier();
this.shutdownNotifier = new ShutdownNotifier();
}

Expand All @@ -35,22 +32,6 @@ public static NotificationManager create() {
return new NotificationManager(url);
}

/**
* Sends a message to the configuration server, to notify it about this agent starting, if a
* configuration server url was provided.
*
* @return true, if the notification was executed successfully
*/
public boolean sendStartNotification() {
boolean successful = false;
if (serverBaseUrl.isEmpty()) log.info("No configuration server url was provided");
else {
log.info("Sending start notification to configuration server with url: {}", serverBaseUrl);
successful = startNotifier.sendNotification(serverBaseUrl);
}
return successful;
}

/**
* Sets up a shutdown notification to the configuration server, if a configuration server url was
* provided.
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@

import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static rocks.inspectit.gepard.agent.internal.properties.PropertiesResolver.SERVER_URL_ENV_PROPERTY;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockserver.model.HttpError;
import rocks.inspectit.gepard.agent.MockServerTestBase;
import rocks.inspectit.gepard.agent.internal.identity.model.AgentInfo;
import rocks.inspectit.gepard.agent.internal.shutdown.ShutdownHookManager;

class NotificationManagerTest extends MockServerTestBase {
Expand All @@ -20,60 +16,11 @@ class NotificationManagerTest extends MockServerTestBase {

private final ShutdownHookManager shutdownHookManager = ShutdownHookManager.getInstance();

private final String agentId = AgentInfo.INFO.getAgentId();

@BeforeEach
void beforeEach() {
shutdownHookManager.reset();
}

@Test
void sendsStartNotificationIfServerUrlWasProvided() throws Exception {
mockServer
.when(request().withMethod("POST").withPath("/api/v1/connections/" + agentId))
.respond(response().withStatusCode(201));

withEnvironmentVariable(SERVER_URL_ENV_PROPERTY, SERVER_URL)
.execute(
() -> {
manager = NotificationManager.create();
});

boolean successful = manager.sendStartNotification();

assertTrue(successful);
}

@Test
void startNotificationFailsWithServerError() throws Exception {
mockServer
.when(request().withMethod("POST").withPath("/api/v1/connections/" + agentId))
.error(HttpError.error().withDropConnection(true));

withEnvironmentVariable(SERVER_URL_ENV_PROPERTY, SERVER_URL)
.execute(
() -> {
manager = NotificationManager.create();
});

boolean successful = manager.sendStartNotification();

assertFalse(successful);
}

@Test
void sendsNoStartNotificationWithoutProvidedServerUrl() {
mockServer
.when(request().withMethod("POST").withPath("/api/v1/connections/" + agentId))
.respond(response().withStatusCode(201));

manager = NotificationManager.create();

boolean successful = manager.sendStartNotification();

assertFalse(successful);
}

@Test
void shouldSetUpShutdownNotificationWithProvidedServerUrl() throws Exception {
withEnvironmentVariable(SERVER_URL_ENV_PROPERTY, SERVER_URL)
Expand Down

0 comments on commit d6f7c86

Please sign in to comment.