xtern commented on a change in pull request #7648:
URL: https://github.com/apache/ignite/pull/7648#discussion_r413057642
##########
File path:
modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
##########
@@ -344,6 +355,252 @@ private void checkJson(String json, Person p) throws
IOException {
assertEquals(p.getSalary(), res.get("salary").asDouble());
}
+ /**
+ * Test that after adding the object to the cache, it is available for
query.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testPutJsonQueryEntity() throws Exception {
+ String cacheName = "person";
+
+ Person simple = new Person(100, "John", "Doe", 10000);
+
+ putObject(cacheName, "300", simple);
+
+ String ret = content(cacheName, GridRestCommand.CACHE_GET,
+ "keyType", "int",
+ "key", "300"
+ );
+
+ info("Get command result: " + ret);
+
+ checkJson(ret, simple);
+
+ JsonNode val = queryObject(
+ cacheName,
+ "type", Person.class.getSimpleName(),
+ "pageSize", "1",
+ "qry", "orgId = ?",
+ "arg1", String.valueOf(simple.getOrganizationId())
+ );
+
+ assertEquals(simple.getOrganizationId().intValue(),
val.get("orgId").intValue());
+ assertEquals(simple.getSalary(), val.get("salary").doubleValue());
+ assertEquals(simple.getFirstName(), val.get("firstName").textValue());
+ assertEquals(simple.getLastName(), val.get("lastName").textValue());
+
+ initCache();
Review comment:
Done
##########
File path:
modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
##########
@@ -344,6 +355,252 @@ private void checkJson(String json, Person p) throws
IOException {
assertEquals(p.getSalary(), res.get("salary").asDouble());
}
+ /**
+ * Test that after adding the object to the cache, it is available for
query.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testPutJsonQueryEntity() throws Exception {
+ String cacheName = "person";
+
+ Person simple = new Person(100, "John", "Doe", 10000);
+
+ putObject(cacheName, "300", simple);
+
+ String ret = content(cacheName, GridRestCommand.CACHE_GET,
+ "keyType", "int",
+ "key", "300"
+ );
+
+ info("Get command result: " + ret);
+
+ checkJson(ret, simple);
+
+ JsonNode val = queryObject(
+ cacheName,
+ "type", Person.class.getSimpleName(),
+ "pageSize", "1",
+ "qry", "orgId = ?",
+ "arg1", String.valueOf(simple.getOrganizationId())
+ );
+
+ assertEquals(simple.getOrganizationId().intValue(),
val.get("orgId").intValue());
+ assertEquals(simple.getSalary(), val.get("salary").doubleValue());
+ assertEquals(simple.getFirstName(), val.get("firstName").textValue());
+ assertEquals(simple.getLastName(), val.get("lastName").textValue());
+
+ initCache();
+ }
+
+ /**
+ * Test adding a new (unregistered) binary object.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ @SuppressWarnings("ThrowableNotThrown")
+ public void testPutUnregistered() throws Exception {
+ LinkedHashMap<String, String> fields = new LinkedHashMap<>();
+
+ fields.put("id", Long.class.getName());
+ fields.put("name", String.class.getName());
+ fields.put("timestamp", Timestamp.class.getName());
+ fields.put("bytes", long[].class.getName());
+ fields.put("igniteUuid", IgniteUuid.class.getName());
+
+ CacheConfiguration<Object, Object> ccfg = new
CacheConfiguration<>("testCache");
+
+ String valType = "SomeNewType";
+
+ ccfg.setQueryEntities(
+ Collections.singletonList(
+ new QueryEntity()
+ .setKeyType("java.lang.Integer")
+ .setValueType(valType)
+ .setFields(fields)
+ .setIndexes(Collections.singleton(new QueryIndex("id"))))
+ );
+
+ IgniteCache cache = grid(0).createCache(ccfg);
+
+ List<Integer> list = F.asList(1, 2, 3);
+
+ OuterClass newType = new OuterClass(Long.MAX_VALUE, "unregistered",
0.1d, list,
+ Timestamp.valueOf("2004-08-26 16:47:03.141592"), new long[]
{Long.MAX_VALUE, -1, Long.MAX_VALUE},
+ UUID.randomUUID(), IgniteUuid.randomUuid(), null);
+
+ putObject(cache.getName(), "300", newType, valType);
+
+ GridTestUtils.assertThrowsWithCause(() -> cache.get(300),
ClassNotFoundException.class);
+
+ assertEquals(newType, getObject(cache.getName(), "300",
OuterClass.class));
+
+ // Sending "optional" (new) field for registered binary type.
+ OuterClass newTypeUpdate = new OuterClass(-1, "update", 0.7d, list,
+ Timestamp.valueOf("2004-08-26 16:47:03.14"), new long[]
{Long.MAX_VALUE, 0, Long.MAX_VALUE},
+ UUID.randomUUID(), IgniteUuid.randomUuid(), new
OuterClass.OptionalObject("test"));
+
+ putObject(cache.getName(), "301", newTypeUpdate, valType);
+
+ assertEquals(newTypeUpdate, getObject(cache.getName(), "301",
OuterClass.class));
+
+ // Check query result.
+ JsonNode res = queryObject(
+ cache.getName(),
+ "type", valType,
+ "pageSize", "1",
+ "keepBinary", "true",
+ "qry", "timestamp < ?",
+ "arg1", "2004-08-26 16:47:03.141"
+ );
+
+ assertEquals(newTypeUpdate, JSON_MAPPER.treeToValue(res,
OuterClass.class));
+
+ grid(0).destroyCache(ccfg.getName());
+ }
+
+ /**
+ * Check serialization of the nested binary object.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testPutNestedBinaryObject() throws Exception {
+ IgniteBinary binary = grid(0).binary();
+
+ BinaryObjectBuilder nested = binary.builder("Nested");
+
+ nested.setField("str1", "stringValue");
+ nested.setField("sqlDate", java.sql.Date.valueOf("2019-01-01"));
+
+ // Registering "Nested" type.
+ jcache().put(-1, nested.build());
+
+ BinaryObjectBuilder parent = binary.builder("Parent");
+
+ parent.setField("nested", nested.build());
+ parent.setField("id", Long.MAX_VALUE);
+ parent.setField("byteVal", (byte)1);
+ parent.setField("timestamp", new Timestamp(new
java.util.Date().getTime()));
+
+ BinaryObject obj = parent.build();
+
+ // Registering "Parent" type.
+ jcache().put(2, obj);
+
+ // Adding another "Parent" object via REST.
+ JsonNode jsonNode = JSON_MAPPER.valueToTree(obj);
+
+ ((ObjectNode)jsonNode).put("id", Long.MIN_VALUE);
+
+ String jsonText = JSON_MAPPER.writeValueAsString(jsonNode);
+
+ info("Put: " + jsonText);
+
+ String ret = content(DEFAULT_CACHE_NAME, GridRestCommand.CACHE_PUT,
+ "keyType", "int",
+ "key", "3",
+ "valueType", "Parent",
+ "val", jsonText
+ );
+
+ info("Put command result: " + ret);
+
+ assertResponseSucceeded(ret, false);
+
+ ret = content(DEFAULT_CACHE_NAME, GridRestCommand.CACHE_GET,
"keyType", "int", "key", "3");
+
+ info("Get command result: " + ret);
+
+ JsonNode res = assertResponseSucceeded(ret, false);
+
+ assertEquals(jsonNode, res);
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testPutComplexObject() throws Exception {
+ String cacheName = "complex";
+
+ DateFormat df = JSON_MAPPER.getDateFormat();
+
+ OuterClass[] objects = new OuterClass[] {
+ new OuterClass(111, "out-0", 0.7d, F.asList(9, 1)),
+ new OuterClass(112, "out-1", 0.1d, F.asList(9, 1)),
+ new OuterClass(113, "out-2", 0.3d, F.asList(9, 1))
+ };
+
+ Complex complex = new Complex(
+ 1234567,
+ "String value",
+ new Timestamp(Calendar.getInstance().getTime().getTime()),
+ Date.valueOf("1986-04-26"),
+ Time.valueOf("23:59:59"),
+ df.parse(df.format(Calendar.getInstance().getTime())),
+ F.asMap("key1", 1, "key2", 2),
+ new int[] {1, 2, 3},
+ F.asList(11, 22, 33).toArray(new Integer[3]),
+ new long[] {Long.MIN_VALUE, 0, Long.MAX_VALUE},
+ F.asList(4, 5, 6),
+ F.asMap(123, null, 4567, null).keySet(),
+ new byte[] {4, 1, 2},
+ new char[] {'a', 'b', 'c'},
+ Color.GREEN,
+ new OuterClass(Long.MIN_VALUE, "outer", 0.7d, F.asList(9, 1)),
+ objects,
+ UUID.randomUUID(),
+ IgniteUuid.randomUuid()
+ );
+
+ putObject(cacheName, "300", complex);
+
+ assertEquals(complex, getObject(cacheName, "300", Complex.class));
+ assertEquals(complex, grid(0).cache(cacheName).get(300));
+
+ // Check query result.
+ JsonNode res = queryObject(
+ cacheName,
+ "type", Complex.class.getSimpleName(),
+ "pageSize", "1",
+ "qry", "sqlDate > ? and sqlDate < ?",
+ "arg1", "1953-03-05",
+ "arg2", "2011-03-11"
+ );
+
+ assertEquals(complex, JSON_MAPPER.treeToValue(res, Complex.class));
+ }
+
+ /**
+ * Test adding a system (java.*) type object.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testPutJvmType() throws Exception {
+ Map<String, int[]> map = U.map("1", new int[] {1, 2, 3});
+ putObject(DEFAULT_CACHE_NAME, "1", map, Map.class.getName());
+ assertTrue(Map.class.isAssignableFrom(jcache().get(1).getClass()));
+
+ Set<int[]> set = new HashSet<>(map.values());
+ putObject(DEFAULT_CACHE_NAME, "2", set, Set.class.getName());
+ assertTrue(Set.class.isAssignableFrom(jcache().get(2).getClass()));
+
+ List<int[]> list = new ArrayList<>(set);
+ putObject(DEFAULT_CACHE_NAME, "3", list, Collection.class.getName());
+
assertTrue(Collection.class.isAssignableFrom(jcache().get(3).getClass()));
+
+ int[] ints = {1, 2, 3};
+ putObject(DEFAULT_CACHE_NAME, "4", ints, int[].class.getName());
+ assertTrue(Arrays.equals(ints, (int[])jcache().get(4)));
+
+ Character[] chars = {'a', 'b', 'c'};
+ putObject(DEFAULT_CACHE_NAME, "5", chars, Character[].class.getName());
+ assertTrue(Arrays.equals(chars, (Object[])jcache().get(5)));
+ }
Review comment:
Done
##########
File path:
modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
##########
@@ -344,6 +355,252 @@ private void checkJson(String json, Person p) throws
IOException {
assertEquals(p.getSalary(), res.get("salary").asDouble());
}
+ /**
+ * Test that after adding the object to the cache, it is available for
query.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testPutJsonQueryEntity() throws Exception {
+ String cacheName = "person";
+
+ Person simple = new Person(100, "John", "Doe", 10000);
+
+ putObject(cacheName, "300", simple);
+
+ String ret = content(cacheName, GridRestCommand.CACHE_GET,
+ "keyType", "int",
+ "key", "300"
+ );
+
+ info("Get command result: " + ret);
+
+ checkJson(ret, simple);
+
+ JsonNode val = queryObject(
+ cacheName,
+ "type", Person.class.getSimpleName(),
+ "pageSize", "1",
+ "qry", "orgId = ?",
+ "arg1", String.valueOf(simple.getOrganizationId())
+ );
+
+ assertEquals(simple.getOrganizationId().intValue(),
val.get("orgId").intValue());
+ assertEquals(simple.getSalary(), val.get("salary").doubleValue());
+ assertEquals(simple.getFirstName(), val.get("firstName").textValue());
+ assertEquals(simple.getLastName(), val.get("lastName").textValue());
+
+ initCache();
+ }
+
+ /**
+ * Test adding a new (unregistered) binary object.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ @SuppressWarnings("ThrowableNotThrown")
+ public void testPutUnregistered() throws Exception {
+ LinkedHashMap<String, String> fields = new LinkedHashMap<>();
+
+ fields.put("id", Long.class.getName());
+ fields.put("name", String.class.getName());
+ fields.put("timestamp", Timestamp.class.getName());
+ fields.put("bytes", long[].class.getName());
+ fields.put("igniteUuid", IgniteUuid.class.getName());
+
+ CacheConfiguration<Object, Object> ccfg = new
CacheConfiguration<>("testCache");
+
+ String valType = "SomeNewType";
+
+ ccfg.setQueryEntities(
+ Collections.singletonList(
+ new QueryEntity()
+ .setKeyType("java.lang.Integer")
+ .setValueType(valType)
+ .setFields(fields)
+ .setIndexes(Collections.singleton(new QueryIndex("id"))))
+ );
+
+ IgniteCache cache = grid(0).createCache(ccfg);
+
+ List<Integer> list = F.asList(1, 2, 3);
+
+ OuterClass newType = new OuterClass(Long.MAX_VALUE, "unregistered",
0.1d, list,
+ Timestamp.valueOf("2004-08-26 16:47:03.141592"), new long[]
{Long.MAX_VALUE, -1, Long.MAX_VALUE},
+ UUID.randomUUID(), IgniteUuid.randomUuid(), null);
+
+ putObject(cache.getName(), "300", newType, valType);
+
+ GridTestUtils.assertThrowsWithCause(() -> cache.get(300),
ClassNotFoundException.class);
+
+ assertEquals(newType, getObject(cache.getName(), "300",
OuterClass.class));
+
+ // Sending "optional" (new) field for registered binary type.
+ OuterClass newTypeUpdate = new OuterClass(-1, "update", 0.7d, list,
+ Timestamp.valueOf("2004-08-26 16:47:03.14"), new long[]
{Long.MAX_VALUE, 0, Long.MAX_VALUE},
+ UUID.randomUUID(), IgniteUuid.randomUuid(), new
OuterClass.OptionalObject("test"));
+
+ putObject(cache.getName(), "301", newTypeUpdate, valType);
+
+ assertEquals(newTypeUpdate, getObject(cache.getName(), "301",
OuterClass.class));
+
+ // Check query result.
+ JsonNode res = queryObject(
Review comment:
Done
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]