DATACOUCH-108 - Fix nondeterministic JSON response assertions.

This commit is contained in:
Michael Nitschinger
2014-09-05 08:25:13 +02:00
parent 502d17e6b7
commit 9f35068629
2 changed files with 25 additions and 16 deletions

View File

@@ -19,6 +19,8 @@ package org.springframework.data.couchbase.core;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.client.protocol.views.Stale;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.spy.memcached.CASValue;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -42,7 +44,11 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Michael Nitschinger
@@ -52,6 +58,11 @@ import static org.junit.Assert.*;
@TestExecutionListeners(CouchbaseTemplateViewListener.class)
public class CouchbaseTemplateTests {
/**
* JSON object mapper to verify results.
*/
private static final ObjectMapper MAPPER = new ObjectMapper();
@Autowired
private CouchbaseClient client;
@@ -68,10 +79,11 @@ public class CouchbaseTemplateTests {
template.save(beer);
String result = (String) client.get(id);
String expected = "{\"_class\":\"org.springframework.data.couchbase.core.Beer\""
+ ",\"is_active\":false,\"name\":\"The Awesome Stout\"}";
assertNotNull(result);
assertEquals(expected, result);
Map<String, Object> converted = MAPPER.readValue(result, new TypeReference<Map<String, Object>>(){});
assertEquals("org.springframework.data.couchbase.core.Beer", converted.get("_class"));
assertEquals(false, converted.get("is_active"));
assertEquals("The Awesome Stout", converted.get("name"));
}
@Test
@@ -89,18 +101,20 @@ public class CouchbaseTemplateTests {
String id = "double-insert-test";
client.delete(id).get();
String expected = "{\"_class\":\"org.springframework.data.couchbase.core."
+ "CouchbaseTemplateTests$SimplePerson\",\"name\":\"Mr. A\"}";
SimplePerson doc = new SimplePerson(id, "Mr. A");
template.insert(doc);
String result = (String) client.get(id);
assertEquals(expected, result);
Map<String, Object> converted = MAPPER.readValue(result, new TypeReference<Map<String, Object>>(){});
assertEquals("org.springframework.data.couchbase.core.CouchbaseTemplateTests$SimplePerson", converted.get("_class"));
assertEquals("Mr. A", converted.get("name"));
doc = new SimplePerson(id, "Mr. B");
template.insert(doc);
result = (String) client.get(id);
assertEquals(expected, result);
converted = MAPPER.readValue(result, new TypeReference<Map<String, Object>>(){});
assertEquals("org.springframework.data.couchbase.core.CouchbaseTemplateTests$SimplePerson", converted.get("_class"));
assertEquals("Mr. A", converted.get("name"));
}
@@ -146,12 +160,6 @@ public class CouchbaseTemplateTests {
template.save(complex);
String expected = "{\"_class\":\"org.springframework.data.couchbase.core."
+ "CouchbaseTemplateTests$ComplexPerson\",\"info1\":{\"nullValue\":null,\"foo\":true,\"bar\""
+ ":false},\"votes\":[],\"firstnames\":[\"Michael\",\"Thomas\",null],\"info2\":"
+ "{}}";
assertEquals(expected, client.get(id));
ComplexPerson response = template.findById(id, ComplexPerson.class);
assertEquals(names, response.getFirstnames());
assertEquals(votes, response.getVotes());

View File

@@ -194,7 +194,8 @@ public class MappingCouchbaseConverterTests {
expected.put("attr1", 0);
converter.write(entity, converted);
assertEquals(expected.toString(), converted.export().toString());
assertEquals(expected.get("_class"), converted.get("_class"));
assertEquals(expected.get("attr1"), converted.get("attr1"));
}
@Test