Best EvoMaster code snippet using org.evomaster.e2etests.utils.GraphQLTestBase.getDataInGraphQLResults
Source:GraphQLTestBase.java
...100 * @param result is the GraphQL action result to be checked101 * @param propertyName is a property of the results to be extracted102 * @return extracted content based on propertyName103 */104 private JsonNode getDataInGraphQLResults(GraphQlCallResult result, String propertyName){105 Integer statusCode = result.getStatusCode();106 if (!statusCode.equals(200)) {107 return null;108 }109 String body = result.getBody();110 ObjectMapper jackson = new ObjectMapper();111 JsonNode node;112 try {113 node = jackson.readTree(body);114 } catch (JsonProcessingException e) {115 return null;116 }117 return node.findPath(propertyName);118 }119 private boolean hasValueInData(GraphQlCallResult result, String value){120 JsonNode data = getDataInGraphQLResults(result, "data");121 if (data == null) return false;122 /*123 if (!data.isNull() && !data.isMissingNode() && data.asText().contains(value)) {124 return true;125 }*/126 if (!data.isNull() && !data.isMissingNode() && data.toString().contains(value)) {127 return true;128 }129 return false;130 }131 protected void assertValueInDataAtLeastOnce(Solution<GraphQLIndividual> solution, String value) {132 boolean ok = solution.getIndividuals().stream().anyMatch(ind -> hasValueInData(ind, value));133 assertTrue(ok);134 }135 protected void assertHasAtLeastOne(Solution<GraphQLIndividual> solution, String methodName, GQMethodType type, int expectedStatusCode, String inResponse){136 boolean ok = solution.getIndividuals().stream().anyMatch(137 ind -> hasAtLeastOne(ind, methodName, type, expectedStatusCode, inResponse));138 String errorMsg = "Seed " + (defaultSeed-1)+". ";139 errorMsg += "Missing " + expectedStatusCode + " " + type + " " + methodName + " " + inResponse + "\n";140 assertTrue(ok, errorMsg + graphActions(solution));141 }142 protected void assertHasAtLeastOne(Solution<GraphQLIndividual> solution,143 String methodName, GQMethodType type, int expectedStatusCode,144 List<String> inResponse, boolean and){145 boolean ok;146 if (and){147 ok = inResponse.stream().allMatch(s-> solution.getIndividuals().stream().anyMatch(ind ->148 hasAtLeastOne(ind, methodName, type, expectedStatusCode, s)));149 }else{150 ok = inResponse.stream().anyMatch(s-> solution.getIndividuals().stream().anyMatch(ind ->151 hasAtLeastOne(ind, methodName, type, expectedStatusCode, s)));152 }153 String errorMsg = "Seed " + (defaultSeed-1)+". ";154 errorMsg += "Missing " + expectedStatusCode + " " + type + " " + methodName + " " + (and?" all of " : " any of ") +String.join(",", inResponse) + "\n";155 assertTrue(ok, errorMsg + graphActions(solution));156 }157 private boolean hasAtLeastOne(EvaluatedIndividual<GraphQLIndividual> ind, String methodName, GQMethodType type, int expectedStatusCode, String inResponse){158 if (ind.getIndividual().seeActions().size() != ind.seeResults(null).size()){159 throw new IllegalStateException(String.format("mismatched size of results (%d) with calls (%d) for GraphQLIndividual",160 ind.seeResults(null).size(), ind.getIndividual().seeActions().size()));161 }162 List<GraphQLAction> actions = ind.getIndividual().seeActions();163 boolean stopped = false;164 for (int i = 0; i < actions.size() && !stopped; i++) {165 GraphQlCallResult res = (GraphQlCallResult) ind.seeResults(null).get(i);166 stopped = res.getStopping();167 boolean matched = actions.get(i).getMethodName().equals(methodName) &&168 actions.get(i).getMethodType().equals(type) && res.getStatusCode() == expectedStatusCode;169 if(!matched) continue;170 if (inResponse == null) continue;171 if (hasValueInData(res, inResponse)) return true;172 }173 return false;174 }175 protected String graphActions(Solution<GraphQLIndividual> solution) {176 StringBuffer msg = new StringBuffer("Graph calls:\n");177 solution.getIndividuals().stream().flatMap(ind -> ind.evaluatedActions().stream())178 .filter(ea -> ea.getAction() instanceof GraphQLAction)179 .map(ea -> {180 GraphQlCallResult res = (GraphQlCallResult)ea.getResult();181 String s = res.getStatusCode() + " ";182 JsonNode node = getDataInGraphQLResults(res, "data");183 if (node != null) s += " data:"+node.toString();184 node = getDataInGraphQLResults(res, "errors");185 if (node != null && (!node.isEmpty() || !node.isMissingNode())) s += " errors:"+node.toString();186 s += ea.getAction().toString() + "\n";187 return s;188 })189 .sorted()190 .forEach(s -> msg.append(s));191 ;192 return msg.toString();193 }194}...
getDataInGraphQLResults
Using AI Code Generation
1 public void test_0() throws Exception {2 String query = "{\n" +3 " getPerson(id: 1) {\n" +4 " }\n" +5 "}";6 String response = makeRequest(query);7 assertEquals(200, responseCode);8 List<PersonDto> results = getDataInGraphQLResults(response, "getPerson", PersonDto.class);9 assertEquals(1, results.size());10 assertEquals(1, results.get(0).getId());11 assertEquals("Foo", results.get(0).getName());12 assertEquals(33, results.get(0).getAge());13 }14}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!