Skip to content

Commit

Permalink
Merge pull request #15 from SlaVcE14/unit-tests
Browse files Browse the repository at this point in the history
add unit tests
  • Loading branch information
SlaVcE14 authored Oct 28, 2023
2 parents 22fa9fa + 626a7d9 commit 98bf9e2
Show file tree
Hide file tree
Showing 6 changed files with 683 additions and 0 deletions.
56 changes: 56 additions & 0 deletions app/src/test/java/com/sjapps/jsonlist/java/GetArrayListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.sjapps.jsonlist.java;

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class GetArrayListTest {

@Test
public void testGetArrayList() {
ArrayList<ArrayList<ListItem>> inputList = new ArrayList<>();

ArrayList<ListItem> list1 = new ArrayList<>();
ListItem item1 = new ListItem();
item1.setName("Item 1");
item1.setValue("Value 1");
list1.add(item1);

ArrayList<ListItem> list2 = new ArrayList<>();
ListItem item2 = new ListItem();
item2.setName("Item 2");
item2.setValue("Value 2");
list2.add(item2);

inputList.add(list1);
inputList.add(list2);

ArrayList<ListItem> result = JsonFunctions.getArrayList(inputList);

assertEquals(4, result.size());
assertEquals("Item 1", result.get(0).getName());
assertEquals("Value 1", result.get(0).getValue());
assertFalse(result.get(0).isSpace());
assertEquals("Item 2", result.get(2).getName());
assertEquals("Value 2", result.get(2).getValue());
assertFalse(result.get(2).isSpace());
assertEquals(0, result.get(0).getId());
assertEquals(1, result.get(2).getId());
}

@Test
public void testGetArrayListEmpty() {

ArrayList<ArrayList<ListItem>> lists = new ArrayList<>();
ArrayList<ListItem> result = JsonFunctions.getArrayList(lists);

assertNotNull(result);
assertEquals(0,result.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.sjapps.jsonlist.java;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.sjapps.jsonlist.FileSystem;

import org.junit.Test;

import java.util.ArrayList;

public class GetJsonArrayRootTest {

@Test
public void getJsonArrayRootTest(){
String data = "[ {data2:\"123\"} ]";
JsonElement element = JsonParser.parseString(data);

JsonArray array = FileSystem.loadDataToJsonArray(element);

ArrayList<ListItem> itemsArr = JsonFunctions.getJsonArrayRoot(array,e -> {});
ArrayList<ListItem> expectedArr = new ArrayList<>();

ListItem root = new ListItem();
root.setName("Json Array");
root.setIsArrayOfObjects(true);

ArrayList<ListItem> listItems = new ArrayList<>();

ListItem OLItem = new ListItem();

OLItem.setName("data2");
OLItem.setValue("123");
listItems.add(OLItem);

ArrayList<ArrayList<ListItem>> arrayListArrayList = new ArrayList<>();
arrayListArrayList.add(listItems);
root.setListObjects(arrayListArrayList);
expectedArr.add(root);
assertEquals(expectedArr,itemsArr);

}

@Test
public void testGetJsonArrayRootWithEmptyArray() {
JsonArray jsonArray = new JsonArray();
ArrayList<ListItem> result = JsonFunctions.getJsonArrayRoot(jsonArray, e -> {});
assertNotNull(result);
assertEquals(1, result.size());
assertEquals("Json Array", result.get(0).getName());
assertTrue(result.get(0).isArrayOfObjects());
assertEquals(0, result.get(0).getListObjects().size());
}

}
214 changes: 214 additions & 0 deletions app/src/test/java/com/sjapps/jsonlist/java/GetJsonObjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package com.sjapps.jsonlist.java;

import static junit.framework.TestCase.*;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;


import org.junit.Test;

import java.util.ArrayList;

public class GetJsonObjectTest {

@Test
public void testGetJsonObjectWithNestedObject() {
JsonObject jsonObject = new JsonObject();
JsonObject nestedObject = new JsonObject();
nestedObject.addProperty("nestedKey", "nestedValue");
jsonObject.add("key1", nestedObject);

ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject, e -> {});

assertNotNull(result);
assertEquals(1, result.size());
assertEquals("key1", result.get(0).getName());
assertTrue(result.get(0).isObject());
assertEquals(1, result.get(0).getObjects().size());
assertEquals("nestedKey", result.get(0).getObjects().get(0).getName());
assertEquals("nestedValue", result.get(0).getObjects().get(0).getValue());
}

@Test
public void testGetJsonObjectWithNestedArray() {
JsonObject jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray();
jsonArray.add("value1");
jsonArray.add("value2");
jsonObject.add("key1", jsonArray);

ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject, e -> {});

assertNotNull(result);
assertEquals(1, result.size());
assertEquals("key1", result.get(0).getName());
// assertTrue(result.get(0).isArray());
assertFalse(result.get(0).isArrayOfObjects());
assertEquals("[\"value1\",\"value2\"]", result.get(0).getValue());
}


@Test
public void testGetJsonObjectFromString() {
String jsonString = "{\"key1\":\"value1\",\"key2\":\"value2\"}";

JsonObject jsonObject = new Gson().fromJson(jsonString, JsonObject.class);

ArrayList<ListItem> result = JsonFunctions.getJsonObject(jsonObject, e -> {});

assertNotNull(result);
assertEquals(2, result.size());
assertEquals("key1", result.get(0).getName());
assertEquals("value1", result.get(0).getValue());
assertEquals("key2", result.get(1).getName());
assertEquals("value2", result.get(1).getValue());
}


@Test
public void getJsonObjectFromString() {
String data = "{'item1':123,'item2':'test','item3':true,'item4':null,'item5':'123'}";

JsonObject object = new Gson().fromJson(data, JsonObject.class);

ArrayList<ListItem> items = JsonFunctions.getJsonObject(object,e -> {});

ArrayList<ListItem> expected = new ArrayList<>();

ListItem item1 = new ListItem();
ListItem item2 = new ListItem();
ListItem item3 = new ListItem();
ListItem item4 = new ListItem();
ListItem item5 = new ListItem();

item1.setName("item1");
item1.setValue("123");

item2.setName("item2");
item2.setValue("test");


item3.setName("item3");
item3.setValue("true");

item4.setName("item4");
item4.setValue("null");

item5.setName("item5");
item5.setValue("123");

expected.add(item1);
expected.add(item2);
expected.add(item3);
expected.add(item4);
expected.add(item5);


assertNotNull(items);
assertEquals(item1,items.get(0));
assertEquals(item2,items.get(1));
assertEquals(item3,items.get(2));
assertEquals(item4,items.get(3));
assertEquals(item5,items.get(4));
assertEquals(expected, items);

}

@Test
public void getJsonObjectFromStringObject() {
String data = "{'item1':'test','item2':{'item3':true,'item4':null}}";

JsonObject object = new Gson().fromJson(data, JsonObject.class);

ArrayList<ListItem> items = JsonFunctions.getJsonObject(object,e -> {});

ArrayList<ListItem> expected = new ArrayList<>();

ListItem item1 = new ListItem();
ListItem item2 = new ListItem();

item1.setName("item1");
item1.setValue("test");

item2.setName("item2");
item2.setIsObject(true);

ArrayList<ListItem> objs = new ArrayList<>();
ListItem item3 = new ListItem();
item3.setName("item3");
item3.setValue("true");
objs.add(item3);

ListItem item4 = new ListItem();
item4.setName("item4");
item4.setValue("null");
objs.add(item4);

item2.setObjects(objs);

expected.add(item1);
expected.add(item2);

assertNotNull(items);
assertEquals(item1,items.get(0));
assertEquals(item2,items.get(1));
assertTrue(items.get(1).isObject());
assertEquals(objs,items.get(1).getObjects());
assertEquals(item3,items.get(1).getObjects().get(0));
assertEquals(item4,items.get(1).getObjects().get(1));
assertEquals(expected, items);

}

@Test
public void getJsonObjectFromStringArray() {
String data = "{'item1':'test','item2':[{'item3':true},{'item4':null}]}";

JsonObject object = new Gson().fromJson(data, JsonObject.class);

ArrayList<ListItem> items = JsonFunctions.getJsonObject(object,e -> {});

ArrayList<ListItem> expected = new ArrayList<>();

ListItem item1 = new ListItem();
ListItem item2 = new ListItem();

item1.setName("item1");
item1.setValue("test");

item2.setName("item2");
item2.setIsArrayOfObjects(true);

ArrayList<ArrayList<ListItem>> objs = new ArrayList<>();
ArrayList<ListItem> items1 = new ArrayList<>();
ListItem item3 = new ListItem();
item3.setName("item3");
item3.setValue("true");
items1.add(item3);
objs.add(items1);


ArrayList<ListItem> items2 = new ArrayList<>();
ListItem item4 = new ListItem();
item4.setName("item4");
item4.setValue("null");
items2.add(item4);
objs.add(items2);

item2.setListObjects(objs);

expected.add(item1);
expected.add(item2);

assertNotNull(items);
assertEquals(item1,items.get(0));
assertEquals(item2,items.get(1));
assertTrue(items.get(1).isArrayOfObjects());
assertEquals(items1,items.get(1).getListObjects().get(0));
assertEquals(items2,items.get(1).getListObjects().get(1));
assertEquals(expected, items);

}
}
Loading

0 comments on commit 98bf9e2

Please sign in to comment.