@Test public void testCompareEqualsInObject() { Json x1 = object("id", 4, "name", "Tom"); Json x2 = object("id", 4, "name", "Hanna"); Json a1 = array(object("person", x1)); Json a2 = array(object("person", x2)); a1.with(a2, new Json[0]); Assert.assertEquals(2, a1.asJsonList().size()); a1 = array(object("person", x1)); a1.with(a2, object("compareBy", "id")); Assert.assertEquals(1, a1.asJsonList().size()); Assert.assertEquals(make("Tom"), a1.at(0).at("person").at("name")); }
@Test public void testSortedArrayMerge() { Json a1 = array(1, 2, 20, 30, 50); Json a2 = array(0, 2, 20, 30, 35, 40, 51); a1.with(a2, "sort"); Assert.assertEquals(array(0, 1, 2, 20, 30, 35, 40, 50, 51), a1); }
@Test public void testUnsortedArrayMerge() { Json a1 = array(4, 35, 1, 65, 2, 456); Json a2 = array(65, 5, 3534, 4); a1.with(a2, object("sort", false)); Assert.assertEquals( TU.set(a1.asJsonList()), TU.set(array(4, 35, 1, 65, 2, 456, 65, 5, 3534, 4).asJsonList())); }
@Test public void testObjectMerge() { Json o1 = object( "id", 2, "name", "John", "address", object( "streetName", "Main", "streetNumber", 20, "city", "Detroit")); Json o2 = o1.dup().set("age", 20).at("address").delAt("city").up(); o1.with(o2, "merge"); Assert.assertTrue(o1.is("age", 20)); Assert.assertTrue(o1.at("address").is("city", "Detroit")); }
@GET @Path("search") public Json search( @QueryParam("id") String id, @QueryParam("name") String searchString, @QueryParam("providers") String providers) { if (id != null && !id.isEmpty()) { return Json.array().add(searchUserById(id)); } Json resultList = Json.array(); final int maxResults = 15; try { if (searchString == null || searchString.length() == 0) return null; else searchString = searchString.trim(); Json user = Json.object(); String name = searchString; name = name.trim(); int idx; // Parse search string if ((idx = name.indexOf(',')) > -1) { // Miller, Bob user.set("LastName", name.substring(0, idx).trim()); user.set("FirstName", name.substring(idx + 1).trim()); } else if ((idx = name.indexOf(' ')) > -1) { // Bob Miller user.set("LastName", name.substring(idx + 1).trim()); user.set("FirstName", name.substring(0, idx).trim()); } else { // Miller user.set("LastName", name); } if (user.is("FirstName", "")) user.delAt("FirstName"); if (user.is("LastName", "")) user.delAt("LastName"); if (user.asJsonMap().size() > 0) { Collection<String> P = providers != null ? Arrays.asList(providers.split(",")) : orderedProviders(); for (String providerName : P) resultList.with(searchProvider(providerName, user, maxResults)); } } catch (Exception e) { e.printStackTrace(); return ko(e); } return prepareReturn(resultList); }