Best EvoMaster code snippet using org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder.getClientClass
Source:RPCEndpointsBuilder.java
...134 List<String> skippedEndpoints = new ArrayList<>();135 Map<Integer, EndpointSchema> authEndpoints = new HashMap<>();136 try {137 Class<?> interfaze = Class.forName(interfaceName);138 InterfaceSchema schema = new InterfaceSchema(interfaceName, endpoints, getClientClass(client) , rpcType, skippedEndpoints, authEndpoints, endpointsForAuth);139 for (Method m : interfaze.getDeclaredMethods()) {140 if (filterMethod(m, skipEndpointsByName, skipEndpointsByAnnotation, involveEndpointsByName, involveEndpointsByAnnotation)){141 try{142 EndpointSchema endpointSchema = build(schema, m, rpcType, authenticationDtoList, customizedRequestValueDtos, notNullAnnotations);143 endpoints.add(endpointSchema);144 }catch (RuntimeException exception){145 /*146 TODO might send such log to core in order to better identify problems which is not handled yet147 */148 SimpleLogger.error("EM Driver Error: fail to handle the endpoint schema "+m.getName()+" with the error msg:"+exception.getMessage());149 }150 } else {151 skippedEndpoints.add(m.getName());152 }153 List<AuthenticationDto> auths = getAuthEndpointInInterface(authenticationDtoList, interfaceName, m);154 if (auths != null && !auths.isEmpty()){155 try{156 // handle endpoint which is for auth setup157 EndpointSchema authEndpoint = build(schema, m, rpcType, null, customizedRequestValueDtos,notNullAnnotations);158 endpointsForAuth.add(authEndpoint);159 for (AuthenticationDto auth: auths){160 EndpointSchema copy = authEndpoint.copyStructure();161 if (auth.jsonAuthEndpoint == null){162 throw new IllegalArgumentException("Driver Config Error: now we only support auth info specified with JsonAuthRPCEndpointDto");163 }164 int index = authenticationDtoList.indexOf(auth);165 // set value based on specified info166 if (copy.getRequestParams().size() != auth.jsonAuthEndpoint.jsonPayloads.size())167 throw new IllegalArgumentException("Driver Config Error: mismatched size of jsonPayloads ("+auth.jsonAuthEndpoint.classNames.size()+") with real endpoint ("+authEndpoint.getRequestParams().size()+").");168 setAuthEndpoint(copy, auth.jsonAuthEndpoint);169 authEndpoints.put(index, copy);170 }171 }catch (RuntimeException exception){172 SimpleLogger.error("EM Driver Error: fail to handle the authEndpoint schema "+m.getName()+" with the error msg:"+exception.getMessage());173 }174 }175 }176 return schema;177 } catch (ClassNotFoundException e) {178 throw new RuntimeException("cannot find the interface with the name (" + interfaceName + ") and the error message is " + e.getMessage());179 }180 }181 /**182 * build the local auth setup183 * @param authenticationDtoList a list of auth info specified by user184 * @return a map of such local auth setup185 * key - index at a list of auth info specified by user186 * value - local endpoint187 */188 public static Map<Integer, LocalAuthSetupSchema> buildLocalAuthSetup(List<AuthenticationDto> authenticationDtoList){189 if (authenticationDtoList==null || authenticationDtoList.isEmpty()) return null;190 Map<Integer, LocalAuthSetupSchema> map = new HashMap<>();191 for (AuthenticationDto dto : authenticationDtoList){192 if (dto.localAuthSetup != null){193 int index = authenticationDtoList.indexOf(dto);194 LocalAuthSetupSchema local = new LocalAuthSetupSchema();195 local.getRequestParams().get(0).setValueBasedOnInstance(dto.localAuthSetup.authenticationInfo);196 map.put(index, local);197 }198 }199 return map;200 }201 private static void setAuthEndpoint(EndpointSchema authEndpoint, JsonAuthRPCEndpointDto jsonAuthEndpoint) throws ClassNotFoundException{202 if (jsonAuthEndpoint.classNames != null && jsonAuthEndpoint.classNames.size() != jsonAuthEndpoint.jsonPayloads.size())203 throw new IllegalArgumentException("Driver Config Error: to specify inputs for auth endpoint, classNames and jsonPayloads should have same size");204 for (int i = 0; i < authEndpoint.getRequestParams().size(); i++){205 NamedTypedValue inputParam = authEndpoint.getRequestParams().get(i);206 String jsonString = jsonAuthEndpoint.jsonPayloads.get(i);207 if (jsonAuthEndpoint.classNames == null){208 setNamedValueBasedOnJsonString(inputParam,jsonString, i);209 }else{210 Class<?> clazz = Class.forName(jsonAuthEndpoint.classNames.get(i));211 try {212 Object value = objectMapper.readValue(jsonString, clazz);213 inputParam.setValueBasedOnInstance(value);214 } catch (JsonProcessingException e) {215 SimpleLogger.uniqueWarn("Driver Config Error: a jsonPayload at ("+i+") cannot be read as the object "+jsonAuthEndpoint.classNames.get(i));216 setNamedValueBasedOnJsonString(inputParam,jsonString, i);217 }218 }219 }220 }221 private static void setNamedValueBasedOnJsonString(NamedTypedValue inputParam, String jsonString, int index){222 if (inputParam instanceof StringParam || inputParam instanceof PrimitiveOrWrapperParam || inputParam instanceof ByteBufferParam){223 setNamedValueBasedOnCandidates(inputParam, jsonString);224 } else if (inputParam instanceof ObjectParam){225 try {226 JsonNode node = objectMapper.readTree(jsonString);227 List<NamedTypedValue> fields = new ArrayList<>();228 for (NamedTypedValue f: ((ObjectParam) inputParam).getType().getFields()){229 NamedTypedValue v = f.copyStructureWithProperties();230 if (node.has(v.getName())){231 setNamedValueBasedOnCandidates(f, node.textValue());232 fields.add(v);233 }else {234 SimpleLogger.uniqueWarn("Driver Config Error: cannot find field with the name "+v.getName()+" in the specified json");235 }236 }237 inputParam.setValue(fields);238 } catch (JsonProcessingException ex) {239 SimpleLogger.uniqueWarn("Driver Config Error: a jsonPayload at ("+index+") cannot be read as a JSON object with error:" +ex.getMessage());240 }241 }242 }243 private static List<AuthenticationDto> getAuthEndpointInInterface(List<AuthenticationDto> authenticationDtos, String interfaceName, Method method){244 if (authenticationDtos == null) return null;245 for (AuthenticationDto dto : authenticationDtos){246 if (dto.localAuthSetup == null && (dto.jsonAuthEndpoint == null || dto.jsonAuthEndpoint.endpointName == null || dto.jsonAuthEndpoint.interfaceName == null)){247 SimpleLogger.uniqueWarn("Driver Config Error: To specify auth for RPC, either localAuthSetup or jsonAuthEndpoint should be specified." +248 "For JsonAuthRPCEndpointDto, endpointName and interfaceName cannot be null");249 }250 }251 return authenticationDtos.stream().filter(a-> a.jsonAuthEndpoint != null252 && a.jsonAuthEndpoint.endpointName.equals(method.getName())253 && a.jsonAuthEndpoint.interfaceName.equals(interfaceName)).collect(Collectors.toList());254 }255 private static boolean filterMethod(Method endpoint,256 List<String> skipEndpointsByName, List<String> skipEndpointsByAnnotation,257 List<String> involveEndpointsByName, List<String> involveEndpointsByAnnotation){258 if (skipEndpointsByName != null && involveEndpointsByName != null)259 throw new IllegalArgumentException("Driver Config Error: skipEndpointsByName and involveEndpointsByName should not be specified at same time.");260 if (skipEndpointsByAnnotation != null && involveEndpointsByAnnotation != null)261 throw new IllegalArgumentException("Driver Config Error: skipEndpointsByAnnotation and involveEndpointsByAnnotation should not be specified at same time.");262 if (skipEndpointsByName != null || skipEndpointsByAnnotation != null)263 return !anyMatchByNameAndAnnotation(endpoint, skipEndpointsByName, skipEndpointsByAnnotation);264 if (involveEndpointsByName != null || involveEndpointsByAnnotation != null)265 return anyMatchByNameAndAnnotation(endpoint, involveEndpointsByName, involveEndpointsByAnnotation);266 return true;267 }268 private static boolean anyMatchByNameAndAnnotation(Method endpoint, List<String> names, List<String> annotations){269 boolean anyMatch = false;270 if (annotations != null){271 for (Annotation annotation : endpoint.getAnnotations()){272 anyMatch = anyMatch || annotations.contains(annotation.annotationType().getName());273 }274 }275 if (names != null)276 anyMatch = anyMatch || names.contains(endpoint.getName());277 return anyMatch;278 }279 private static String getClientClass(Object client){280 if (client == null) return null;281 String clazzType = client.getClass().getName();282 // handle com.sun.proxy283 if (!clazzType.startsWith("com.sun.proxy.")){284 return clazzType;285 }286 Class<?>[] clazz = client.getClass().getInterfaces();287 if (clazz.length == 0){288 SimpleLogger.error("Error: the client is not related to any interface");289 return null;290 }291 if (clazz.length > 1)292 SimpleLogger.error("ERROR: the client has more than one interfaces");293 return clazz[0].getName();...
getClientClass
Using AI Code Generation
1Class<?> clientClass = Class.forName(clientClassName);2Constructor<?> constructor = clientClass.getConstructor();3Object client = constructor.newInstance();4Method method = clientClass.getMethod("getPetById", long.class);5Object response = method.invoke(client, 1L);6System.out.println(response);
getClientClass
Using AI Code Generation
1Method method = clientClass.getMethod("test", String.class);2String result = (String) method.invoke(null, "test");3public void test_0() throws Throwable {4 String arg0 = "test";5 Assertions.assertNotNull(response);6}7public void test_0() throws Throwable {8 String arg0 = "test";9 Assertions.assertNotNull(response);10}11public void test_0() throws Throwable {12 String arg0 = "test";13 Assertions.assertNotNull(response);14}15public void test_0() throws Throwable {16 String arg0 = "test";17 Assertions.assertNotNull(response);18}19public void test_0() throws Throwable {
getClientClass
Using AI Code Generation
1getClientClass("org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder", "org.evomaster.e2etests.spring.examples.rpc.RPCServiceClient")2getServerClass("org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder", "org.evomaster.e2etests.spring.examples.rpc.RPCServiceServer")3getRpcServiceClass("org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder", "org.evomaster.e2etests.spring.examples.rpc.RPCService")4getRpcServiceClass("org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder", "org.evomaster.e2etests.spring.examples.rpc.RPCService")5getRpcServiceClass("org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder", "org.evomaster.e2etests.spring.examples.rpc.RPCService")6getRpcServiceClass("org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder", "org.evomaster.e2etests.spring.examples.rpc.RPCService")
getClientClass
Using AI Code Generation
1Class<?> clientClass = RPCEndpointsBuilder.getClientClass(endpoint);2Method resetMethod = clientClass.getMethod("reset");3resetMethod.invoke(null);4Method resetClientMethod = clientClass.getMethod("resetClient");5resetClientMethod.invoke(null);6Method resetServerMethod = clientClass.getMethod("resetServer");7resetServerMethod.invoke(null);8Method resetDBMethod = clientClass.getMethod("resetDB");9resetDBMethod.invoke(null);10Method resetCacheMethod = clientClass.getMethod("resetCache");11resetCacheMethod.invoke(null);12Method resetExternalServicesMethod = clientClass.getMethod("resetExternalServices");13resetExternalServicesMethod.invoke(null);14Method resetFileSystemMethod = clientClass.getMethod("resetFileSystem");15resetFileSystemMethod.invoke(null);16Method resetNetworkMethod = clientClass.getMethod("resetNetwork");17resetNetworkMethod.invoke(null);18Method resetEnvMethod = clientClass.getMethod("resetEnv");19resetEnvMethod.invoke(null);20Method resetArgsMethod = clientClass.getMethod("resetArgs");21resetArgsMethod.invoke(null);22Method resetPropertiesMethod = clientClass.getMethod("resetProperties");23resetPropertiesMethod.invoke(null);24Method resetRandomMethod = clientClass.getMethod("resetRandom");25resetRandomMethod.invoke(null);26Method resetInternalStateMethod = clientClass.getMethod("resetInternalState");27resetInternalStateMethod.invoke(null);28Method resetExternalStateMethod = clientClass.getMethod("resetExternalState");29resetExternalStateMethod.invoke(null);30Method resetInternalAndExternalStateMethod = clientClass.getMethod("resetInternalAndExternalState");31resetInternalAndExternalStateMethod.invoke(null);
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!!