Make JsonUtils methods static

This commit is contained in:
Greg Gauthier 2023-07-19 10:17:00 +01:00
parent 0c1a40cd2f
commit a7e3b2ea4a
3 changed files with 9 additions and 11 deletions

View File

@ -14,23 +14,23 @@ import java.util.Map;
import java.util.stream.Collectors;
public class JsonUtils {
public JSONObject getJsonFromGson(JsonObject gJsonObject) {
public static JSONObject getJsonFromGson(JsonObject gJsonObject) {
return new JSONObject(gJsonObject.toString());
}
public JSONObject getJsonFromDataTable(DataTable dataTable) {
public static JSONObject getJsonFromDataTable(DataTable dataTable) {
List<Map<String, String>> tableDetails =
dataTable.transpose().asMaps(String.class, String.class);
String bodyString = new Gson().toJson(tableDetails.get(0)); //root element
return new JSONObject(bodyString);
}
public JsonObject getFixtureDataFrom(String filename) throws IOException {
public static JsonObject getFixtureDataFrom(String filename) throws IOException {
String json = getJsonAsString(filename);
return new Gson().fromJson(json, JsonObject.class);
}
private String getJsonAsString(String filename) throws IOException {
private static String getJsonAsString(String filename) throws IOException {
InputStream inputStream =
JsonUtils.class
.getResourceAsStream(

View File

@ -7,10 +7,10 @@ import io.cucumber.junit.platform.engine.Cucumber;
import kong.unirest.json.JSONObject;
@Cucumber
public class MyStepdefs {
public class MyStepDefTests {
@Given("The following data in a table")
public void theFollowingDataInATable(DataTable dataTable) {
JSONObject dataTableAsJson = new JsonUtils().getJsonFromDataTable(dataTable);
JSONObject dataTableAsJson = JsonUtils.getJsonFromDataTable(dataTable);
System.out.println(dataTableAsJson);
}
}

View File

@ -13,8 +13,6 @@ import java.io.IOException;
public class UnitTests {
public JsonUtils jsonUtils = new JsonUtils();
@Test
@DisplayName("Convert A Google JSON to a Kong JSON")
public void TestGetJsonFromGson() {
@ -23,7 +21,7 @@ public class UnitTests {
JsonObject gJsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
//When we convert the object
JSONObject jObj = jsonUtils.getJsonFromGson(gJsonObject);
JSONObject jObj = JsonUtils.getJsonFromGson(gJsonObject);
//Then the new object should be a Kong JSONObject
Assertions.assertEquals(JSONObject.class,jObj.getClass());
@ -32,9 +30,9 @@ public class UnitTests {
@Test
@DisplayName("Read JSON From a File")
public void TestGetDataFromJson() {
JsonObject fixtureData = null;
JsonObject fixtureData;
try {
fixtureData = jsonUtils.getFixtureDataFrom("flerbus");
fixtureData = JsonUtils.getFixtureDataFrom("flerbus");
} catch (IOException e) {
throw new RuntimeException(e);
}