Skip to content

Instantly share code, notes, and snippets.

@vojtech-cerveny
Created July 2, 2017 10:59
Show Gist options
  • Save vojtech-cerveny/d7eea3486bf773eba4791c2b0aa2e575 to your computer and use it in GitHub Desktop.
Save vojtech-cerveny/d7eea3486bf773eba4791c2b0aa2e575 to your computer and use it in GitHub Desktop.
JSON to Object[][]?
package components;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.testng.annotations.DataProvider;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
public class JSONProvider {
@DataProvider(name = "keyProvider")
public Object[][] keyProvider() throws IOException, ParseException {
JSONParser parser = new JSONParser();
String filePath = new File("src/test/java/data/template.json").getAbsolutePath();
JSONObject template = (JSONObject) parser.parse(new FileReader(filePath));
Set<String> keys = template.keySet();
Iterator i = keys.iterator();
ArrayList<Object[]> array = new ArrayList<Object[]>();
while(i.hasNext()){
Object[] ob = new Object[] {i.next()};
array.add(ob);
}
System.out.println(array);
return (Object[][]) array.toArray();
}
}
{
"description":"",
"license":"cc-by-nc|cc-by-nc-sa",
"title":"",
"download_url":"https://api.soundcloud.com/tracks/\\d{9,}/download",
"duration": "\\d{2,}",
"last_modified":"\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\+\\d{4}",
"stream_url":"https://api.soundcloud.com/tracks/\\d{9,}/stream",
"tag_list":"[a-zA-Z]+",
"id": "\\d{2,}"
}
@Test(dataProvider = "keyProvider")
public void test_check_record_keys(String key) throws IOException, ParseException {
r = new Response();
r.setApi_key(DEFAULT_API_KEY);
r.setLimit("100");
System.out.println(key);
JSONArray jsonArray = r.getResults();
for (JSONObject record : (Iterable<JSONObject>) jsonArray) {
Assert.assertTrue(record.containsKey(key));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment