How to use hasValueInData method of org.evomaster.e2etests.utils.GraphQLTestBase class

Best EvoMaster code snippet using org.evomaster.e2etests.utils.GraphQLTestBase.hasValueInData

Source:GraphQLTestBase.java Github

copy

Full Screen

...82 protected void assertAnyWithErrors(Solution<GraphQLIndividual> solution) {83 boolean ok = solution.getIndividuals().stream().anyMatch(ind -> !noneWithErrors(ind));84 assertTrue(ok);85 }86 protected boolean hasValueInData(EvaluatedIndividual<GraphQLIndividual> ind, String value) {87 List<GraphQLAction> actions = ind.getIndividual().seeActions();88 boolean stopped = false;89 for (int i = 0; i < actions.size() && !stopped; i++) {90 GraphQlCallResult res = (GraphQlCallResult) ind.seeResults(null).get(i);91 stopped = res.getStopping();92 if (hasValueInData(res, value)){93 return true;94 }95 }96 return false;97 }98 /**99 *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();...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful