Skip to content

Commit

Permalink
Merge pull request #851 from ashishbhasin45/unit-test-cases
Browse files Browse the repository at this point in the history
Added new unit test cases for json parser and xml parser
  • Loading branch information
rsoika committed Mar 2, 2024
2 parents 1e81edb + d9fa16d commit 06bd44f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collection;

import org.imixs.workflow.ItemCollection;
import org.junit.Test;

import org.junit.Assert;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* Test class test the parsing of different json structures.
*
*
* @author rsoika
*/
@RunWith(Parameterized.class)
public class TestJSONParserSimple {

private String jsonString;
private String key;
private String response;

public TestJSONParserSimple(String jsonString, String key, String response) {
this.jsonString = jsonString;
this.key = key;
this.response = response;
}

/**
* Parse a single key value.
*
Expand Down Expand Up @@ -50,4 +65,37 @@ public void testWorkitem() throws ParseException {

}

@Test
public void testKeyNotPresent() {
String json = "{\"key1\":\"value1\",\"key2\": 2}";
String key = "randomKey";
Assert.assertNull(JSONParser.getKey(key, json));
}

@Test
public void testNullValue() {
String json = "{\"key1\":null}";
Assert.assertNull(JSONParser.getKey("key1", json));
}

// Arrange for different key types in the json
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "{\"key\":{\"nestedKey\":\"nestedValue\"}}", "key", "{\"nestedKey\":\"nestedValue\"}" },
{ "{\"key\":[1,2,3] }", "key", "[1,2,3]" },
{ "{\"key\":true }", "key", "true" },
{ "{\"key\":false }", "key", "false" },
{ "{\"key\":42 }", "key", "42" },
});
}

@Test
public void testGettingDifferentKeyTypesFromJson() {
// act
String actual = JSONParser.getKey(key, jsonString);
//assert
Assert.assertEquals(response, actual);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.imixs.workflow.xml;

import java.util.List;
import java.util.Map;
import java.util.*;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.exceptions.PluginException;
Expand Down Expand Up @@ -377,4 +376,28 @@ public void testParseEventModelTag() {

}

@Test
public void testParseTagList(){
//arrange
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<library>\n" +
" <book>\n" +
" <title>Harry Potter and the Philosopher's Stone</title>\n" +
" <author>J.K. Rowling</author>\n" +
" </book>\n" +
" <book>\n" +
" <title>The Great Gatsby</title>\n" +
" <author>F. Scott Fitzgerald</author>\n" +
" </book>\n" +
"</library>";
try {
//act
var result = XMLParser.parseTagList(xmlString, "book");
//assert
Assert.assertEquals(2, result.size());
Assert.assertEquals("Harry Potter and the Philosopher's Stone", result.get(0).getItemValue("title").get(0));
} catch (PluginException e) {
}
}

}

0 comments on commit 06bd44f

Please sign in to comment.