Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refact json parser tests
  • Loading branch information
jaeopt committed May 5, 2020
commit f0df4e29f45472ed36c64ad35342ea4c07e59876

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,112 @@
*/
package com.optimizely.ab.optimizelyjson;

import com.optimizely.ab.config.parser.JsonParseException;
import com.optimizely.ab.config.parser.*;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;

/**
* Common tests for all JSON parsers
*/
public class OptimizelyJSONTest {
protected String orgJson;
protected Map<String,Object> orgMap;
protected boolean canSupportGetValue;

@Before
public void setUp() throws Exception {
Class parserClass = getParser().getClass();
canSupportGetValue = parserClass.equals(GsonConfigParser.class) ||
parserClass.equals(JacksonConfigParser.class);

orgJson =
"{ " +
" \"k1\": \"v1\", " +
" \"k2\": true, " +
" \"k3\": { " +
" \"kk1\": 1.2, " +
" \"kk2\": { " +
" \"kkk1\": true, " +
" \"kkk2\": 3.5, " +
" \"kkk3\": \"vvv3\", " +
" \"kkk4\": [5.7, true, \"vvv4\"] " +
" } " +
" } " +
"} ";

public class OptimizelyJSONExtendedTest extends OptimizelyJSONCoreTest {
Map<String,Object> m3 = new HashMap<String,Object>();
m3.put("kkk1", true);
m3.put("kkk2", 3.5);
m3.put("kkk3", "vvv3");
m3.put("kkk4", new ArrayList(Arrays.asList(5.7, true, "vvv4")));

Map<String,Object> m2 = new HashMap<String,Object>();
m2.put("kk1", 1.2);
m2.put("kk2", m3);

Map<String,Object> m1 = new HashMap<String, Object>();
m1.put("k1", "v1");
m1.put("k2", true);
m1.put("k3", m2);

orgMap = m1;
}

protected String compact(String str) {
return str.replaceAll("\\s", "");
}
protected ConfigParser getParser() { return DefaultConfigParser.getInstance(); }

// Common tests for all parsers (GSON, Jackson, Json, JsonSimple)
@Test
public void testOptimizelyJSON() {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());
Map<String,Object> map = oj1.toMap();

OptimizelyJSON oj2 = new OptimizelyJSON(map, getParser());
String data = oj2.toString();

assertEquals(compact(data), compact(orgJson));
}

@Test
public void testToStringFromString() {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());
assertEquals(compact(oj1.toString()), compact(orgJson));
}

@Test
public void testToStringFromMap() {
OptimizelyJSON oj1 = new OptimizelyJSON(orgMap, getParser());
assertEquals(compact(oj1.toString()), compact(orgJson));
}

@Test
public void testToMapFromString() {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());
assertEquals(oj1.toMap(), orgMap);
}

@Test
public void testToMapFromMap() {
OptimizelyJSON oj1 = new OptimizelyJSON(orgMap, getParser());
assertEquals(oj1.toMap(), orgMap);
}

// GetValue tests

@Test
public void testGetValueNullKeyPath() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

TestTypes.MD1 md1 = oj1.getValue(null, TestTypes.MD1.class);
Expand All @@ -49,6 +146,8 @@ public void testGetValueNullKeyPath() throws JsonParseException {

@Test
public void testGetValueEmptyKeyPath() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

TestTypes.MD1 md1 = oj1.getValue("", TestTypes.MD1.class);
Expand All @@ -62,6 +161,8 @@ public void testGetValueEmptyKeyPath() throws JsonParseException {

@Test
public void testGetValueWithKeyPathToMapWithLevel1() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

TestTypes.MD2 md2 = oj1.getValue("k3", TestTypes.MD2.class);
Expand All @@ -72,6 +173,8 @@ public void testGetValueWithKeyPathToMapWithLevel1() throws JsonParseException {

@Test
public void testGetValueWithKeyPathToMapWithLevel2() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

TestTypes.MD3 md3 = oj1.getValue("k3.kk2", TestTypes.MD3.class);
Expand All @@ -81,6 +184,8 @@ public void testGetValueWithKeyPathToMapWithLevel2() throws JsonParseException {

@Test
public void testGetValueWithKeyPathToBoolean() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

Boolean value = oj1.getValue("k3.kk2.kkk1", Boolean.class);
Expand All @@ -90,6 +195,8 @@ public void testGetValueWithKeyPathToBoolean() throws JsonParseException {

@Test
public void testGetValueWithKeyPathToDouble() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

Double value = oj1.getValue("k3.kk2.kkk2", Double.class);
Expand All @@ -99,6 +206,8 @@ public void testGetValueWithKeyPathToDouble() throws JsonParseException {

@Test
public void testGetValueWithKeyPathToString() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k3.kk2.kkk3", String.class);
Expand All @@ -108,6 +217,8 @@ public void testGetValueWithKeyPathToString() throws JsonParseException {

@Test
public void testGetValueWithInvalidKeyPath() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k3..kkk3", String.class);
Expand All @@ -116,6 +227,8 @@ public void testGetValueWithInvalidKeyPath() throws JsonParseException {

@Test
public void testGetValueWithInvalidKeyPath2() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k1.", String.class);
Expand All @@ -124,6 +237,8 @@ public void testGetValueWithInvalidKeyPath2() throws JsonParseException {

@Test
public void testGetValueWithInvalidKeyPath3() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("x9", String.class);
Expand All @@ -132,6 +247,8 @@ public void testGetValueWithInvalidKeyPath3() throws JsonParseException {

@Test
public void testGetValueWithInvalidKeyPath4() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

String value = oj1.getValue("k3.x9", String.class);
Expand All @@ -140,6 +257,8 @@ public void testGetValueWithInvalidKeyPath4() throws JsonParseException {

@Test
public void testGetValueWithWrongType() throws JsonParseException {
assumeTrue("GetValue API is supported for Gson and Jackson parsers only", canSupportGetValue);

OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

Integer value = oj1.getValue("k3.kk2.kkk3", Integer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class OptimizelyJSONWithGsonParserTest extends OptimizelyJSONExtendedTest {
/**
* Tests for GSON parser only
*/
public class OptimizelyJSONWithGsonParserTest extends OptimizelyJSONTest {
@Override
protected ConfigParser getParser() {
return new GsonConfigParser();
}

// Tests for GSON only

@Test
public void testGetValueWithNotMatchingType() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

// GSON returns non-null object but variable is null (while Jackson returns null object)

TestTypes.NotMatchingType md = oj1.getValue(null, TestTypes.NotMatchingType.class);
assertNull(md.x99);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class OptimizelyJSONWithJacksonParserTest extends OptimizelyJSONExtendedTest {
/**
* Tests for Jackson parser only
*/
public class OptimizelyJSONWithJacksonParserTest extends OptimizelyJSONTest {
@Override
protected ConfigParser getParser() {
return new JacksonConfigParser();
}

// Tests for Jackson only

@Test
public void testGetValueWithNotMatchingType() throws JsonParseException {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());

// Jackson returns null object when variables not matching (while GSON returns an object with null variables

TestTypes.NotMatchingType md = oj1.getValue(null, TestTypes.NotMatchingType.class);
assertNull(md);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@

import static org.junit.Assert.*;

public class OptimizelyJSONWithJsonParserTest extends OptimizelyJSONCoreTest {
/**
* Tests for org.json parser only
*/
public class OptimizelyJSONWithJsonParserTest extends OptimizelyJSONTest {
@Override
protected ConfigParser getParser() {
return new JsonConfigParser();
}

// Tests for Json only

@Test
public void testGetValueThrowsException() {
OptimizelyJSON oj1 = new OptimizelyJSON(orgJson, getParser());
Expand Down
Loading