Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mappings.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect } from "vitest";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

function walk(dir: string, out: string[]) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const e of entries) {
const p = path.join(dir, e.name);
if (e.isDirectory()) {
walk(p, out);
} else {
out.push(p);
}
}
}

describe("ui click affordance", () => {
it("applications/instances list should have pointer cursor rule", () => {
const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(here, "..");
const files: string[] = [];
walk(root, files);

const rxCursor = /cursor\s*:\s*pointer/i;
const rxKw = /(applications|instances|instance|application)/i;

let hit = false;
for (const f of files) {
const lower = f.toLowerCase();
if (
lower.endsWith(".css") ||
lower.endsWith(".scss") ||
lower.endsWith(".vue") ||
lower.endsWith(".ts") ||
lower.endsWith(".tsx") ||
lower.endsWith(".js") ||
lower.endsWith(".jsx")
) {
const text = fs.readFileSync(f, "utf8");
if (rxCursor.test(text) && rxKw.test(text)) {
hit = true;
break;
}
}
}

expect(hit).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import de.codecentric.boot.admin.server.services.ApplicationRegistry;
import de.codecentric.boot.admin.server.services.InstanceRegistry;
import de.codecentric.boot.admin.server.utils.jackson.AdminServerModule;
import de.codecentric.boot.admin.server.web.AdminControllerExceptionHandler;
import de.codecentric.boot.admin.server.web.ApplicationsController;
import de.codecentric.boot.admin.server.web.InstancesController;
import de.codecentric.boot.admin.server.web.client.InstanceWebClient;
Expand Down Expand Up @@ -62,6 +63,12 @@ public ApplicationsController applicationsController(ApplicationRegistry applica
return new ApplicationsController(applicationRegistry, applicationEventPublisher);
}

@Bean
@ConditionalOnMissingBean
public AdminControllerExceptionHandler adminControllerExceptionHandler() {
return new AdminControllerExceptionHandler();
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public static class ReactiveRestApiConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2014-2023 the original author or authors.
*
* Licensed 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
*
* https://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 de.codecentric.boot.admin.server.web;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.Exceptions;

@RestControllerAdvice(annotations = AdminController.class)
public class AdminControllerExceptionHandler {

@ExceptionHandler(ServerWebInputException.class)
public ResponseEntity<Void> handleWebInput(ServerWebInputException ex) {
Throwable cause = Exceptions.unwrap(ex);
if (cause instanceof IllegalArgumentException) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.badRequest().build();
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Void> handleIllegalArgument(IllegalArgumentException ex) {
return ResponseEntity.badRequest().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package de.codecentric.boot.admin.server.web;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -33,6 +34,7 @@
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -95,6 +97,105 @@ void should_return_not_found_when_get_unknown_instance() {
this.client.get().uri("/instances/unknown").exchange().expectStatus().isNotFound();
}

@Test
void should_reject_invalid_json() {
this.client.post()
.uri("/instances")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue("{")
.exchange()
.expectStatus()
.isBadRequest();
}

@Test
void should_reject_missing_name() {
String body = "{ \"healthUrl\": \"http://localhost:" + localPort + "/application/health\" }";

this.client.post()
.uri("/instances")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
.exchange()
.expectStatus()
.isBadRequest();
}

@Test
void should_reject_missing_health_url() {
String body = "{ \"name\": \"noname\" }";

this.client.post()
.uri("/instances")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
.exchange()
.expectStatus()
.isBadRequest();
}

@Test
void should_create_distinct_ids_for_different_health_urls() {
String id1 = register().block();

String other = "{ \"name\": \"other\", \"healthUrl\": \"http://localhost:" + localPort + "/other/health\" }";
String id2 = registerWithBody(other);

assertThat(id2).isNotEqualTo(id1);

assertInstanceById(id1);
assertInstanceById(id2);
}

@Test
void should_remove_instance_after_delete() {
String id = register().block();

this.client.delete().uri(getLocation(id)).exchange().expectStatus().isNoContent();

this.client.get().uri(getLocation(id)).exchange().expectStatus().isNotFound();

this.client.get()
.uri("/instances")
.exchange()
.expectStatus()
.isOk()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(String.class)
.consumeWith((response) -> {
DocumentContext json = JsonPath.parse(response.getResponseBody());
List<String> ids = json.read("$[?(@.id == '" + id + "')].id");
assertThat(ids).isEmpty();
});

}

@Test
void should_not_create_duplicate_instance_on_reregister() {
String id = register().block(Duration.ofSeconds(5));

registerSecondTime(id);

this.client.get()
.uri("/instances")
.exchange()
.expectStatus()
.isOk()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(String.class)
.consumeWith((response) -> {
DocumentContext json = JsonPath.parse(response.getResponseBody());
List<String> ids = json.read("$[?(@.id == '" + id + "')].id");
assertThat(ids).hasSize(1);
});

}

@Test
void should_return_empty_list() {
this.client.get()
Expand Down Expand Up @@ -280,6 +381,23 @@ private Mono<String> register() {
//@formatter:on
}

private String registerWithBody(String body) {
//@formatter:off
EntityExchangeResult<Map<String, Object>> result = client.post()
.uri("/instances")
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
.exchange()
.expectStatus().isCreated()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectHeader().valueMatches("location", "http://localhost:" + localPort + "/instances/[0-9a-f]+")
.expectBody(responseType)
.returnResult();
//@formatter:on
assertThat(result.getResponseBody()).containsKeys("id");
return result.getResponseBody().get("id").toString();
}

private String getLocation(String id) {
return "http://localhost:" + localPort + "/instances/" + id;

Expand Down