Best EvoMaster code snippet using org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder.isNativeThriftDto
Source:RPCEndpointsBuilder.java
...562 return getActualType(map, (TypeVariable) t);563 return t;564 }565 private static void getAllFields(Class<?> clazz, List<Field> fieldList, RPCType type){566 if (type == RPCType.THRIFT && isNativeThriftDto(clazz)){567 getFieldForNativeThriftDto(clazz, fieldList);568 return;569 }570 fieldList.addAll(0, Arrays.asList(clazz.getDeclaredFields()));571 if (!Exception.class.isAssignableFrom(clazz) && clazz.getSuperclass() != null && clazz.getSuperclass() != Object.class)572 getAllFields(clazz.getSuperclass(), fieldList, type);573 }574 private static Map<Integer, CustomizedRequestValueDto> getCustomizationBasedOnSpecifiedType(Map<Integer, CustomizedRequestValueDto> customizationDtos, String objTypeName){575 if (customizationDtos == null) return null;576 return customizationDtos.entrySet().stream().filter(s-> s.getValue().specificRequestTypeName == null ||577 s.getValue().specificRequestTypeName.equals(objTypeName)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));578 }579 private static String findGetterOrSetter(Class<?> clazz, Field field, boolean findGetter){580 List<Method> found;581 if (findGetter){582 found = Arrays.stream(clazz.getMethods()).filter(m->583 Modifier.isPublic(m.getModifiers()) &&584// (m.getName().equalsIgnoreCase("get"+field.getName()) || m.getName().equalsIgnoreCase("is"+field.getName())) &&585 isGetter(field.getName(), m.getName(), field.getType().getTypeName()) &&586 m.getParameterCount() == 0587 ).collect(Collectors.toList());588 }else {589 found = Arrays.stream(clazz.getMethods()).filter(m->590 Modifier.isPublic(m.getModifiers()) &&591// m.getName().equalsIgnoreCase("set"+field.getName()) &&592 isSetter(field.getName(), m.getName(), field.getType().getTypeName()) &&593 m.getParameterCount() == 1 &&594 (m.getParameterTypes()[0].equals(field.getType()) || m.getParameterTypes()[0].equals(PrimitiveOrWrapperParam.getPrimitiveOrWrapper(field.getType())))595 ).collect(Collectors.toList());596 }597 if (found.size() == 1)598 return found.get(0).getName();599 String msg = "RPC extract schema Error: cannot access field property, there exist "+found.size()+" methods to access the field "+ field.getName() + " for the class "+ clazz.getName();600 if (found.size() > 1){601 /*602 instead of throwing the exception,603 provide a warning and use the first one604 */605 SimpleLogger.uniqueWarn(msg);606 return found.get(0).getName();607 }608 SimpleLogger.uniqueWarn(msg);609 return null;610 }611 private static boolean isSetter(String fieldName, String methodName, String type){612 boolean isBoolean = type.equals(Boolean.class.getName()) || type.equals(boolean.class.getName());613 String fieldText = fieldName;614 if (isBoolean && fieldText.startsWith("is") && fieldText.length() > 2)615 fieldText = fieldText.substring(2);616 String gsMethod = "set";617 return methodName.equalsIgnoreCase(gsMethod+fieldText) || methodName.equalsIgnoreCase(gsMethod+fieldName);618 }619 private static boolean isGetter(String fieldName, String methodName, String type){620 boolean isBoolean = type.equals(Boolean.class.getName()) || type.equals(boolean.class.getName());621 return methodName.equalsIgnoreCase("get"+fieldName) || (isBoolean && (methodName.equalsIgnoreCase(fieldName) || methodName.equalsIgnoreCase("is"+fieldName)));622 }623 private static void handleNamedValueWithCustomizedDto(NamedTypedValue namedTypedValue, Map<Integer, CustomizedRequestValueDto> customizationDtos, Set<String> relatedCustomization){624 List<String> candidateReferences = new ArrayList<>();625 List<NamedTypedValue> candidates = new ArrayList<>();626 customizationDtos.forEach((i, dto)->{627 if (dto.combinedKeyValuePairs != null628 // && (dto.specificRequestTypeName == null || dto.specificRequestTypeName.equals(namedTypedValue.getType().getFullTypeName()))629 ){630 dto.combinedKeyValuePairs.forEach(p->{631 if (p.fieldKey.equals(namedTypedValue.getName())){632 NamedTypedValue copy = namedTypedValue.copyStructureWithProperties();633 boolean ok = setNamedValueBasedOnCandidates(copy, p.fieldValue);634 if (ok){635 if (!candidateReferences.contains(""+i)){636 relatedCustomization.add(""+i);637 candidateReferences.add(""+i);638 candidates.add(copy);639 } else640 throw new IllegalArgumentException("Error: there should not exist same key with the name "+p.fieldKey+"in a combinedKeyValuePairs");641 }642 }643 });644 }645 });646 if (!candidates.isEmpty()){647 namedTypedValue.setCandidateReferences(candidateReferences);648 namedTypedValue.setCandidates(candidates);649 return;650 }651 // check for keyValues652 List<CustomizedRequestValueDto> ikey = customizationDtos.values().stream().filter(s-> s.keyValues!= null && s.keyValues.key.equals(namedTypedValue.getName())653 //&& (s.specificRequestTypeName== null || s.specificRequestTypeName.equals(namedTypedValue.getType().getFullTypeName()))654 ).collect(Collectors.toList());655 if (ikey.size() == 1){656 setCandidatesForNamedValue(namedTypedValue, ikey.get(0));657 } else if (ikey.size() > 1){658 throw new IllegalStateException("Error: more than one Dto for independent key with "+getKeyForCustomizedRequestValueDto(ikey.get(0)));659 }660 }661 private static void setCandidatesForNamedValue(NamedTypedValue namedTypedValue, CustomizedRequestValueDto customizedRequestValueDto){662 boolean handled = true;663 List<NamedTypedValue> candidates = new ArrayList<>();664 if (namedTypedValue instanceof PrimitiveOrWrapperParam || namedTypedValue instanceof StringParam || namedTypedValue instanceof ByteBufferParam){665 for (String v: customizedRequestValueDto.keyValues.values){666 NamedTypedValue copy= namedTypedValue.copyStructureWithProperties();667 handled = handled && setNamedValueBasedOnCandidates(copy, v);668 candidates.add(copy);669 }670 }else {671 SimpleLogger.uniqueWarn("Error: Do not support configuring pre-defined values for the type "+namedTypedValue.getType().getFullTypeName());672 return;673 }674 if (handled){675 namedTypedValue.setCandidates(candidates);676 }677 }678 private static boolean setNamedValueBasedOnCandidates(NamedTypedValue copy, String value){679 try {680 if (copy instanceof PrimitiveOrWrapperParam){681 ((PrimitiveOrWrapperParam) copy).setValueBasedOnStringValue(value);682 }else if (copy instanceof StringParam)683 copy.setValue(value);684 else if (copy instanceof ByteBufferParam)685 copy.setValue(value.getBytes());686 }catch (RuntimeException exception){687 SimpleLogger.uniqueWarn("Error: fail to generate candidates with string value "+value+" for "+copy.getName() +" with type "+copy.getType().getFullTypeName());688 return false;689 }690 return true;691 }692 private static void handleConstraint(NamedTypedValue namedTypedValue, Annotation annotation, List<CustomizedNotNullAnnotationForRPCDto> notNullAnnotations){693 if (annotation.annotationType().getName().startsWith("javax.validation.constraints")){694 JavaXConstraintHandler.handleParam(namedTypedValue, annotation);695 } else if (notNullAnnotations != null && !notNullAnnotations.isEmpty()){696 boolean isRequired = notNullAnnotations.stream().anyMatch(a-> isRequired(annotation, a));697 namedTypedValue.setNullable(!isRequired);698 }699 // remove the log for the moment, might need it later700// else {701// SimpleLogger.info("annotation with "+ annotation.annotationType().getName()+" is not handled");702// }703 }704 private static boolean isRequired(Annotation annotation, CustomizedNotNullAnnotationForRPCDto notNullAnnotations){705 if (annotation.annotationType().getName().equals(notNullAnnotations.annotationType)){706 if (notNullAnnotations.annotationMethod != null && notNullAnnotations.equalsTo !=null){707 try {708 return annotation.annotationType().getDeclaredMethod(notNullAnnotations.annotationMethod).invoke(annotation).equals(notNullAnnotations.equalsTo);709 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {710 SimpleLogger.uniqueWarn("Error: fail to invoke the specified method in the annotation with the error msg:"+e.getMessage());711 return false;712 }713 }714 return true;715 }716 return false;717 }718 private static Class<?> getTemplateClass(Type type, Map<TypeVariable, Type> genericTypeMap){719 Type actualType = type;720 if (type instanceof TypeVariable)721 actualType = getActualType(genericTypeMap, (TypeVariable) type);722 if (actualType instanceof ParameterizedType){723 return (Class<?>) ((ParameterizedType)actualType).getRawType();724 }else if (actualType instanceof Class)725 return (Class<?>) actualType;726 throw new RuntimeException("unhanded type:"+ type);727 }728 /**729 * there might exist some additional info generated by eg instrumentation730 * then we need to skip reflection on such info with the specified name731 * @param name is a name of object to check732 * @return whether to skip the object733 */734 private static boolean doSkipReflection(String name){735 return name.equals("$jacocoData");736 }737 private static boolean isMetaMap(Field field){738 boolean result = field.getName().equals("metaDataMap")739 && Map.class.isAssignableFrom(field.getType());740 if (!result) return result;741 Type genericType = field.getGenericType();742 Type valueType = ((ParameterizedType) genericType).getActualTypeArguments()[1];743 return valueType.getTypeName().equals("org.apache.thrift.meta_data.FieldMetaData");744 }745 private final static String NATIVE_THRIFT_DTO_INTERFACE = "org.apache.thrift.TBase";746 private final static String NATIVE_THRIFT_FIELD_SCHEMA = "metaDataMap";747 private static boolean isNativeThriftDto(Class<?> clazz){748 return clazz.getInterfaces().length > 0 && Arrays.stream(clazz.getInterfaces()).anyMatch(i-> i.getName().equals(NATIVE_THRIFT_DTO_INTERFACE));749 }750 private static void getFieldForNativeThriftDto(Class<?> clazz, List<Field> fields){751 try {752 Field metaMap_field = clazz.getDeclaredField(NATIVE_THRIFT_FIELD_SCHEMA);753 if (isMetaMap(metaMap_field)){754 Object metaMap = metaMap_field.get(null);755 if (metaMap instanceof Map){756 for (Object f : ((Map)metaMap).values()){757 Field fname = f.getClass().getDeclaredField("fieldName");758 fname.setAccessible(true);759 String name = (String) fname.get(f);760 fields.add(clazz.getDeclaredField(name));761 }762 }763 }764 } catch (NoSuchFieldException | IllegalAccessException e) {765 SimpleLogger.uniqueWarn("Error: fail to get the metaDataMap field in native dto");766 }767 }768 private static void handleNativeRPCConstraints(Class<?> clazz, List<NamedTypedValue> fields, RPCType type){769 if (type == RPCType.THRIFT && isNativeThriftDto(clazz)){770 try {771 Field metaMap_field = clazz.getDeclaredField(NATIVE_THRIFT_FIELD_SCHEMA);772 if (isMetaMap(metaMap_field))773 handleMetaMap(metaMap_field, fields);774 } catch (NoSuchFieldException e) {775 SimpleLogger.uniqueWarn("Error: fail to get the metaDataMap field in native dto");776 }777 }778 }779 private static void handleMetaMap(Field metaMap_field, List<NamedTypedValue> fields){780 Object metaMap = null;781 try {782 metaMap = metaMap_field.get(null);783 if (metaMap instanceof Map){...
isNativeThriftDto
Using AI Code Generation
1import org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder2import org.evomaster.client.java.controller.problem.rpc.RPCMethod3import org.evomaster.client.java.controller.problem.rpc.RPCMethodParameter4import org.evomaster.client.java.controller.problem.rpc.RPCMethodType5import org.evomaster.client.java.controller.problem.rpc.RPCReturnType6import org.evomaster.client.java.controller.problem.rpc.RPCTarget7import org.evomaster.client.java.controller.problem.rpc.RPCVariable8import org.evomaster.client.java.controller.problem.rpc.RPCVariableType9import org.evomaster.client.java.controller.problem.rpc.RPCVariableValue10import org.evomaster.client.java.controller.problem.rpc.RPCVariableValueDto11import org.evomaster.client.java.controller.problem.rpc.RPCVariableValueSimple12import org.evomaster.client.java.controller.problem.rpc.RPCTarget13import org.evomaster.client.java.controller.problem.rpc.RPCMethod14import org.evomaster.client.java.controller.problem.rpc.RPCMethodParameter15import org.evomaster.client.java.controller.problem.rpc.RPCReturnType16import org.evomaster.client.java.controller.problem.rpc.RPCVariable17import org.evomaster.client.java.controller.problem.rpc.RPCVariableType18import org.evomaster.client.java.controller.problem.rpc.RPCVariableValue19import org.evomaster.client.java.controller.problem.rpc.RPCVariableValueDto20import org.evomaster.client.java.controller.problem.rpc.RPCVariableValueSimple21import org.evomaster.client.java.controller.problem.rpc.RPCMethodType22import org.evomaster.client.java.controller.problem.rpc.RPCVariableValueDto23import org.evo
isNativeThriftDto
Using AI Code Generation
1import org.evomaster.client.java.controller.problem.ProblemInfo2import org.evomaster.client.java.controller.problem.RestProblem3import org.evomaster.client.java.controller.problem.RestResourceCalls4import org.evomaster.client.java.controller.problem.RestResourceInfo5import org.evomaster.client.java.controller.problem.rest.*6import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftDto7import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftEnumDto8import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftObjectDto9import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftPrimitiveDto10import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftType11import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftUnionDto12import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftVoidDto13import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftWrapperDto14import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftWrapperType15import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlDto16import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlType17import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlWrapperDto18import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlWrapperType19import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlUnionDto20import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlVoidDto21import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlWrapperType.*22import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlType.*23import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftWrapperType.*24import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftType.*25import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlUnionDto.*26import org.evomaster.client.java.controller.problem.rpc.thrift.ThriftXmlWrapperDto.*
isNativeThriftDto
Using AI Code Generation
1public class ExampleTest {2 public void testRunEM() {3 Example example = new Example();4 ExampleDto dto = new ExampleDto();5 dto.setField1("field1");6 dto.setField2("field2");7 dto.setField3("field3");8 ExampleThriftDto thriftDto = new ExampleThriftDto();9 thriftDto.setField1("field1");10 thriftDto.setField2("field2");11 thriftDto.setField3("field3");12 ExampleThriftDto thriftDto2 = new ExampleThriftDto();13 thriftDto2.setField1("field1");14 thriftDto2.setField2("field2");15 thriftDto2.setField3("field3");16 ExampleThriftDto thriftDto3 = new ExampleThriftDto();17 thriftDto3.setField1("field1");18 thriftDto3.setField2("field2");19 thriftDto3.setField3("field3");20 ExampleThriftDto thriftDto4 = new ExampleThriftDto();21 thriftDto4.setField1("field1");22 thriftDto4.setField2("field2");23 thriftDto4.setField3("field3");24 ExampleThriftDto thriftDto5 = new ExampleThriftDto();25 thriftDto5.setField1("field1");26 thriftDto5.setField2("field2");
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!!