Skip to content

Commit

Permalink
Fix TestPutRpc unit tests to look for string in the JSON and avoid po…
Browse files Browse the repository at this point in the history
…tential

problems with unknown field ordering.

Signed-off-by: Chris Larsen <clarsen@euphoriaaudio.com>
  • Loading branch information
manolama committed Sep 26, 2013
1 parent 4ad0659 commit aad03f5
Showing 1 changed file with 109 additions and 93 deletions.
202 changes: 109 additions & 93 deletions test/tsd/TestPutRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"));
}
}

0 comments on commit aad03f5

Please sign in to comment.