Make JsonUtils methods static
This commit is contained in:
parent
0c1a40cd2f
commit
a7e3b2ea4a
|
@ -14,23 +14,23 @@ import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class JsonUtils {
|
public class JsonUtils {
|
||||||
public JSONObject getJsonFromGson(JsonObject gJsonObject) {
|
public static JSONObject getJsonFromGson(JsonObject gJsonObject) {
|
||||||
return new JSONObject(gJsonObject.toString());
|
return new JSONObject(gJsonObject.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject getJsonFromDataTable(DataTable dataTable) {
|
public static JSONObject getJsonFromDataTable(DataTable dataTable) {
|
||||||
List<Map<String, String>> tableDetails =
|
List<Map<String, String>> tableDetails =
|
||||||
dataTable.transpose().asMaps(String.class, String.class);
|
dataTable.transpose().asMaps(String.class, String.class);
|
||||||
String bodyString = new Gson().toJson(tableDetails.get(0)); //root element
|
String bodyString = new Gson().toJson(tableDetails.get(0)); //root element
|
||||||
return new JSONObject(bodyString);
|
return new JSONObject(bodyString);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonObject getFixtureDataFrom(String filename) throws IOException {
|
public static JsonObject getFixtureDataFrom(String filename) throws IOException {
|
||||||
String json = getJsonAsString(filename);
|
String json = getJsonAsString(filename);
|
||||||
return new Gson().fromJson(json, JsonObject.class);
|
return new Gson().fromJson(json, JsonObject.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getJsonAsString(String filename) throws IOException {
|
private static String getJsonAsString(String filename) throws IOException {
|
||||||
InputStream inputStream =
|
InputStream inputStream =
|
||||||
JsonUtils.class
|
JsonUtils.class
|
||||||
.getResourceAsStream(
|
.getResourceAsStream(
|
||||||
|
|
|
@ -7,10 +7,10 @@ import io.cucumber.junit.platform.engine.Cucumber;
|
||||||
import kong.unirest.json.JSONObject;
|
import kong.unirest.json.JSONObject;
|
||||||
|
|
||||||
@Cucumber
|
@Cucumber
|
||||||
public class MyStepdefs {
|
public class MyStepDefTests {
|
||||||
@Given("The following data in a table")
|
@Given("The following data in a table")
|
||||||
public void theFollowingDataInATable(DataTable dataTable) {
|
public void theFollowingDataInATable(DataTable dataTable) {
|
||||||
JSONObject dataTableAsJson = new JsonUtils().getJsonFromDataTable(dataTable);
|
JSONObject dataTableAsJson = JsonUtils.getJsonFromDataTable(dataTable);
|
||||||
System.out.println(dataTableAsJson);
|
System.out.println(dataTableAsJson);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,8 +13,6 @@ import java.io.IOException;
|
||||||
|
|
||||||
public class UnitTests {
|
public class UnitTests {
|
||||||
|
|
||||||
public JsonUtils jsonUtils = new JsonUtils();
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Convert A Google JSON to a Kong JSON")
|
@DisplayName("Convert A Google JSON to a Kong JSON")
|
||||||
public void TestGetJsonFromGson() {
|
public void TestGetJsonFromGson() {
|
||||||
|
@ -23,7 +21,7 @@ public class UnitTests {
|
||||||
JsonObject gJsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
|
JsonObject gJsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
|
||||||
|
|
||||||
//When we convert the object
|
//When we convert the object
|
||||||
JSONObject jObj = jsonUtils.getJsonFromGson(gJsonObject);
|
JSONObject jObj = JsonUtils.getJsonFromGson(gJsonObject);
|
||||||
|
|
||||||
//Then the new object should be a Kong JSONObject
|
//Then the new object should be a Kong JSONObject
|
||||||
Assertions.assertEquals(JSONObject.class,jObj.getClass());
|
Assertions.assertEquals(JSONObject.class,jObj.getClass());
|
||||||
|
@ -32,9 +30,9 @@ public class UnitTests {
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Read JSON From a File")
|
@DisplayName("Read JSON From a File")
|
||||||
public void TestGetDataFromJson() {
|
public void TestGetDataFromJson() {
|
||||||
JsonObject fixtureData = null;
|
JsonObject fixtureData;
|
||||||
try {
|
try {
|
||||||
fixtureData = jsonUtils.getFixtureDataFrom("flerbus");
|
fixtureData = JsonUtils.getFixtureDataFrom("flerbus");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue