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.
*/
......@@ -40,78 +44,77 @@ import de.codecentric.boot.admin.web.AdminController;
@RequestMapping("/api/applications")
public class RegistryController {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistryController.class);
private static final Logger LOGGER = LoggerFactory.getLogger(RegistryController.class);
private final ApplicationRegistry registry;
private final ApplicationRegistry registry;
public RegistryController(ApplicationRegistry registry) {
this.registry = registry;
}
public RegistryController(ApplicationRegistry registry) {
this.registry = registry;
}
/**
* Register an application within this admin application.
*
* @param application The application infos.
* @return The registered application.
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Application> register(@RequestBody Application application) {
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);
}
/**
* Register an application within this admin application.
*
* @param application The application infos.
* @return The registered application.
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> register(@RequestBody Application application, UriComponentsBuilder builder) {
Application applicationWithSource = Application.copyOf(application).withSource("http-api").build();
LOGGER.debug("Register application {}", applicationWithSource.toString());
String id = registry.register(applicationWithSource).getId();
URI location = builder.path("/{id}").buildAndExpand(id).toUri();
return ResponseEntity.created(location).body(singletonMap("id", id));
}
/**
* List all registered applications with name
*
* @param name the name to search for
* @return List
*/
@RequestMapping(method = RequestMethod.GET)
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();
} else {
return registry.getApplicationsByName(name);
}
}
/**
* List all registered applications with name
*
* @param name the name to search for
* @return List
*/
@RequestMapping(method = RequestMethod.GET)
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();
} else {
return registry.getApplicationsByName(name);
}
}
/**
* Get a single application out of the registry.
*
* @param id The application identifier.
* @return The registered application.
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> get(@PathVariable String id) {
LOGGER.debug("Deliver registered application with ID '{}'", id);
Application application = registry.getApplication(id);
if (application != null) {
return ResponseEntity.ok(application);
} else {
return ResponseEntity.notFound().build();
}
}
/**
* Get a single application out of the registry.
*
* @param id The application identifier.
* @return The registered application.
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> get(@PathVariable String id) {
LOGGER.debug("Deliver registered application with ID '{}'", id);
Application application = registry.getApplication(id);
if (application != null) {
return ResponseEntity.ok(application);
} else {
return ResponseEntity.notFound().build();
}
}
/**
* Unregister an application within this admin application.
*
* @param id The application id.
* @return the unregistered application.
*/
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> unregister(@PathVariable String id) {
LOGGER.debug("Unregister application with ID '{}'", id);
Application application = registry.deregister(id);
if (application != null) {
return ResponseEntity.ok(application);
} else {
return ResponseEntity.notFound().build();
}
}
/**
* Unregister an application within this admin application.
*
* @param id The application id.
* @return the unregistered application.
*/
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> unregister(@PathVariable String id) {
LOGGER.debug("Unregister application with ID '{}'", id);
Application application = registry.deregister(id);
if (application != null) {
return ResponseEntity.ok(application);
} else {
return ResponseEntity.notFound().build();
}
}
}
......@@ -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;
......@@ -42,76 +47,78 @@ import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
public class RegistryControllerTest {
private static final String APPLICATION_TEST_JSON;
private static final String APPLICATION_TWICE_JSON;
static {
try {
APPLICATION_TEST_JSON = new JSONObject().put("name", "test")
.put("healthUrl", "http://localhost/mgmt/health").toString();
APPLICATION_TWICE_JSON = new JSONObject().put("name", "twice")
.put("healthUrl", "http://localhost/mgmt/health").toString();
} catch (JSONException ex) {
throw new ExceptionInInitializerError(ex);
}
}
private MockMvc mvc;
@Before
public void setup() {
ApplicationRegistry registry = new ApplicationRegistry(new SimpleApplicationStore(),
new HashingApplicationUrlIdGenerator());
registry.setApplicationEventPublisher(Mockito.mock(ApplicationEventPublisher.class));
mvc = MockMvcBuilders.standaloneSetup(new RegistryController(registry)).build();
}
@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();
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"))
.andExpect(jsonPath("$.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())
.andExpect(jsonPath("$[0].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())
.andExpect(jsonPath("$.id").value(id));
mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isNotFound());
}
private String extractId(MvcResult result) throws UnsupportedEncodingException {
return JsonPath.compile("$.id").read(result.getResponse().getContentAsString());
}
@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())
.andExpect(jsonPath("$").isEmpty());
}
@Test
public void test_delete_notFound() throws Exception {
mvc.perform(delete("/api/applications/unknown")).andExpect(status().isNotFound());
}
private static final String APPLICATION_TEST_JSON;
private static final String APPLICATION_TWICE_JSON;
static {
try {
APPLICATION_TEST_JSON = new JSONObject().put("name", "test")
.put("healthUrl", "http://localhost/mgmt/health")
.toString();
APPLICATION_TWICE_JSON = new JSONObject().put("name", "twice")
.put("healthUrl", "http://localhost/mgmt/health")
.toString();
} catch (JSONException ex) {
throw new ExceptionInInitializerError(ex);
}
}
private MockMvc mvc;
@Before
public void setup() {
ApplicationRegistry registry = new ApplicationRegistry(new SimpleApplicationStore(),
new HashingApplicationUrlIdGenerator());
registry.setApplicationEventPublisher(Mockito.mock(ApplicationEventPublisher.class));
mvc = MockMvcBuilders.standaloneSetup(new RegistryController(registry)).build();
}
@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(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(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?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(delete("/api/applications/{id}", id))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(id));
mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isNotFound());
}
private String extractId(MvcResult result) throws UnsupportedEncodingException {
return JsonPath.compile("$.id").read(result.getResponse().getContentAsString());
}
@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())
.andExpect(jsonPath("$").isEmpty());
}
@Test
public void test_delete_notFound() throws Exception {
mvc.perform(delete("/api/applications/unknown")).andExpect(status().isNotFound());
}
}
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