com.scvngr.levelup.core.model.util.JsonUtilsTest.java Source code

Java tutorial

Introduction

Here is the source code for com.scvngr.levelup.core.model.util.JsonUtilsTest.java

Source

/*
 * Copyright (C) 2014 SCVNGR, Inc. d/b/a LevelUp
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.scvngr.levelup.core.model.util;

import android.test.suitebuilder.annotation.SmallTest;

import com.scvngr.levelup.core.test.SupportAndroidTestCase;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Tests {@link com.scvngr.levelup.core.model.util.JsonUtils}.
 */
public final class JsonUtilsTest extends SupportAndroidTestCase {
    /**
     * Tests {@link JsonUtils#optLongNullable}.
     */
    @SmallTest
    public void testOptLongNullable() throws JSONException {
        final JSONObject object = new JSONObject();
        object.put("test_literal", 1L);
        object.put("test_object", Long.valueOf(1L));
        object.put("test_null", null);

        final Long literal = JsonUtils.optLongNullable(object, "test_literal");
        assertNotNull(literal);
        assertEquals(1L, literal.longValue());

        assertEquals(Long.valueOf(1L), JsonUtils.optLongNullable(object, "test_object"));
        assertNull(JsonUtils.optLongNullable(object, "test_null"));
        assertNull(JsonUtils.optLongNullable(object, "test_key_not_found"));
    }

    /**
     * Tests {@link com.scvngr.levelup.core.model.util.JsonUtils#optString(org.json.JSONObject, String)}.
     *
     * @throws org.json.JSONException if there's a parse error.
     */
    @SmallTest
    public void testOptString() throws JSONException {
        final JSONObject object1 = new JSONObject("{ 'test': null }");

        // JsonObject returns a non-null value, we shouldn't.
        assertNotNull(object1.optString("test"));
        assertNull(JsonUtils.optString(object1, "test"));

        final JSONObject object2 = new JSONObject("{ 'test': 'null' }");
        assertEquals("null", JsonUtils.optString(object2, "test"));
    }

    /**
     * Tests {@link com.scvngr.levelup.core.model.util.JsonUtils#optIntegerSet(org.json.JSONObject, String)}.
     *
     * @throws org.json.JSONException if there's a parse error.
     */
    public void testIntegerSet() throws JSONException {
        final JSONObject object1 = new JSONObject("{ 'test': null }");

        Set<Integer> ints = JsonUtils.optIntegerSet(object1, "test");

        assertNull(ints);

        final JSONObject object2 = new JSONObject("{ 'test': [2,3,5,23] }");

        ints = JsonUtils.optIntegerSet(object2, "test");

        assertNotNull(ints);
        assertEquals(new HashSet<Integer>(Arrays.asList(2, 3, 5, 23)), ints);

        assertEquals(new HashSet<Integer>(Arrays.asList(123)),
                JsonUtils.optIntegerSet(new JSONObject("{ 'test': [123] }"), "test"));

        assertEquals(new HashSet<Integer>(Arrays.asList(0)),
                JsonUtils.optIntegerSet(new JSONObject("{ 'test': [0] }"), "test"));

        assertEquals(new HashSet<Integer>(Arrays.asList(1)),
                JsonUtils.optIntegerSet(new JSONObject("{ 'test': [1,1,1] }"), "test"));

        assertEquals(Collections.emptySet(), JsonUtils.optIntegerSet(new JSONObject("{ 'test': [] }"), "test"));
    }

    /**
     * Tests {@link com.scvngr.levelup.core.model.util.JsonUtils#optStringSet(org.json.JSONObject, String)}.
     *
     * @throws org.json.JSONException if there's a parse error.
     */
    public void testStringSet() throws JSONException {
        final JSONObject object1 = new JSONObject("{ 'test': null }");

        Set<String> strings = JsonUtils.optStringSet(object1, "test");

        assertNull(strings);

        final JSONObject object2 = new JSONObject("{ 'test': ['foo','bar', 'baz'] }");

        strings = JsonUtils.optStringSet(object2, "test");

        assertNotNull(strings);
        assertEquals(new HashSet<String>(Arrays.asList("foo", "bar", "baz")), strings);

        assertEquals(new HashSet<String>(Arrays.asList("foo")),
                JsonUtils.optStringSet(new JSONObject("{ 'test': ['foo'] }"), "test"));

        assertEquals(new HashSet<String>(Arrays.asList("")),
                JsonUtils.optStringSet(new JSONObject("{ 'test': [''] }"), "test"));

        assertEquals(new HashSet<String>(Arrays.asList("foo")),
                JsonUtils.optStringSet(new JSONObject("{ 'test': ['foo','foo','foo'] }"), "test"));

        assertEquals(Collections.emptySet(), JsonUtils.optStringSet(new JSONObject("{ 'test': [] }"), "test"));
    }

    @SmallTest
    public void testOptMonetaryValue() throws JSONException {
        final JSONObject json = new JSONObject("{ 'amount' : 7000 }");

        JsonUtils.optMonetaryValue(json, "amount");
    }
}