From aad03f5794ae972303be72781d83d752022382b6 Mon Sep 17 00:00:00 2001 From: clarsen Date: Thu, 26 Sep 2013 17:53:09 -0400 Subject: [PATCH] Fix TestPutRpc unit tests to look for string in the JSON and avoid potential problems with unknown field ordering. Signed-off-by: Chris Larsen --- test/tsd/TestPutRpc.java | 202 +++++++++++++++++++++------------------ 1 file changed, 109 insertions(+), 93 deletions(-) diff --git a/test/tsd/TestPutRpc.java b/test/tsd/TestPutRpc.java index 9ea7a6f646..7a0668b237 100644 --- a/test/tsd/TestPutRpc.java +++ b/test/tsd/TestPutRpc.java @@ -15,6 +15,7 @@ import static org.mockito.Mockito.when; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.nio.charset.Charset; import java.util.HashMap; @@ -101,8 +102,10 @@ public void putSingleSummary() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.OK, query.response().getStatus()); - assertEquals("{\"failed\":0,\"success\":1}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"failed\":0")); + assertTrue(response.contains("\"success\":1")); } @Test @@ -113,8 +116,11 @@ public void putSingleDetails() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.OK, query.response().getStatus()); - assertEquals("{\"errors\":[],\"failed\":0,\"success\":1}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"failed\":0")); + assertTrue(response.contains("\"success\":1")); + assertTrue(response.contains("\"errors\":[]")); } @Test @@ -125,8 +131,11 @@ public void putSingleSummaryAndDetails() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.OK, query.response().getStatus()); - assertEquals("{\"errors\":[],\"failed\":0,\"success\":1}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"failed\":0")); + assertTrue(response.contains("\"success\":1")); + assertTrue(response.contains("\"errors\":[]")); } @Test @@ -139,8 +148,10 @@ public void putDoubleSummary() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.OK, query.response().getStatus()); - assertEquals("{\"failed\":0,\"success\":2}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"failed\":0")); + assertTrue(response.contains("\"success\":2")); } @Test @@ -294,11 +305,11 @@ public void noSuchUniqueName() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"doesnotexist\"," - + "\"timestamp\":1365465600,\"value\":\"42\",\"tags\":{\"host\":" - + "\"web01\"}},\"error\":\"Unknown metric\"}],\"failed\":1," - + "\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Unknown metric\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -309,10 +320,11 @@ public void missingMetric() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":null,\"timestamp\"" - + ":1365465600,\"value\":\"42\",\"tags\":{\"host\":\"web01\"}}," - + "\"error\":\"Metric name was empty\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Metric name was empty\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -323,10 +335,11 @@ public void nullMetric() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":null,\"timestamp\"" - + ":1365465600,\"value\":\"42\",\"tags\":{\"host\":\"web01\"}}," - + "\"error\":\"Metric name was empty\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Metric name was empty\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -337,10 +350,11 @@ public void missingTimestamp() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":0,\"value\":\"42\",\"tags\":{\"host\":\"web01\"}}," - + "\"error\":\"Invalid timestamp\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Invalid timestamp\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -351,10 +365,11 @@ public void nullTimestamp() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":0,\"value\":\"42\",\"tags\":{\"host\":\"web01\"}}," - + "\"error\":\"Invalid timestamp\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Invalid timestamp\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -365,10 +380,11 @@ public void invalidTimestamp() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":-1,\"value\":\"42\",\"tags\":{\"host\":\"web01\"}}," - + "\"error\":\"Invalid timestamp\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Invalid timestamp\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -379,11 +395,11 @@ public void missingValue() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":null,\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Empty value\"}],\"failed\":1," - + "\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Empty value\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -394,11 +410,11 @@ public void nullValue() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":null,\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Empty value\"}],\"failed\":1," - + "\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Empty value\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -409,11 +425,11 @@ public void emptyValue() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"\",\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Empty value\"}],\"failed\":1," - + "\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Empty value\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -424,11 +440,11 @@ public void badValue() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"notanumber\",\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Unable to parse value to a number" - + "\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Unable to parse value to a number\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -439,11 +455,11 @@ public void ValueNaN() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"NaN\",\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Unable to parse value to a number" - + "\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Unable to parse value to a number\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test (expected = BadRequestException.class) @@ -463,11 +479,11 @@ public void ValueINF() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"+INF\",\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Unable to parse value to a number" - + "\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Unable to parse value to a number\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -478,11 +494,11 @@ public void ValueNINF() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"-INF\",\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Unable to parse value to a number" - + "\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Unable to parse value to a number\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test (expected = BadRequestException.class) @@ -511,11 +527,11 @@ public void ValueInfiniy() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"+Infinity\",\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Unable to parse value to a number" - + "\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Unable to parse value to a number\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -526,11 +542,11 @@ public void ValueNInfiniy() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"-Infinity\",\"tags\":" - + "{\"host\":\"web01\"}},\"error\":\"Unable to parse value to a number" - + "\"}],\"failed\":1,\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Unable to parse value to a number\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test (expected = BadRequestException.class) @@ -550,11 +566,11 @@ public void missingTags() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"42\",\"tags\":" - + "null},\"error\":\"Missing tags\"}],\"failed\":1," - + "\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Missing tags\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -565,11 +581,11 @@ public void nullTags() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"42\",\"tags\":" - + "null},\"error\":\"Missing tags\"}],\"failed\":1," - + "\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Missing tags\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } @Test @@ -580,10 +596,10 @@ public void emptyTags() throws Exception { PutDataPointRpc put = new PutDataPointRpc(); put.execute(tsdb, query); assertEquals(HttpResponseStatus.BAD_REQUEST, query.response().getStatus()); - assertEquals("{\"errors\":[{\"datapoint\":{\"metric\":\"sys.cpu.nice\"," - + "\"timestamp\":1365465600,\"value\":\"42\",\"tags\":" - + "{}},\"error\":\"Missing tags\"}],\"failed\":1," - + "\"success\":0}", - query.response().getContent().toString(Charset.forName("UTF-8"))); + final String response = + query.response().getContent().toString(Charset.forName("UTF-8")); + assertTrue(response.contains("\"error\":\"Missing tags\"")); + assertTrue(response.contains("\"failed\":1")); + assertTrue(response.contains("\"success\":0")); } }