Skip to content

Commit

Permalink
updating file's name
Browse files Browse the repository at this point in the history
  • Loading branch information
Yueh Chun Wu authored and Yueh Chun Wu committed Mar 13, 2024
1 parent b14b309 commit c3ecf92
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 184 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,19 +512,19 @@ The `addNodes` function effectively explores the entire JSON structure, creating

<h2>Junit Test</h2>

* I created a new test file called `MileStone4Test` for testing `toStream` method's functionalities.
* I created a new test file called `MileStone3Test` for testing `toStream` method's functionalities.

```
obj.toStream().forEach(node -> do some transformation, possibly based on the path of the node);
List<String> titles = obj.toStream().map(node -> extract value for key "title").collect(Collectors.toList());
obj.toStream().filter(node -> node with certain properties).forEach(node -> do some transformation);
```

* When you run `mvn clean test -Dtest=MileStone4Test` in the root dir, you can check each test case's printed result.
* When you run `mvn clean test -Dtest=MileStone3Test` in the root dir, you can check each test case's printed result.

<img width="810" alt="截圖 2024-02-28 下午5 55 07" src="https://github.com/yuehcw/MSWE-262P-MileStone/assets/152671651/cb3f7965-19a8-4199-a697-d7928a548a7b">

<h1>MileStone5</h1>
<h1>MileStone4</h1>

* Add asynchronous methods to the library that allow the client code to proceed, while specifying what to do when the JSONObject becomes available. This is useful for when reading very large files. For example:

Expand Down Expand Up @@ -566,7 +566,7 @@ The `toJSONObject` static function acts as an interface to this functionality, h

<h2>Junit Test</h2>

* I created a new test file called `MileStone5Test` for testing `toJSONObject` method's functionalities.
* I created a new test file called `MileStone4Test` for testing `toJSONObject` method's functionalities.

* Inside the testing file, `testTransformJSONObjectKeyAsyn` verify that the XML to JSON conversion correctly applies the key transformation function to all keys in the resulting JSON object.

Expand Down Expand Up @@ -615,4 +615,4 @@ public void testTransformJSONObjectKeyAsynExceptionHandler() {
}
```

* When you run `mvn clean test -Dtest=MileStone5Test` in the root dir, you can check each test case's printed result.
* When you run `mvn clean test -Dtest=MileStone4Test` in the root dir, you can check each test case's printed result.
45 changes: 45 additions & 0 deletions src/test/java/org/json/junit/MileStone3Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import org.json.JSONObject;
import org.json.XML;
import org.junit.Test;

import java.util.List;
import java.util.stream.Collectors;

import static junit.framework.Assert.assertEquals;

public class MileStone3Test {
@Test
public void testMileStone3() {
JSONObject obj = XML.toJSONObject("<Books><book><title>AAA</title><author>ASmith</author></book><book><title>BBB</title><author>BSmith</author></book></Books>");

//obj.toStream().forEach(node -> do some transformation, possibly based on the path of the node);
obj.toStream().forEach(node -> {
String path = node.getPath();
Object value = node.getValue();
String transformedKey = node.getKey();
String transformedValue = value.toString();

if (path.contains("author")) {
node.setKey(transformedKey.toUpperCase());
node.setValue(transformedValue.toUpperCase());
System.out.println(node.toString());
}
});
System.out.println("------------------");

//List<String> titles = obj.toStream().map(node -> extract value for key "title").collect(Collectors.toList());
List<String> titles = obj.toStream().filter(node -> node.getPath().contains("title"))
.map(node -> node.getValue().toString())
.collect(Collectors.toList());
assertEquals(titles.size(), 2);
System.out.println(titles.get(0));
System.out.println(titles.get(1));
System.out.println("------------------");

//obj.toStream().filter(node -> node with certain properties).forEach(node -> do some transformation);
obj.toStream().filter(node -> node.getKey().equals("author"))
.forEach(node -> {
System.out.println(node.getValue().toString().replace("Smith", "Kevin"));
});
}
}
168 changes: 133 additions & 35 deletions src/test/java/org/json/junit/MileStone4Test.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,144 @@
import org.json.JSONNode;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.XML;
import org.junit.Test;

import java.util.List;
import java.util.stream.Collectors;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;

public class MileStone4Test {

private JSONObject convertValuesToString(JSONObject jsonObject) {
JSONObject result = new JSONObject();
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
value = convertValuesToString((JSONObject) value);
} else if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
JSONArray newArray = new JSONArray();
for (int i = 0; i < array.length(); i++) {
Object element = array.get(i);
if (element instanceof JSONObject) {
newArray.put(convertValuesToString((JSONObject) element));
} else {
newArray.put(String.valueOf(element));
}
}
value = newArray;
} else {
value = String.valueOf(value);
}
result.put(key, value);
}
return result;
}

@Test
public void testTransformJSONObjectKeyAsyn(){
try {
FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml");
Function<String, String> keyTransformer = (key) -> "swe262_" + key;
Consumer<Exception> exceptionHandler = (e) -> e.printStackTrace();

Future<JSONObject> futureActual = XML.toJSONObject(reader, keyTransformer, exceptionHandler);
String expect = "{\n" +
" \"swe262_library\": {\n" +
" \"swe262_book\": [\n" +
" {\n" +
" \"swe262_title\": \"Programming Languages\",\n" +
" \"swe262_author\": \"Robert W. Sebesta\",\n" +
" \"swe262_isbn\": \"9780133943023\",\n" +
" \"swe262_published\": \"2016\"\n" +
" },\n" +
" {\n" +
" \"swe262_title\": \"Clean Code: A Handbook of Agile Software Craftsmanship\",\n" +
" \"swe262_author\": \"Robert C. Martin\",\n" +
" \"swe262_isbn\": \"9780132350884\",\n" +
" \"swe262_published\": \"2008\"\n" +
" },\n" +
" {\n" +
" \"swe262_title\": \"Design Patterns: Elements of Reusable Object-Oriented Software\",\n" +
" \"swe262_author\": \"Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides\",\n" +
" \"swe262_isbn\": \"9780201633610\",\n" +
" \"swe262_published\": \"1994\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";

// block the execution until the task is complete
while(!futureActual.isDone()) {
System.out.println("Calculating...");
Thread.sleep(300);
}

JSONObject actual = futureActual.get();

actual = convertValuesToString(actual);

org.json.junit.Util.compareActualVsExpectedJsonObjects(actual, new JSONObject(expect));
reader.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Fail to close file.");
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("Something wrong while blockign futureActual.isDone().");
e.printStackTrace();
} catch (ExecutionException e) {
System.out.println("error found when executing futureActual.get().");
e.printStackTrace();
}
}


@Test
public void testMileStone4() {
JSONObject obj = XML.toJSONObject("<Books><book><title>AAA</title><author>ASmith</author></book><book><title>BBB</title><author>BSmith</author></book></Books>");

//obj.toStream().forEach(node -> do some transformation, possibly based on the path of the node);
obj.toStream().forEach(node -> {
String path = node.getPath();
Object value = node.getValue();
String transformedKey = node.getKey();
String transformedValue = value.toString();

if (path.contains("author")) {
node.setKey(transformedKey.toUpperCase());
node.setValue(transformedValue.toUpperCase());
System.out.println(node.toString());
public void testTransformJSONObjectKeyAsynReturnType() {
try {
FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml");
Function<String, String> keyTransformer = (key) -> "swe262_" + key;
Consumer<Exception> exceptionHandler = (e) -> e.printStackTrace();

Future<JSONObject> futureActual = XML.toJSONObject(reader, keyTransformer, exceptionHandler);

while (!futureActual.isDone()) {
System.out.println("Calculating...");
Thread.sleep(300);
}
});
System.out.println("------------------");

//List<String> titles = obj.toStream().map(node -> extract value for key "title").collect(Collectors.toList());
List<String> titles = obj.toStream().filter(node -> node.getPath().contains("title"))
.map(node -> node.getValue().toString())
.collect(Collectors.toList());
assertEquals(titles.size(), 2);
System.out.println(titles.get(0));
System.out.println(titles.get(1));
System.out.println("------------------");

//obj.toStream().filter(node -> node with certain properties).forEach(node -> do some transformation);
obj.toStream().filter(node -> node.getKey().equals("author"))
.forEach(node -> {
System.out.println(node.getValue().toString().replace("Smith", "Kevin"));
});

JSONObject actual = futureActual.get();

assertTrue(actual instanceof JSONObject);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

@Test
public void testTransformJSONObjectKeyAsynExceptionHandler() {
try {
FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml");
Consumer<Exception> exceptionHandler = (e) -> System.out.println("OMG ERROR!!");

Future<JSONObject> futureActual = XML.toJSONObject(reader, null, exceptionHandler);
assertNull(futureActual);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit c3ecf92

Please sign in to comment.