How to test a Datastream with jsonobject in Apache Flink

Viewed 281

I am new to testing and i am trying to write a unit test cases on a Flink Datastream which takes input a jsonobject and passes the json object to a processfuntion and it returns a valid or invalid jsonobject when certain rule conditions are met below is the junit test case, below i am trying to compare the output jsonobject from process function with the jsonobject of the input file

@Test
public void testcompareInputAndOutputDataJSONSignal() throws Exception {

    org.json.JSONObject jsonObject = toJsonObject();
    String input = jsonObject.toString();
    String output = JSONDataStreamOutput();
    assertEquals(mapper.readTree(input), mapper.readTree(output));
}

below is my toJSONObject and JSONDataStream meathods

public static JSONObject toJsonObject() throws IOException, ParseException {
        JSONParser jsonParser = new JSONParser();
        FileReader fileReader = new FileReader(getFileFromResources("input.json"));
        JSONObject obj = (JSONObject) jsonParser.parse(fileReader);
        return obj;
    }

public String SignalDataStreamOutput() throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream<JSONObject> validSignal = env.fromElements(toJsonObject())
                .process(new JsonFilter());
        String outputFolder = "output";
        validSignal.writeAsText(outputFolder).setParallelism(1);
        env.execute();
        String content = new String(Files.readAllBytes(Paths.get("output.txt")));
        return content;
}

What i am doing is i am converting a jsonfile to jsonobject using the toJSONObject method and sending to a data stream using SignalDataStreamOutput method which will intern send it to a process function in JsonFilter class and validate it against a set of rules and if it's valid it will return a jsonobject and when trying to access the jsonobject directly from stream i am getting value like org.apache.flink@994jdkeiri so i am trying to write the output to a file and trying to read it back to a string and comparing it in test method but this is a work around process and i found a link to use Mockito framework here i changed it to use json object like below

final Collector<JSONObject> collectorMock = (Collector<JSONObject>)Mockito.mock(JsonFilter.class);
final Context contextMock = Mockito.mock(Context.class);

@Test
    public void testcompareInputAndOutputDataForValidSignal() throws Exception {

        org.json.JSONObject jsonObject = convertToJsonObject();
        Mockito.verify(collectorMock).collect(jsonObject);
    }

but the above approach is also not working can you suggest me simplified approach to test the json object

0 Answers
Related