List of usage examples for com.fasterxml.jackson.databind.node ObjectNode equals
public boolean equals(Object paramObject)
From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchIndexUtils.java
/** Creates a list of JsonNodes containing the mapping for fields that will _enable_ or _disable_ field data depending on fielddata_info is present * (note this can convert a property to a dynamic template, but never the other way round) * @param instream/*from w w w.ja v a 2s .c o m*/ * @param f * @param field_lookups * @param fielddata_info 3tuple containing not_analyzed, analyzed, and override * @param mapper * @return */ protected static Stream<Tuple2<Either<String, Tuple2<String, String>>, JsonNode>> createFieldLookups( final Stream<String> instream, final Function<String, Either<String, Tuple2<String, String>>> f, final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> field_lookups, final Optional<Tuple3<JsonNode, JsonNode, Boolean>> fielddata_info, final SearchIndexSchemaDefaultBean search_index_schema_override, final Map<Either<String, Tuple2<String, String>>, String> type_override, final ObjectMapper mapper, final String index_type) { return instream.<Tuple2<Either<String, Tuple2<String, String>>, JsonNode>>map(Lambdas.wrap_u(fn -> { final Either<String, Tuple2<String, String>> either_tmp = f.apply(fn); final Optional<String> maybe_type = Optional.ofNullable(type_override.get(either_tmp)); // add type if present final Either<String, Tuple2<String, String>> either = maybe_type .<Either<String, Tuple2<String, String>>>map(type -> { return either_tmp.<Either<String, Tuple2<String, String>>>either(s -> Either.left(s), t2 -> Either.right(Tuples._2T(t2._1(), type))); }).orElse(either_tmp); final ObjectNode mutable_field_metadata = (ObjectNode) Optional.ofNullable(field_lookups.get(either)) .map(j -> j.deepCopy()) .orElse(either.either(Lambdas.wrap_fj_u(__ -> mapper.readTree(BACKUP_FIELD_MAPPING_PROPERTIES)), Lambdas.wrap_fj_u(__ -> mapper.readTree(BACKUP_FIELD_MAPPING_TEMPLATES)))); //(note that these 2 mappings don't have "type"s - therefore they will result in default_templates not properties - you need the type to generate a property) final ObjectNode mutable_field_mapping_tmp = either.isLeft() ? mutable_field_metadata : (ObjectNode) mutable_field_metadata.get("mapping"); //(override with type if set) maybe_type.ifPresent(type -> mutable_field_mapping_tmp.put("type", type)); final boolean has_type = mutable_field_mapping_tmp.has("type"); final Tuple2<ObjectNode, Either<String, Tuple2<String, String>>> toplevel_eithermod = Lambdas .get(() -> { if (either.isLeft() && !has_type) { final ObjectNode top_level = (ObjectNode) mapper.createObjectNode().set("mapping", mutable_field_metadata); return Tuples._2T(top_level, Either.<String, Tuple2<String, String>>right(Tuples._2T(fn, "*"))); } else { // right[dynamic] *OR* (left[properties] and has-type) return Tuples._2T(mutable_field_metadata, either); } }); final ObjectNode mutable_field_mapping = toplevel_eithermod._2().isLeft() ? toplevel_eithermod._1() : (ObjectNode) toplevel_eithermod._1().get("mapping"); // Special case ... if we're columnar and we're merging with tokenized and non-dual then convert to untokenized instead if (fielddata_info.filter(t3 -> t3._3()).isPresent() && mutable_field_mapping.equals( mapper.convertValue(search_index_schema_override.tokenized_string_field(), JsonNode.class))) { mutable_field_mapping.removeAll(); mutable_field_mapping.setAll((ObjectNode) mapper .convertValue(search_index_schema_override.untokenized_string_field(), ObjectNode.class)); } if (toplevel_eithermod._2().isRight()) { if (!toplevel_eithermod._1().has(PATH_MATCH_NAME) && !toplevel_eithermod._1().has(RAW_MATCH_NAME)) { toplevel_eithermod._1().put(PATH_MATCH_NAME, toplevel_eithermod._2().right().value()._1()); if (!toplevel_eithermod._1().has(TYPE_MATCH_NAME)) toplevel_eithermod._1().put(TYPE_MATCH_NAME, toplevel_eithermod._2().right().value()._2()); } if (!has_type) { if (toplevel_eithermod._2().right().value()._2().equals("*")) { // type is mandatory mutable_field_mapping.put("type", "{dynamic_type}"); } else { mutable_field_mapping.put("type", toplevel_eithermod._2().right().value()._2()); } } } handleMappingFields(mutable_field_mapping, fielddata_info, mapper, index_type); setMapping(mutable_field_mapping, fielddata_info, mapper, index_type); return Tuples._2T(toplevel_eithermod._2(), toplevel_eithermod._1()); })); }
From source file:com.ikanow.aleph2.search_service.elasticsearch.services.ElasticsearchIndexService.java
/** Check if a new mapping based on a schema is equivalent to a mapping previously stored (from a schema) * @param stored_mapping//from ww w . ja v a2 s. c o m * @param new_mapping * @param mapper * @return * @throws JsonProcessingException * @throws IOException */ protected static boolean mappingsAreEquivalent(final IndexTemplateMetaData stored_mapping, final JsonNode new_mapping, final ObjectMapper mapper) throws JsonProcessingException, IOException { final ObjectNode stored_json_mappings = StreamSupport.stream(stored_mapping.mappings().spliterator(), false) .reduce(mapper.createObjectNode(), Lambdas.wrap_u( (acc, kv) -> (ObjectNode) acc.setAll((ObjectNode) mapper.readTree(kv.value.string()))), (acc1, acc2) -> acc1); // (can't occur) final JsonNode new_json_mappings = Optional.ofNullable(new_mapping.get("mappings")) .orElse(mapper.createObjectNode()); final JsonNode stored_json_settings = mapper.convertValue(Optional.ofNullable(stored_mapping.settings()) .orElse(ImmutableSettings.settingsBuilder().build()).getAsMap(), JsonNode.class); final JsonNode new_json_settings = Optional.ofNullable(new_mapping.get("settings")) .orElse(mapper.createObjectNode()); final ObjectNode stored_json_aliases = StreamSupport .stream(Optional.ofNullable( stored_mapping.aliases()).orElse( ImmutableOpenMap.of()) .spliterator(), false) .reduce(mapper.createObjectNode(), Lambdas.wrap_u((acc, kv) -> (ObjectNode) acc.set(kv.key, kv.value.filteringRequired() ? mapper.createObjectNode().set("filter", mapper.readTree(kv.value.filter().string())) : mapper.createObjectNode())), (acc1, acc2) -> acc1); // (can't occur) final JsonNode new_json_aliases = Optional.ofNullable(new_mapping.get("aliases")) .orElse(mapper.createObjectNode()); return stored_json_mappings.equals(new_json_mappings) && stored_json_settings.equals(new_json_settings) && stored_json_aliases.equals(new_json_aliases); }
From source file:com.rusticisoftware.tincan.RemoteLRSTest.java
@Test public void testUpdateState() throws Exception { ObjectMapper mapper = Mapper.getInstance(); ObjectNode changeSet = mapper.createObjectNode(); // What changes are to be made ObjectNode correctSet = mapper.createObjectNode(); // What the correct content should be after change ObjectNode currentSet = mapper.createObjectNode(); // What the actual content is after change // Load initial change set String data = "{ \"x\" : \"foo\", \"y\" : \"bar\" }"; Map<String, String> changeSetMap = mapper.readValue(data, Map.class); for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); changeSet.put(k, v);/*from ww w. j a v a 2 s .c om*/ } Map<String, String> correctSetMap = changeSetMap; // In the beginning, these are equal for (String k : correctSetMap.keySet()) { String v = correctSetMap.get(k); correctSet.put(k, v); } StateDocument doc = new StateDocument(); doc.setActivity(activity); doc.setAgent(agent); doc.setId("test"); LRSResponse clear = lrs.deleteState(doc); Assert.assertTrue(clear.getSuccess()); doc.setContentType("application/json"); doc.setContent(changeSet.toString().getBytes("UTF-8")); LRSResponse save = lrs.saveState(doc); Assert.assertTrue(save.getSuccess()); StateLRSResponse retrieveBeforeUpdate = lrs.retrieveState("test", activity, agent, null); Assert.assertTrue(retrieveBeforeUpdate.getSuccess()); StateDocument beforeDoc = retrieveBeforeUpdate.getContent(); Map<String, String> c = mapper.readValue(new String(beforeDoc.getContent(), "UTF-8"), Map.class); for (String k : c.keySet()) { String v = c.get(k); currentSet.put(k, v); } Assert.assertTrue(currentSet.equals(correctSet)); doc.setContentType("application/json"); data = "{ \"x\" : \"bash\", \"z\" : \"faz\" }"; changeSet.removeAll(); changeSetMap = mapper.readValue(data, Map.class); for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); changeSet.put(k, v); } doc.setContent(changeSet.toString().getBytes("UTF-8")); // Update the correct set with the changes for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); correctSet.put(k, v); } currentSet.removeAll(); LRSResponse update = lrs.updateState(doc); Assert.assertTrue(update.getSuccess()); StateLRSResponse retrieveAfterUpdate = lrs.retrieveState("test", activity, agent, null); Assert.assertTrue(retrieveAfterUpdate.getSuccess()); StateDocument afterDoc = retrieveAfterUpdate.getContent(); Map<String, String> ac = mapper.readValue(new String(afterDoc.getContent(), "UTF-8"), Map.class); for (String k : ac.keySet()) { String v = ac.get(k); currentSet.put(k, v); } Assert.assertTrue(currentSet.equals(correctSet)); }
From source file:com.rusticisoftware.tincan.RemoteLRSTest.java
@Test public void testUpdateActivityProfile() throws Exception { ObjectMapper mapper = Mapper.getInstance(); ObjectNode changeSet = mapper.createObjectNode(); // What changes are to be made ObjectNode correctSet = mapper.createObjectNode(); // What the correct content should be after change ObjectNode currentSet = mapper.createObjectNode(); // What the actual content is after change // Load initial change set String data = "{ \"x\" : \"foo\", \"y\" : \"bar\" }"; Map<String, String> changeSetMap = mapper.readValue(data, Map.class); for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); changeSet.put(k, v);/*from ww w . ja v a 2s.c o m*/ } Map<String, String> correctSetMap = changeSetMap; // In the beginning, these are equal for (String k : correctSetMap.keySet()) { String v = correctSetMap.get(k); correctSet.put(k, v); } ActivityProfileDocument doc = new ActivityProfileDocument(); doc.setActivity(activity); doc.setId("test"); LRSResponse clear = lrs.deleteActivityProfile(doc); Assert.assertTrue(clear.getSuccess()); doc.setContentType("application/json"); doc.setContent(changeSet.toString().getBytes("UTF-8")); LRSResponse save = lrs.saveActivityProfile(doc); Assert.assertTrue(save.getSuccess()); ActivityProfileLRSResponse retrieveBeforeUpdate = lrs.retrieveActivityProfile("test", activity); Assert.assertTrue(retrieveBeforeUpdate.getSuccess()); ActivityProfileDocument beforeDoc = retrieveBeforeUpdate.getContent(); Map<String, String> c = mapper.readValue(new String(beforeDoc.getContent(), "UTF-8"), Map.class); for (String k : c.keySet()) { String v = c.get(k); currentSet.put(k, v); } Assert.assertTrue(currentSet.equals(correctSet)); doc.setContentType("application/json"); data = "{ \"x\" : \"bash\", \"z\" : \"faz\" }"; changeSet.removeAll(); changeSetMap = mapper.readValue(data, Map.class); for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); changeSet.put(k, v); } doc.setContent(changeSet.toString().getBytes("UTF-8")); // Update the correct set with the changes for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); correctSet.put(k, v); } currentSet.removeAll(); LRSResponse update = lrs.updateActivityProfile(doc); Assert.assertTrue(update.getSuccess()); ActivityProfileLRSResponse retrieveAfterUpdate = lrs.retrieveActivityProfile("test", activity); Assert.assertTrue(retrieveAfterUpdate.getSuccess()); ActivityProfileDocument afterDoc = retrieveAfterUpdate.getContent(); Map<String, String> ac = mapper.readValue(new String(afterDoc.getContent(), "UTF-8"), Map.class); for (String k : ac.keySet()) { String v = ac.get(k); currentSet.put(k, v); } Assert.assertTrue(currentSet.equals(correctSet)); }
From source file:com.rusticisoftware.tincan.RemoteLRSTest.java
@Test public void testUpdateAgentProfile() throws Exception { ObjectMapper mapper = Mapper.getInstance(); ObjectNode changeSet = mapper.createObjectNode(); // What changes are to be made ObjectNode correctSet = mapper.createObjectNode(); // What the correct content should be after change ObjectNode currentSet = mapper.createObjectNode(); // What the actual content is after change // Load initial change set String data = "{ \"firstName\" : \"Dave\", \"lastName\" : \"Smith\", \"State\" : \"CO\" }"; Map<String, String> changeSetMap = mapper.readValue(data, Map.class); for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); changeSet.put(k, v);// w ww . j a v a 2s. c o m } Map<String, String> correctSetMap = changeSetMap; // In the beginning, these are equal for (String k : correctSetMap.keySet()) { String v = correctSetMap.get(k); correctSet.put(k, v); } AgentProfileDocument doc = new AgentProfileDocument(); doc.setAgent(agent); doc.setId("test"); LRSResponse clear = lrs.deleteAgentProfile(doc); Assert.assertTrue(clear.getSuccess()); doc.setContentType("application/json"); doc.setContent(changeSet.toString().getBytes("UTF-8")); LRSResponse save = lrs.saveAgentProfile(doc); Assert.assertTrue(save.getSuccess()); AgentProfileLRSResponse retrieveBeforeUpdate = lrs.retrieveAgentProfile("test", agent); Assert.assertTrue(retrieveBeforeUpdate.getSuccess()); AgentProfileDocument beforeDoc = retrieveBeforeUpdate.getContent(); Map<String, String> c = mapper.readValue(new String(beforeDoc.getContent(), "UTF-8"), Map.class); for (String k : c.keySet()) { String v = c.get(k); currentSet.put(k, v); } Assert.assertTrue(currentSet.equals(correctSet)); doc.setContentType("application/json"); data = "{ \"lastName\" : \"Jones\", \"City\" : \"Colorado Springs\" }"; changeSet.removeAll(); changeSetMap = mapper.readValue(data, Map.class); for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); changeSet.put(k, v); } doc.setContent(changeSet.toString().getBytes("UTF-8")); // Update the correct set with the changes for (String k : changeSetMap.keySet()) { String v = changeSetMap.get(k); correctSet.put(k, v); } currentSet.removeAll(); LRSResponse update = lrs.updateAgentProfile(doc); Assert.assertTrue(update.getSuccess()); AgentProfileLRSResponse retrieveAfterUpdate = lrs.retrieveAgentProfile("test", agent); Assert.assertTrue(retrieveAfterUpdate.getSuccess()); AgentProfileDocument afterDoc = retrieveAfterUpdate.getContent(); Map<String, String> ac = mapper.readValue(new String(afterDoc.getContent(), "UTF-8"), Map.class); for (String k : ac.keySet()) { String v = ac.get(k); currentSet.put(k, v); } Assert.assertTrue(currentSet.equals(correctSet)); }