Skip to content
Permalink
Browse files

test: Uniform tests for default doDelete methods

  • Loading branch information...
rhwood committed May 9, 2019
1 parent 36fc2e5 commit 810b7770f3714ec11750c37f776bc01f4346d14e
@@ -2,6 +2,12 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNotNull;

import java.util.Locale;

import javax.annotation.OverridingMethodsMustInvokeSuper;
@@ -11,6 +17,7 @@
import jmri.server.json.schema.JsonSchemaServiceCache;
import jmri.util.JUnitUtil;
import org.junit.Assert;
import org.junit.Test;

/**
* Common methods for JMRI JSON Service HTTP provider tests.
@@ -31,7 +38,7 @@
@OverridingMethodsMustInvokeSuper
public void setUp() throws Exception {
JUnitUtil.setUp();
this.mapper = new ObjectMapper();
mapper = new ObjectMapper();
// require valid inputs and outputs for tests by default
InstanceManager.getDefault(JsonServerPreferences.class).setValidateClientMessages(true);
InstanceManager.getDefault(JsonServerPreferences.class).setValidateServerMessages(true);
@@ -43,11 +50,24 @@ public void setUp() throws Exception {
*/
@OverridingMethodsMustInvokeSuper
public void tearDown() throws Exception {
this.service = null;
this.mapper = null;
service = null;
mapper = null;
JUnitUtil.tearDown();
}

@Test
public void testDoDelete() throws JsonException {
try {
assumeNotNull(service); // protect against JUnit tests in Eclipse that test this class directly
service.doDelete("foo", "foo", NullNode.getInstance(), locale, 42);
fail("Expected exception not thrown.");
} catch (JsonException ex) {
assertEquals("Code is HTTP METHOD NOT ALLOWED", 405, ex.getCode());
assertEquals("Message", "Deleting foo is not allowed.", ex.getMessage());
assertEquals("ID is 42", 42, ex.getId());
}
}

/**
* Validate a JsonNode message produced by the JMRI JSON server against
* published JMRI JSON service schema. Asserts a failure if the node is not
@@ -18,7 +18,7 @@
*
* @author Randall Wood Copyright 2018
*/
public class JsonNamedBeanHttpServiceTest extends JsonHttpServiceTestBase<JsonNamedBeanHttpService<Turnout>> {
public class JsonNamedBeanHttpServiceTest extends JsonNamedBeanHttpServiceTestBase<Turnout, JsonNamedBeanHttpService<Turnout>> {

public JsonNamedBeanHttpServiceTest() {
}
@@ -69,7 +69,6 @@ public void testDoGet() throws Exception {
*/
@Test
public void testGetNamedBean() throws Exception {
Turnout bean = null;
String name = "non-existant";
String type = "non-existant";
try {
@@ -95,7 +94,7 @@ public void testGetNamedBeanWithProperties() throws Exception {
String name = "IT1";
// retain turnout as NamedBean to ensure only "generic" NamedBean
// methods are used
Turnout bean = InstanceManager.getDefault(TurnoutManager.class).provide(name);
bean = InstanceManager.getDefault(TurnoutManager.class).provide(name);
bean.setUserName("Turnout 1");
bean.setComment("Turnout Comment");
bean.setProperty("foo", "bar");
@@ -132,7 +131,6 @@ public void testGetNamedBeanWithProperties() throws Exception {
*/
@Test
public void testPostNamedBean() throws Exception {
Turnout bean = null;
String name = "non-existant";
String type = "non-existant";
try {
@@ -145,5 +143,4 @@ public void testPostNamedBean() throws Exception {
Assert.assertEquals("Message Id", 42, ex.getId());
}
}

}
@@ -1,10 +1,17 @@
package jmri.server.json;

import jmri.NamedBean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNotNull;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.node.NullNode;

/**
* Test handling of null, or non-existent Named Beans. Testing of existent, or
* non-null Named Beans is covered elsewhere.
@@ -15,7 +22,6 @@
*/
public abstract class JsonNamedBeanHttpServiceTestBase<B extends NamedBean, S extends JsonNamedBeanHttpService<B>> extends JsonHttpServiceTestBase<S> {

protected S service;
protected B bean = null;

@After
@@ -92,4 +98,17 @@ public void testPostNamedBean() throws Exception {
}
}

@Test
@Override
public void testDoDelete() throws JsonException {
try {
assumeNotNull(service); // protect against JUnit tests in Eclipse that test this class directly
service.doDelete(service.getType(), "non-existant", NullNode.getInstance(), locale, 42);
fail("Expected exception not thrown.");
} catch (JsonException ex) {
assertEquals("Code is HTTP METHOD NOT ALLOWED", 405, ex.getCode());
assertEquals("Message", "Deleting " + service.getType() + " is not allowed.", ex.getMessage());
assertEquals("ID is 42", 42, ex.getId());
}
}
}
@@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.StdDateFormat;

import static org.junit.Assert.assertEquals;
@@ -143,6 +144,36 @@ public void testDoPut() throws IOException, JsonException {
}
}

@Test
@Override
public void testDoDelete() throws JsonException {
ObjectNode message = mapper.createObjectNode();
IdTagManager manager = InstanceManager.getDefault(IdTagManager.class);
manager.provide("ID1");
// delete an idTag
assertNotNull(manager.getBeanBySystemName("ID1"));
try {
// first attempt should fail on conflict
service.doDelete(JsonIdTag.IDTAG, "ID1", NullNode.getInstance(), locale, 0);
fail("Expected exception not thrown.");
} catch (JsonException ex) {
assertEquals(409, ex.getCode());
assertEquals("Manager", ex.getAdditionalData().path(JSON.CONFLICT).path(0).asText());
message = message.put(JSON.FORCE_DELETE, ex.getAdditionalData().path(JSON.FORCE_DELETE).asText());
}
assertNotNull(manager.getBeanBySystemName("ID1"));
// will throw if prior catch failed
service.doDelete(JsonIdTag.IDTAG, "ID1", message, locale, 0);
assertNull(manager.getBeanBySystemName("ID1"));
try {
// deleting again should throw an exception
service.doDelete(JsonIdTag.IDTAG, "ID1", NullNode.getInstance(), locale, 0);
fail("Expected exception not thrown.");
} catch (JsonException ex) {
assertEquals(404, ex.getCode());
}
}

/**
* Test of doGetList method, of class JsonIdTagHttpService.
* @throws java.lang.Exception rethrows any exceptions from instance.doGetList()
@@ -9,7 +9,6 @@
import static org.junit.Assert.fail;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Light;
@@ -130,17 +129,6 @@ public void testDoGetList() throws JsonException {
this.validate(result);
}

@Test
public void testDelete() {
try {
service.doDelete(JsonLight.LIGHT, "", NullNode.getInstance(), locale, 0);
} catch (JsonException ex) {
assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getCode());
return;
}
fail("Did not throw expected error.");
}

@Before
@Override
public void setUp() throws Exception {
@@ -6,9 +6,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import javax.servlet.http.HttpServletResponse;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Memory;
@@ -98,16 +96,6 @@ public void testDoGetList() throws JsonException {
assertEquals(2, result.size());
}

@Test
public void testDelete() {
try {
service.doDelete(JsonMemory.MEMORY, "", NullNode.getInstance(), locale, 42);
fail("Did not throw expected error.");
} catch (JsonException ex) {
assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getCode());
}
}

@Before
public void setUp() throws Exception {
super.setUp();
@@ -98,7 +98,7 @@ public void testDoGetList() throws JsonException {
}

@Test
public void testDelete() {
public void testDoDelete() {
try {
service.doDelete(JsonPowerServiceFactory.POWER, "", NullNode.getInstance(), locale, 42);
Assert.fail("Expected exception not thrown");
@@ -9,7 +9,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;

import javax.servlet.http.HttpServletResponse;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Reporter;
@@ -151,16 +150,6 @@ public void testDoGetList() throws JsonException {
assertEquals(2, result.size());
}

@Test
public void testDelete() {
try {
service.doDelete(REPORTER, "", NullNode.getInstance(), locale, 42);
fail("Did not throw expected error.");
} catch (JsonException ex) {
assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getCode());
}
}

// The minimal setup for log4J
@Before
public void setUp() throws Exception {
@@ -9,7 +9,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import javax.servlet.http.HttpServletResponse;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.Route;
@@ -218,16 +217,6 @@ public void testDoGetList() throws JsonException {
assertEquals(2, result.size());
}

@Test
public void testDelete() {
try {
service.doDelete(JsonRouteServiceFactory.ROUTE, "", NullNode.getInstance(), locale, 42);
fail("Did not throw expected error.");
} catch (JsonException ex) {
assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getCode());
}
}

@Before
public void setUp() throws Exception {
super.setUp();
@@ -155,17 +155,6 @@ public void testDoGetList() throws JsonException {
assertEquals(2, result.size());
}

@Test
public void testDelete() {
try {
service.doDelete(JsonSensor.SENSOR, "", NullNode.getInstance(), locale, 42);
} catch (JsonException ex) {
assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getCode());
return;
}
fail("Did not throw expected error.");
}

@Before
public void setUp() throws Exception {
super.setUp();
@@ -134,17 +134,6 @@ public void testDoGetList() throws JsonException {
assertEquals(2, result.size());
}

@Test
public void testDelete() {
try {
service.doDelete(JsonTurnoutServiceFactory.TURNOUT, "", NullNode.getInstance(), locale, 42);
} catch (JsonException ex) {
assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getCode());
return;
}
fail("Did not throw expected error.");
}

@Before
@Override
public void setUp() throws Exception {

0 comments on commit 810b777

Please sign in to comment.
You can’t perform that action at this time.