Skip to content

Commit

Permalink
fix: adjust response handling according to agent
Browse files Browse the repository at this point in the history
  • Loading branch information
binarycoded committed Nov 22, 2024
1 parent 6ead8cf commit ef1a7fa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import rocks.inspectit.gepard.agentmanager.configuration.service.ConfigurationService;
Expand Down Expand Up @@ -35,9 +36,11 @@ public ResponseEntity<InspectitConfiguration> getAgentConfiguration(
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok()
.header("x-gepard-service-registered", String.valueOf(isFirstRequest))
.body(configuration);
if (isFirstRequest) {
return ResponseEntity.status(HttpStatus.CREATED).body(configuration);
}

return ResponseEntity.ok().body(configuration);
}

@PutMapping
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agentmanager.configuration.validation;

import static rocks.inspectit.gepard.agentmanager.connection.service.ConnectionService.*;

import java.util.Map;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import rocks.inspectit.gepard.agentmanager.exception.MissingHeaderException;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ConfigurationRequestHeaderValidator {
public static void validateConfigurationRequestHeaders(Map<String, String> headers) {
validateHeader(headers, "x-gepard-service-name");
validateHeader(headers, "x-gepard-vm-id");
validateHeader(headers, "x-gepard-gepard-version");
validateHeader(headers, "x-gepard-otel-version");
validateHeader(headers, "x-gepard-java-version");
validateHeader(headers, "x-gepard-start-time");
validateHeader(headers, X_GEPARD_SERVICE_NAME);
validateHeader(headers, X_GEPARD_VM_ID);
validateHeader(headers, X_GEPARD_GEPARD_VERSION);
validateHeader(headers, X_GEPARD_OTEL_VERSION);
validateHeader(headers, X_GEPARD_JAVA_VERSION);
validateHeader(headers, X_GEPARD_START_TIME);
}

private static void validateHeader(Map<String, String> headers, String headerName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
@RequiredArgsConstructor
public class ConnectionService {

public static final String X_GEPARD_SERVICE_NAME = "x-gepard-service-name";
public static final String X_GEPARD_VM_ID = "x-gepard-vm-id";
public static final String X_GEPARD_GEPARD_VERSION = "x-gepard-gepard-version";
public static final String X_GEPARD_OTEL_VERSION = "x-gepard-otel-version";
public static final String X_GEPARD_JAVA_VERSION = "x-gepard-java-version";
public static final String X_GEPARD_START_TIME = "x-gepard-start-time";

private final ConcurrentHashMap<String, Connection> connectionCache;
private final RegexQueryService regexQueryService;

Expand Down Expand Up @@ -72,7 +79,7 @@ public ConnectionDto handleUpdateRequest(
public List<ConnectionDto> getConnections() {
return connectionCache.entrySet().stream()
.map(entry -> ConnectionDto.fromConnection(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
.toList();
}

/**
Expand All @@ -85,7 +92,7 @@ public List<ConnectionDto> queryConnections(QueryConnectionRequest query) {
return connectionCache.entrySet().stream()
.filter(entry -> matchesConnection(entry.getValue(), query))
.map(entry -> ConnectionDto.fromConnection(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
.toList();
}

/**
Expand Down Expand Up @@ -207,12 +214,12 @@ private void connectAgent(String connectionId, Map<String, String> headers) {

validateConfigurationRequestHeaders(headers);

String serviceName = headers.get("x-gepard-service-name");
String vmId = headers.get("x-gepard-vm-id");
String gepardVersion = headers.get("x-gepard-gepard-version");
String otelVersion = headers.get("x-gepard-otel-version");
String javaVersion = headers.get("x-gepard-java-version");
String startTime = headers.get("x-gepard-start-time");
String serviceName = headers.get(X_GEPARD_SERVICE_NAME);
String vmId = headers.get(X_GEPARD_VM_ID);
String gepardVersion = headers.get(X_GEPARD_GEPARD_VERSION);
String otelVersion = headers.get(X_GEPARD_OTEL_VERSION);
String javaVersion = headers.get(X_GEPARD_JAVA_VERSION);
String startTime = headers.get(X_GEPARD_START_TIME);

Map<String, String> attributes =
headers.entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ class ConfigurationControllerTest {
@Autowired private ObjectMapper objectMapper;

@MockBean private ConfigurationService configurationService;

@MockBean private ConnectionService connectionService;

private static String agentId = "12345";
private static final String AGENT_ID = "12345";

@Test
void getConfiguration_whenNoConfigAvailable_shouldReturnNoContent() throws Exception {

when(configurationService.getConfiguration()).thenReturn(null);

mockMvc
.perform(get("/api/v1/agent-configuration/" + agentId))
.perform(get("/api/v1/agent-configuration/" + AGENT_ID))
.andExpect(status().isNoContent());
}

Expand All @@ -50,36 +51,12 @@ void getConfiguration_whenConfigAvailable_shouldReturnOk() throws Exception {
when(configurationService.getConfiguration()).thenReturn(new InspectitConfiguration());

mockMvc
.perform(get("/api/v1/agent-configuration/" + agentId).headers(headers))
.perform(get("/api/v1/agent-configuration/" + AGENT_ID).headers(headers))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(objectMapper.writeValueAsString(new InspectitConfiguration())));
}

@Test
void getConfiguration_whenAgentIsNew_shouldReturnRegistrationHeaderTrue() throws Exception {
HttpHeaders headers = getGepardHeaders();
when(configurationService.getConfiguration()).thenReturn(new InspectitConfiguration());
when(connectionService.handleConfigurationRequest(agentId, headers.toSingleValueMap()))
.thenReturn(true);

mockMvc
.perform(get("/api/v1/agent-configuration/" + agentId).headers(headers))
.andExpect(header().string("x-gepard-service-registered", "true"));
}

@Test
void getConfiguration_whenAgentIsNew_shouldReturnRegistrationHeaderFalse() throws Exception {
HttpHeaders headers = getGepardHeaders();
when(configurationService.getConfiguration()).thenReturn(new InspectitConfiguration());
when(connectionService.handleConfigurationRequest(agentId, headers.toSingleValueMap()))
.thenReturn(false);

mockMvc
.perform(get("/api/v1/agent-configuration/" + agentId).headers(headers))
.andExpect(header().string("x-gepard-service-registered", "false"));
}

@Test
void updateConfiguration_shouldReturnOkAndConfiguration() throws Exception {
InspectitConfiguration configuration = InspectitConfigurationTestUtil.createConfiguration();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agentmanager.testutils;

import static rocks.inspectit.gepard.agentmanager.connection.service.ConnectionService.*;
import static rocks.inspectit.gepard.agentmanager.connection.service.ConnectionService.X_GEPARD_START_TIME;

import java.time.Instant;
import java.util.Map;
import org.springframework.http.HttpHeaders;
Expand All @@ -24,12 +27,12 @@ public static Map<String, String> getGepardHeadersAsMap() {
*/
public static HttpHeaders getGepardHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.add("x-gepard-service-name", "test-service");
headers.add("x-gepard-vm-id", "test-vm-id");
headers.add("x-gepard-gepard-version", "test-gepard-version");
headers.add("x-gepard-otel-version", "test-otel-version");
headers.add("x-gepard-java-version", "test-java-version");
headers.add("x-gepard-start-time", Instant.now().toString());
headers.add(X_GEPARD_SERVICE_NAME, "test-service");
headers.add(X_GEPARD_VM_ID, "test-vm-id");
headers.add(X_GEPARD_GEPARD_VERSION, "test-gepard-version");
headers.add(X_GEPARD_OTEL_VERSION, "test-otel-version");
headers.add(X_GEPARD_JAVA_VERSION, "test-java-version");
headers.add(X_GEPARD_START_TIME, Instant.now().toString());
headers.add("x-gepard-attribute-test-attribute", "test-attribute-value");
return headers;
}
Expand Down

0 comments on commit ef1a7fa

Please sign in to comment.