private void initUserSelectionMap() { try { String jsonString = readFromFile(NEWS_CAT_FILE); Crashlytics.log(Log.INFO, LOG_TAG, "jsonString - " + jsonString); fullJsonMap = JsonHelper.toMap(new JSONObject(jsonString)); Crashlytics.log(Log.INFO, LOG_TAG, "fullJsonMap - " + fullJsonMap.toString()); userSelectionMap = getUserFeedMapFromJsonMap(fullJsonMap); Crashlytics.log(Log.INFO, LOG_TAG, "UserSelectionMap - " + userSelectionMap.toString()); } catch (JSONException e) { Crashlytics.logException(e); } }
public void updateNewsCatFileWithNewAssets() { try { String jsonString = readFromFile(NEWS_CAT_FILE, true); Crashlytics.log(Log.INFO, LOG_TAG, "newJsonString - " + jsonString); Map<String, Object> newFullJsonMap = JsonHelper.toMap(new JSONObject(jsonString)); Crashlytics.log(Log.INFO, LOG_TAG, "newFullJsonMap - " + newFullJsonMap.toString()); convertUserFeedMapToJsonMap(newFullJsonMap); Map<String, List<String>> newUserSelectionMap = getUserFeedMapFromJsonMap(newFullJsonMap); Crashlytics.log(Log.INFO, LOG_TAG, "newUserSelectionMap - " + newUserSelectionMap); setUserSelectionMap(newUserSelectionMap); } catch (JSONException e) { e.printStackTrace(); } }
@Test public void shouldKeepParametersCase() { Person p = Person.create("name", "Joe", "last_name", "Schmoe"); Map map = JsonHelper.toMap(p.toJson(true)); a(map.get("name")).shouldBeEqual("Joe"); a(map.get("last_name")).shouldBeEqual("Schmoe"); map = JsonHelper.toMap(p.toJson(true, "Name", "Last_Name")); a(map.get("Name")).shouldBeEqual("Joe"); a(map.get("Last_Name")).shouldBeEqual("Schmoe"); }
@Override public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException { this._callbackContext = callbackContext; if (ACTION_INIT.equals(action)) { return init(args.getString(0), args.getString(1), args.getString(2)); } else if (ACTION_LOGIN.equals(action)) { JSONArray permissions = args.optJSONArray(0); return login(permissions, callbackContext); } else if (ACTION_SHARE.equals(action)) { return shareOrLogin(args.getString(0), args.getString(1)); } else if (ACTION_FRIENDS_GET.equals(action)) { return friendsGet(args.getString(0), args.getString(1), callbackContext); } else if (ACTION_FRIENDS_GET_ONLINE.equals(action)) { return friendsGetOnline(args.getString(0), args.getString(1), callbackContext); } else if (ACTION_STREAM_PUBLISH.equals(action)) { // TODO } else if (ACTION_USERS_GET_INFO.equals(action)) { return usersGetInfo(args.getString(0), args.getString(1), callbackContext); } else if (ACTION_CALL_API_METHOD.equals(action)) { String method = args.getString(0); JSONObject params = args.getJSONObject(1); return callApiMethod(method, JsonHelper.toMap(params), callbackContext); } Log.e(TAG, "Unknown action: " + action); _callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.ERROR, "Unimplemented method: " + action)); _callbackContext.error("Unimplemented method: " + action); return true; }
@Test public void shouldInjectCustomContentIntoJson() { deleteAndPopulateTable("posts"); Post p = Post.findById(1); String json = p.toJson(true, "title"); Map map = JsonHelper.toMap(json); Map injected = (Map) map.get("injected"); a(injected.get("secret_name")).shouldBeEqual("Secret Name"); }
@Test public void shouldGenerateSimpleJson() { deleteAndPopulateTable("people"); Person p = Person.findFirst("name = ? and last_name = ? ", "John", "Smith"); // test no indent String json = p.toJson(false, "name", "last_name", "dob"); Map map = JsonHelper.toMap(json); a(map.get("name")).shouldBeEqual("John"); a(map.get("last_name")).shouldBeEqual("Smith"); a(map.get("dob")).shouldBeEqual("1934-12-01"); }
@Test public void shouldReturnSecondsInDateTime() throws ParseException { Person p = new Person(); p.set("name", "john", "last_name", "doe").saveIt(); p.refresh(); String json = p.toJson(true); System.out.println(json); @SuppressWarnings("unchecked") Map<String, String> map = JsonHelper.toMap(json); Date d = new ISO8601DateFormat().parse(map.get("created_at")); // difference between date in Json and in original model instance should be less than 1000 // milliseconds a(Math.abs(d.getTime() - p.getTimestamp("created_at").getTime()) < 1000L).shouldBeTrue(); }
@Test public void shouldIncludeUglyChildren() { deleteAndPopulateTables("users", "addresses"); List<User> personList = User.findAll().orderBy("id").include(Address.class); User u = personList.get(0); String json = u.toJson(false); Map m = JsonHelper.toMap(json); a(m.get("first_name")).shouldBeEqual("Marilyn"); a(m.get("last_name")).shouldBeEqual("Monroe"); Map children = (Map) m.get("children"); List<Map> addresses = (List<Map>) children.get("addresses"); the(addresses.size()).shouldBeEqual(3); // at this point, no need to verify, since the order of addresses is not guaranteed.. or is it?? }
@Test @SuppressWarnings("unchecked") public void shouldIncludeParent() { deleteAndPopulateTables("libraries", "books", "readers"); List<Book> books = Book.findAll() .orderBy(Book.getMetaModel().getIdName()) .include(Reader.class, Library.class); Map book = JsonHelper.toMap(books.get(0).toJson(true)); Map parents = (Map) book.get("parents"); the(parents.size()).shouldBeEqual(1); List<Map> libraries = (List<Map>) parents.get("libraries"); the(libraries.size()).shouldBeEqual(1); Map library = libraries.get(0); a(library.get("address")).shouldBeEqual("124 Pine Street"); }