@Test public void fill() { while (Calendar.getInstance().get(Calendar.SECOND) > 55) thread.sleep(5, TimeUnit.Second); mockHelper.reset(); mockHelper.mock("/service-helper"); mockCarousel.reset(); mockCarousel.register("key.get", (key, header, parameter, cacheTime) -> { JSONObject json = new JSONObject(); json.put("code", 0); JSONObject data = new JSONObject(); JSONObject service = new JSONObject(); service.put("id", parameter.get("ids")); service.put("name", "name " + service.getString("id")); data.put(service.getString("id"), service); json.put("data", data); return json.toJSONString(); }); Assert.assertNull(serviceHelper.fill(null, null)); JSONArray array = new JSONArray(); Assert.assertTrue(serviceHelper.fill(array, null).isEmpty()); Assert.assertTrue(array.isEmpty()); String[] keys = new String[]{"service", "name", "helper"}; for (int i = 0; i < 10; i++) { JSONObject object = new JSONObject(); for (String key : keys) object.put(key, key + " " + i); array.add(object); } Assert.assertEquals(array.hashCode(), serviceHelper.fill(array, null).hashCode()); for (int i = 0; i < 10; i++) { JSONObject object = array.getJSONObject(i); for (String key : keys) Assert.assertEquals(key + " " + i, object.getString(key)); } Assert.assertEquals(array.hashCode(), serviceHelper.fill(array, new String[0]).hashCode()); for (int i = 0; i < 10; i++) { JSONObject object = array.getJSONObject(i); for (String key : keys) Assert.assertEquals(key + " " + i, object.getString(key)); } array = serviceHelper.fill(array, new String[]{"service", "helper"}); for (int i = 0; i < 10; i++) { JSONObject object = array.getJSONObject(i); JSONObject obj = object.getJSONObject("service"); Assert.assertEquals("service " + i, obj.getString("id")); Assert.assertEquals("name service " + i, obj.getString("name")); Assert.assertEquals("name " + i, object.getString("name")); obj = object.getJSONObject("helper"); Assert.assertEquals("helper " + i, obj.getString("id")); Assert.assertEquals("name helper " + i, obj.getString("name")); } }
public JSONArray fill(JSONArray array, String[] names) { if (validator.isEmpty(array) || validator.isEmpty(names)) return array; for (int i = 0, size = array.size(); i < size; i++) { JSONObject object = array.getJSONObject(i); for (String name : names) { if (!object.containsKey(name)) continue; String id = object.getString(name); if (!validator.isEmpty(id)) object.put(name, get(id)); } } return array; }
ServiceHelperSupport { public JSONArray fill(JSONArray array, String[] names) { if (validator.isEmpty(array) || validator.isEmpty(names)) return array; for (int i = 0, size = array.size(); i < size; i++) { JSONObject object = array.getJSONObject(i); for (String name : names) { if (!object.containsKey(name)) continue; String id = object.getString(name); if (!validator.isEmpty(id)) object.put(name, get(id)); } } return array; } }
ServiceHelperSupport { public JSONArray fill(JSONArray array, String[] names) { if (validator.isEmpty(array) || validator.isEmpty(names)) return array; for (int i = 0, size = array.size(); i < size; i++) { JSONObject object = array.getJSONObject(i); for (String name : names) { if (!object.containsKey(name)) continue; String id = object.getString(name); if (!validator.isEmpty(id)) object.put(name, get(id)); } } return array; }  }
ServiceHelperSupport { public JSONArray fill(JSONArray array, String[] names) { if (validator.isEmpty(array) || validator.isEmpty(names)) return array; for (int i = 0, size = array.size(); i < size; i++) { JSONObject object = array.getJSONObject(i); for (String name : names) { if (!object.containsKey(name)) continue; String id = object.getString(name); if (!validator.isEmpty(id)) object.put(name, get(id)); } } return array; }  JSONObject get(String id); JSONArray fill(JSONArray array, String[] names); }
ServiceHelperSupport { public JSONArray fill(JSONArray array, String[] names) { if (validator.isEmpty(array) || validator.isEmpty(names)) return array; for (int i = 0, size = array.size(); i < size; i++) { JSONObject object = array.getJSONObject(i); for (String name : names) { if (!object.containsKey(name)) continue; String id = object.getString(name); if (!validator.isEmpty(id)) object.put(name, get(id)); } } return array; }  JSONObject get(String id); JSONArray fill(JSONArray array, String[] names);  }