Commit e15d7d2d by Johannes Edmeier

Just respond with the id for successful registration

fixes #590 (old client - new server)
parent cf2a9eee
......@@ -15,6 +15,7 @@
*/
package de.codecentric.boot.admin.registry.web;
import java.net.URI;
import java.util.Collection;
import org.slf4j.Logger;
......@@ -27,11 +28,14 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.UriComponentsBuilder;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import de.codecentric.boot.admin.web.AdminController;
import static java.util.Collections.singletonMap;
/**
* REST controller for controlling registration of managed applications.
*/
......@@ -55,12 +59,12 @@ public class RegistryController {
* @return The registered application.
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Application> register(@RequestBody Application application) {
Application applicationWithSource = Application.copyOf(application).withSource("http-api")
.build();
public ResponseEntity<?> register(@RequestBody Application application, UriComponentsBuilder builder) {
Application applicationWithSource = Application.copyOf(application).withSource("http-api").build();
LOGGER.debug("Register application {}", applicationWithSource.toString());
Application registeredApp = registry.register(applicationWithSource);
return ResponseEntity.status(HttpStatus.CREATED).body(registeredApp);
String id = registry.register(applicationWithSource).getId();
URI location = builder.path("/{id}").buildAndExpand(id).toUri();
return ResponseEntity.created(location).body(singletonMap("id", id));
}
/**
......@@ -70,8 +74,7 @@ public class RegistryController {
* @return List
*/
@RequestMapping(method = RequestMethod.GET)
public Collection<Application> applications(
@RequestParam(value = "name", required = false) String name) {
public Collection<Application> applications(@RequestParam(value = "name", required = false) String name) {
LOGGER.debug("Deliver registered applications with name={}", name);
if (name == null || name.isEmpty()) {
return registry.getApplications();
......
......@@ -15,20 +15,25 @@
*/
package de.codecentric.boot.admin.registry.web;
import static org.mockito.Matchers.matches;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.UnsupportedEncodingException;
import org.hamcrest.CoreMatchers;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.internal.matchers.Matches;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
......@@ -48,9 +53,11 @@ public class RegistryControllerTest {
static {
try {
APPLICATION_TEST_JSON = new JSONObject().put("name", "test")
.put("healthUrl", "http://localhost/mgmt/health").toString();
.put("healthUrl", "http://localhost/mgmt/health")
.toString();
APPLICATION_TWICE_JSON = new JSONObject().put("name", "twice")
.put("healthUrl", "http://localhost/mgmt/health").toString();
.put("healthUrl", "http://localhost/mgmt/health")
.toString();
} catch (JSONException ex) {
throw new ExceptionInInitializerError(ex);
}
......@@ -68,31 +75,30 @@ public class RegistryControllerTest {
@Test
public void test_register_twice_get_and_remove() throws Exception {
MvcResult result = mvc
.perform(post("/api/applications").contentType(MediaType.APPLICATION_JSON)
.content(APPLICATION_TEST_JSON))
.andExpect(status().isCreated()).andExpect(jsonPath("$.name").value("test"))
.andExpect(jsonPath("$.healthUrl").value("http://localhost/mgmt/health"))
.andExpect(jsonPath("$.id").isNotEmpty()).andReturn();
MvcResult result = mvc.perform(
post("/api/applications").contentType(MediaType.APPLICATION_JSON).content(APPLICATION_TEST_JSON))
.andExpect(status().isCreated())
.andExpect(header().string(HttpHeaders.LOCATION, new Matches("http://localhost/[0-9a-f]+")))
.andExpect(jsonPath("$.id").isNotEmpty())
.andReturn();
String id = extractId(result);
mvc.perform(post("/api/applications").contentType(MediaType.APPLICATION_JSON)
.content(APPLICATION_TWICE_JSON)).andExpect(status().isCreated())
.andExpect(jsonPath("$.name").value("twice"))
.andExpect(jsonPath("$.healthUrl").value("http://localhost/mgmt/health"))
mvc.perform(post("/api/applications").contentType(MediaType.APPLICATION_JSON).content(APPLICATION_TWICE_JSON))
.andExpect(status().isCreated())
.andExpect(header().string(HttpHeaders.LOCATION, new Matches("http://localhost/[0-9a-f]+")))
.andExpect(jsonPath("$.id").value(id));
mvc.perform(get("/api/applications")).andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(id));
mvc.perform(get("/api/applications")).andExpect(status().isOk()).andExpect(jsonPath("$[0].id").value(id));
mvc.perform(get("/api/applications?name=twice")).andExpect(status().isOk())
mvc.perform(get("/api/applications?name=twice"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(id));
mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(id));
mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(id));
mvc.perform(delete("/api/applications/{id}", id)).andExpect(status().isOk())
mvc.perform(delete("/api/applications/{id}", id))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(id));
mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isNotFound());
......@@ -105,7 +111,8 @@ public class RegistryControllerTest {
@Test
public void test_get_notFound() throws Exception {
mvc.perform(get("/api/applications/unknown")).andExpect(status().isNotFound());
mvc.perform(get("/api/applications?name=unknown")).andExpect(status().isOk())
mvc.perform(get("/api/applications?name=unknown"))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isEmpty());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment