Best EvoMaster code snippet using org.evomaster.client.java.controller.problem.rpc.schema.params.ObjectParam.ObjectParam
Source:ExampleBuilderTest.java
...115 assertNotNull(endpoint.getResponse());116 assertNotNull(endpoint.getRequestParams());117 assertEquals(1, endpoint.getRequestParams().size());118 NamedTypedValue p1 = endpoint.getRequestParams().get(0);119 assertTrue(p1 instanceof ObjectParam);120 assertEquals(4, ((ObjectParam)p1).getType().getFields().size());121 for (NamedTypedValue p : ((ObjectParam)p1).getType().getFields()){122 assertTrue(p instanceof StringParam);123 StringParam ns = (StringParam) p;124 if (p.getName().equals("longValue")){125 assertTrue(p.isNullable());126 assertFalse(ns.getMaxInclusive());127 assertTrue(ns.getMinInclusive());128 assertEquals(Long.MAX_VALUE, ns.getMax().longValue());129 assertNull(ns.getMin());130 }else if (p.getName().equals("intValue")){131 assertFalse(p.isNullable());132 assertEquals(Integer.MAX_VALUE, ns.getMax().intValue());133 assertFalse(ns.getMaxInclusive());134 assertTrue(ns.getMinInclusive());135 }else if (p.getName().equals("bigIntegerValue")){136 assertTrue(p.isNullable());137 assertEquals(Long.MAX_VALUE, ns.getMax().longValue());138 assertFalse(ns.getMaxInclusive());139 assertTrue(ns.getMinInclusive());140 }else if (p.getName().equals("bigDecimalValue")){141 assertTrue(p.isNullable());142 assertEquals(Double.MAX_VALUE, ns.getMax().doubleValue());143 assertFalse(ns.getMaxInclusive());144 assertEquals(15, ns.getPrecision());145 assertEquals(5, ns.getScale());146 assertFalse(ns.getMinInclusive());147 assertEquals(0.0, ns.getMin().doubleValue());148 }149 }150 }151 @Test152 public void testImmutableObj(){153 EndpointSchema endpoint = getOneEndpoint("immutableObj");154 assertNotNull(endpoint.getResponse());155 assertNotNull(endpoint.getRequestParams());156 assertEquals(1, endpoint.getRequestParams().size());157 NamedTypedValue p1 = endpoint.getRequestParams().get(0);158 assertTrue(p1 instanceof ObjectParam);159 assertEquals(3, ((ObjectParam)p1).getType().getFields().size());160 for (NamedTypedValue p : ((ObjectParam)p1).getType().getFields()){161 assertFalse(p.isMutable());162 if (p.getName().equals("nullLong")){163 assertTrue(p.isNullable());164 assertNull(p.getDefaultValue());165 }else if (p.getName().equals("pbool")){166 assertFalse(p.isNullable());167 assertNotNull(p.getDefaultValue());168 assertTrue(p.getDefaultValue() instanceof BooleanParam);169 assertFalse(((BooleanParam)p.getDefaultValue()).getValue());170 }else if (p.getName().equals("wbool")){171 assertTrue(p.isNullable());172 assertNotNull(p.getDefaultValue());173 assertTrue(p.getDefaultValue() instanceof BooleanParam);174 assertTrue(((BooleanParam)p.getDefaultValue()).getValue());175 }176 }177 }178 @Test179 public void testbigNumber(){180 EndpointSchema endpoint = getOneEndpoint("bigNumber");181 assertNotNull(endpoint.getResponse());182 assertNotNull(endpoint.getRequestParams());183 assertEquals(1, endpoint.getRequestParams().size());184 NamedTypedValue p1 = endpoint.getRequestParams().get(0);185 assertTrue(p1 instanceof ObjectParam);186 assertEquals(8, ((ObjectParam)p1).getType().getFields().size());187 for (NamedTypedValue p : ((ObjectParam)p1).getType().getFields()){188 assertTrue(p instanceof NumericConstraintBase);189 if (p.getName().equals("bdPositiveFloat")){190 assertEquals(4, ((NumericConstraintBase)p).getPrecision());191 assertEquals(2, ((NumericConstraintBase)p).getScale());192 assertEquals("42.42", ((NumericConstraintBase)p).getMax().toString());193 assertEquals("0", ((NumericConstraintBase)p).getMin().toString());194 assertFalse(((NumericConstraintBase) p).getMinInclusive());195 assertTrue(((NumericConstraintBase) p).getMaxInclusive());196 }else if (p.getName().equals("bdPositiveOrZeroFloat")){197 assertEquals(4, ((NumericConstraintBase)p).getPrecision());198 assertEquals(2, ((NumericConstraintBase)p).getScale());199 assertEquals("42.42", ((NumericConstraintBase)p).getMax().toString());200 assertEquals("0", ((NumericConstraintBase)p).getMin().toString());201 assertTrue(((NumericConstraintBase) p).getMinInclusive());202 assertTrue(((NumericConstraintBase) p).getMaxInclusive());203 } else if (p.getName().equals("bdNegativeFloat")){204 assertEquals(4, ((NumericConstraintBase)p).getPrecision());205 assertEquals(2, ((NumericConstraintBase)p).getScale());206 assertEquals("0", ((NumericConstraintBase)p).getMax().toString());207 assertEquals("-42.42", ((NumericConstraintBase)p).getMin().toString());208 assertTrue(((NumericConstraintBase) p).getMinInclusive());209 assertFalse(((NumericConstraintBase) p).getMaxInclusive());210 } else if (p.getName().equals("bdNegativeOrZeroFloat")){211 assertEquals(4, ((NumericConstraintBase)p).getPrecision());212 assertEquals(2, ((NumericConstraintBase)p).getScale());213 assertEquals("0", ((NumericConstraintBase)p).getMax().toString());214 assertEquals("-42.42", ((NumericConstraintBase)p).getMin().toString());215 assertTrue(((NumericConstraintBase) p).getMinInclusive());216 assertTrue(((NumericConstraintBase) p).getMaxInclusive());217 } else if (p.getName().equals("biPositive")){218 assertEquals(2, ((NumericConstraintBase)p).getPrecision());219 assertEquals(0, ((NumericConstraintBase)p).getScale());220 assertEquals("42", ((NumericConstraintBase)p).getMax().toString());221 assertEquals("0", ((NumericConstraintBase)p).getMin().toString());222 assertFalse(((NumericConstraintBase) p).getMinInclusive());223 assertTrue(((NumericConstraintBase) p).getMaxInclusive());224 }else if (p.getName().equals("biPositiveOrZero")){225 assertEquals(2, ((NumericConstraintBase)p).getPrecision());226 assertEquals(0, ((NumericConstraintBase)p).getScale());227 assertEquals("42", ((NumericConstraintBase)p).getMax().toString());228 assertEquals("0", ((NumericConstraintBase)p).getMin().toString());229 assertTrue(((NumericConstraintBase) p).getMinInclusive());230 assertTrue(((NumericConstraintBase) p).getMaxInclusive());231 } else if (p.getName().equals("biNegative")){232 assertEquals(2, ((NumericConstraintBase)p).getPrecision());233 assertEquals(0, ((NumericConstraintBase)p).getScale());234 assertEquals("0", ((NumericConstraintBase)p).getMax().toString());235 assertEquals("-42", ((NumericConstraintBase)p).getMin().toString());236 assertTrue(((NumericConstraintBase) p).getMinInclusive());237 assertFalse(((NumericConstraintBase) p).getMaxInclusive());238 } else if (p.getName().equals("biNegativeOrZero")){239 assertEquals(2, ((NumericConstraintBase)p).getPrecision());240 assertEquals(0, ((NumericConstraintBase)p).getScale());241 assertEquals("0", ((NumericConstraintBase)p).getMax().toString());242 assertEquals("-42", ((NumericConstraintBase)p).getMin().toString());243 assertTrue(((NumericConstraintBase) p).getMinInclusive());244 assertTrue(((NumericConstraintBase) p).getMaxInclusive());245 }246 }247 BigNumberObj bigNumberObj = new BigNumberObj(){{248 // bigdecimal249 setBdPositiveFloat(new BigDecimal("10.12"));250 setBdPositiveOrZeroFloat(new BigDecimal("0.00"));251 setBdNegativeFloat(new BigDecimal("-10.12"));252 setBdNegativeOrZeroFloat(new BigDecimal("-2.16"));253 // biginteger254 setBiPositive(BigInteger.TEN);255 setBiPositiveOrZero(BigInteger.ZERO);256 setBiNegative(BigInteger.valueOf(-10));257 setBiNegativeOrZero(BigInteger.valueOf(-2));258 }};259 p1.setValueBasedOnInstance(bigNumberObj);260 ParamDto dto = p1.getDto();261 assertNotNull(dto.stringValue);262 assertEquals(8, dto.innerContent.size());263 assertEquals("10.12", dto.innerContent.get(0).stringValue);264 assertEquals("-10.12", dto.innerContent.get(1).stringValue);265 assertEquals("0.00", dto.innerContent.get(2).stringValue);266 assertEquals("-2.16", dto.innerContent.get(3).stringValue);267 assertEquals("10", dto.innerContent.get(4).stringValue);268 assertEquals("0", dto.innerContent.get(5).stringValue);269 assertEquals("-10", dto.innerContent.get(6).stringValue);270 assertEquals("-2", dto.innerContent.get(7).stringValue);271 List<String> testScript = p1.newInstanceWithJava(0);272 assertEquals("com.thrift.example.artificial.BigNumberObj arg0 = null;",testScript.get(0));273 assertEquals("{",testScript.get(1));274 assertEquals(" arg0 = new com.thrift.example.artificial.BigNumberObj();",testScript.get(2));275 assertEquals(" java.math.BigDecimal arg0_bdPositiveFloat = null;",testScript.get(3));276 assertEquals(" {",testScript.get(4));277 assertEquals(" java.math.MathContext arg0_bdPositiveFloat_mc = new java.math.MathContext(4);",testScript.get(5));278 assertEquals(" arg0_bdPositiveFloat = new java.math.BigDecimal(\"10.12\", arg0_bdPositiveFloat_mc);",testScript.get(6));279 assertEquals(" arg0_bdPositiveFloat.setScale(2, java.math.RoundingMode.HALF_UP);",testScript.get(7));280 assertEquals(" }",testScript.get(8));281 assertEquals(" arg0.setBdPositiveFloat(arg0_bdPositiveFloat);",testScript.get(9));282 assertEquals(" java.math.BigDecimal arg0_bdNegativeFloat = null;",testScript.get(10));283 assertEquals(" {",testScript.get(11));284 assertEquals(" java.math.MathContext arg0_bdNegativeFloat_mc = new java.math.MathContext(4);",testScript.get(12));285 assertEquals(" arg0_bdNegativeFloat = new java.math.BigDecimal(\"-10.12\", arg0_bdNegativeFloat_mc);",testScript.get(13));286 assertEquals(" arg0_bdNegativeFloat.setScale(2, java.math.RoundingMode.HALF_UP);",testScript.get(14));287 assertEquals(" }",testScript.get(15));288 assertEquals(" arg0.setBdNegativeFloat(arg0_bdNegativeFloat);",testScript.get(16));289 assertEquals(" java.math.BigDecimal arg0_bdPositiveOrZeroFloat = null;",testScript.get(17));290 assertEquals(" {",testScript.get(18));291 assertEquals(" java.math.MathContext arg0_bdPositiveOrZeroFloat_mc = new java.math.MathContext(4);",testScript.get(19));292 assertEquals(" arg0_bdPositiveOrZeroFloat = new java.math.BigDecimal(\"0.00\", arg0_bdPositiveOrZeroFloat_mc);",testScript.get(20));293 assertEquals(" arg0_bdPositiveOrZeroFloat.setScale(2, java.math.RoundingMode.HALF_UP);",testScript.get(21));294 assertEquals(" }",testScript.get(22));295 assertEquals(" arg0.setBdPositiveOrZeroFloat(arg0_bdPositiveOrZeroFloat);",testScript.get(23));296 assertEquals(" java.math.BigDecimal arg0_bdNegativeOrZeroFloat = null;",testScript.get(24));297 assertEquals(" {",testScript.get(25));298 assertEquals(" java.math.MathContext arg0_bdNegativeOrZeroFloat_mc = new java.math.MathContext(4);",testScript.get(26));299 assertEquals(" arg0_bdNegativeOrZeroFloat = new java.math.BigDecimal(\"-2.16\", arg0_bdNegativeOrZeroFloat_mc);",testScript.get(27));300 assertEquals(" arg0_bdNegativeOrZeroFloat.setScale(2, java.math.RoundingMode.HALF_UP);",testScript.get(28));301 assertEquals(" }",testScript.get(29));302 assertEquals(" arg0.setBdNegativeOrZeroFloat(arg0_bdNegativeOrZeroFloat);",testScript.get(30));303 assertEquals(" java.math.BigInteger arg0_biPositive = null;",testScript.get(31));304 assertEquals(" {",testScript.get(32));305 assertEquals(" arg0_biPositive = new java.math.BigInteger(\"10\");",testScript.get(33));306 assertEquals(" }",testScript.get(34));307 assertEquals(" arg0.setBiPositive(arg0_biPositive);",testScript.get(35));308 assertEquals(" java.math.BigInteger arg0_biPositiveOrZero = null;",testScript.get(36));309 assertEquals(" {",testScript.get(37));310 assertEquals(" arg0_biPositiveOrZero = new java.math.BigInteger(\"0\");",testScript.get(38));311 assertEquals(" }",testScript.get(39));312 assertEquals(" arg0.setBiPositiveOrZero(arg0_biPositiveOrZero);",testScript.get(40));313 assertEquals(" java.math.BigInteger arg0_biNegative = null;",testScript.get(41));314 assertEquals(" {",testScript.get(42));315 assertEquals(" arg0_biNegative = new java.math.BigInteger(\"-10\");",testScript.get(43));316 assertEquals(" }",testScript.get(44));317 assertEquals(" arg0.setBiNegative(arg0_biNegative);",testScript.get(45));318 assertEquals(" java.math.BigInteger arg0_biNegativeOrZero = null;",testScript.get(46));319 assertEquals(" {",testScript.get(47));320 assertEquals(" arg0_biNegativeOrZero = new java.math.BigInteger(\"-2\");",testScript.get(48));321 assertEquals(" }",testScript.get(49));322 assertEquals(" arg0.setBiNegativeOrZero(arg0_biNegativeOrZero);",testScript.get(50));323 assertEquals("}",testScript.get(51));324 List<String> assertionScript = p1.newAssertionWithJava("arg0", 0);325 assertEquals("assertEquals(\"10.12\", arg0.getBdPositiveFloat().toString());", assertionScript.get(0));326 assertEquals("assertEquals(\"-10.12\", arg0.getBdNegativeFloat().toString());", assertionScript.get(1));327 assertEquals("assertEquals(\"0.00\", arg0.getBdPositiveOrZeroFloat().toString());", assertionScript.get(2));328 assertEquals("assertEquals(\"-2.16\", arg0.getBdNegativeOrZeroFloat().toString());", assertionScript.get(3));329 assertEquals("assertEquals(\"10\", arg0.getBiPositive().toString());", assertionScript.get(4));330 assertEquals("assertEquals(\"0\", arg0.getBiPositiveOrZero().toString());", assertionScript.get(5));331 assertEquals("assertEquals(\"-10\", arg0.getBiNegative().toString());", assertionScript.get(6));332 assertEquals("assertEquals(\"-2\", arg0.getBiNegativeOrZero().toString());", assertionScript.get(7));333 }334 @Test335 public void testEnumWithConstructor(){336 EndpointSchema endpoint = getOneEndpoint("handleEnumWithConstructor");337 assertNotNull(endpoint.getResponse());338 assertNotNull(endpoint.getRequestParams());339 assertEquals(1, endpoint.getRequestParams().size());340 NamedTypedValue p1 = endpoint.getRequestParams().get(0);341 assertTrue(p1 instanceof ObjectParam);342 ObjectEnum objectEnum = new ObjectEnum(){{343 enumWithConstructor = EnumWithConstructor.FIRST;344 }};345 p1.setValueBasedOnInstance(objectEnum);346 List<String> testScript = p1.newInstanceWithJava(0);347 assertEquals(5, testScript.size());348 assertEquals("com.thrift.example.artificial.ObjectEnum arg0 = null;", testScript.get(0));349 assertEquals("{", testScript.get(1));350 assertEquals(" arg0 = new com.thrift.example.artificial.ObjectEnum();", testScript.get(2));351 assertEquals(" arg0.enumWithConstructor = com.thrift.example.artificial.EnumWithConstructor.FIRST;", testScript.get(3));352 assertEquals("}", testScript.get(4));353 }354 @Test355 public void testNestedGeneric() throws ClassNotFoundException {356 EndpointSchema endpoint = getOneEndpoint("handleNestedGenericString");357 assertNotNull(endpoint.getResponse());358 assertNotNull(endpoint.getRequestParams());359 assertEquals(1, endpoint.getRequestParams().size());360 NamedTypedValue p1 = endpoint.getRequestParams().get(0);361 assertTrue(p1 instanceof ObjectParam);362 assertEquals(3, ((ObjectParam)p1).getType().getFields().size());363 assertNull(((ObjectParam)p1).getValue());364 Object p1Instance = p1.newInstance();365 assertNull(p1Instance);366 List<String> testScript = p1.newInstanceWithJava(0);367 ParamDto dto = p1.getDto();368 dto.innerContent = null;369 p1.setValueBasedOnDto(dto);370 List<String> testScriptWithDto = p1.newInstanceWithJava(0);371 assertEquals(testScript, testScriptWithDto);372 }373 @Test374 public void testLocalAuthSetup(){375 EndpointSchema endpoint = getOneEndpoint("authorizedEndpoint");376 assertTrue(endpoint.getRequestParams() == null || endpoint.getRequestParams().isEmpty());377 assertNotNull(endpoint.getResponse());378 RPCActionDto dto = endpoint.getDto();379 }380 @Test381 public void testEndpointsLoad() {382 assertEquals(expectedNumberOfEndpoints(), schema.getEndpoints().size());383 }384 @Test385 public void testAuthValueSetup() throws ClassNotFoundException {386 assertNotNull(schema.getAuthEndpoints());387 assertEquals(2, schema.getAuthEndpoints().size());388 for (Map.Entry<Integer, EndpointSchema> entry : schema.getAuthEndpoints().entrySet()){389 EndpointSchema endpointSchema = entry.getValue();390 assertEquals(1, endpointSchema.getRequestParams().size());391 NamedTypedValue p1 = endpointSchema.getRequestParams().get(0);392 assertTrue(p1 instanceof ObjectParam);393 Object p1Instance = p1.newInstance();394 assertTrue(p1Instance instanceof AuthLoginDto);395 if (entry.getKey().equals(0)){396 assertEquals("foo", ((AuthLoginDto) p1Instance).id);397 assertEquals("zXQV47zsrjfJRnTD", ((AuthLoginDto) p1Instance).passcode);398 }else if (entry.getKey().equals(1)){399 assertEquals("bar", ((AuthLoginDto) p1Instance).id);400 assertEquals("5jbNvXvaejDG5MhS", ((AuthLoginDto) p1Instance).passcode);401 }402 }403 }404 @Test405 public void testHandleCustomizedRequestA() {406 EndpointSchema endpoint = getOneEndpoint("handleCustomizedRequestA");407 assertNotNull(endpoint.getRelatedCustomizedCandidates());408 assertEquals(2, endpoint.getRelatedCustomizedCandidates().size());409 assertTrue(endpoint.getRelatedCustomizedCandidates().containsAll(Arrays.asList("0", "1")));410 NamedTypedValue p1 = endpoint.getRequestParams().get(0);411 assertTrue(p1 instanceof ObjectParam);412 assertTrue(p1.isNullable());413 assertEquals(3, ((ObjectParam) p1).getType().getFields().size());414 for (NamedTypedValue f : ((ObjectParam) p1).getType().getFields()) {415 if (f.getName().equals("value")) {416 assertTrue(f instanceof IntParam);417 assertNull(((IntParam) f).getMin());418 assertNull(((IntParam) f).getMax());419 } else if (f.getName().equals("id")) {420 assertTrue(f instanceof StringParam);421 assertNotNull(f.getCandidateReferences());422 assertNotNull(f.getCandidates());423 assertEquals(2, f.getCandidates().size());424 assertEquals(2, f.getCandidateReferences().size());425 IntStream.range(0, 1).forEach(i -> {426 assertTrue(f.getCandidates().get(i) instanceof StringParam);427 String value = ((StringParam) f.getCandidates().get(i)).getValue();428 if (f.getCandidateReferences().get(i).equals("0")) {429 assertEquals("foo", value);430 } else if (f.getCandidateReferences().get(i).equals("1")) {431 assertEquals("bar", value);432 }433 });434 } else if (f.getName().equals("passcode")) {435 assertTrue(f instanceof StringParam);436 assertNotNull(f.getCandidateReferences());437 assertNotNull(f.getCandidates());438 assertEquals(2, f.getCandidates().size());439 assertEquals(2, f.getCandidateReferences().size());440 IntStream.range(0, 1).forEach(i -> {441 assertTrue(f.getCandidates().get(i) instanceof StringParam);442 String value = ((StringParam) f.getCandidates().get(i)).getValue();443 if (f.getCandidateReferences().get(i).equals("0")) {444 assertEquals("foo_passcode", value);445 } else if (f.getCandidateReferences().get(i).equals("1")) {446 assertEquals("bar_passcode", value);447 }448 });449 } else450 fail("do not handle param " + f.getName());451 }452 }453 @Test454 public void testHandleCustomizedRequestB() {455 EndpointSchema endpoint = getOneEndpoint("handleCustomizedRequestB");456 assertNotNull(endpoint.getRelatedCustomizedCandidates());457 assertEquals(0, endpoint.getRelatedCustomizedCandidates().size());458 NamedTypedValue p1 = endpoint.getRequestParams().get(0);459 assertTrue(p1 instanceof ObjectParam);460 assertTrue(p1.isNullable());461 assertEquals(1, ((ObjectParam) p1).getType().getFields().size());462 NamedTypedValue f = ((ObjectParam) p1).getType().getFields().get(0);463 assertNull(f.getCandidateReferences());464 assertNotNull(f.getCandidates());465 assertEquals(3, f.getCandidates().size());466 List<Double> candidates = Arrays.asList(0.42, 42.42, 100.42);467 IntStream.range(0, 2).forEach(i -> {468 assertTrue(f.getCandidates().get(i) instanceof DoubleParam);469 Double value = ((DoubleParam) f.getCandidates().get(i)).getValue();470 assertTrue(candidates.contains(value));471 });472 }473 @Test474 public void testConstraintInputs() {475 EndpointSchema endpoint = getOneEndpoint("constraintInputs");476 assertNotNull(endpoint.getResponse());477 assertEquals(2, endpoint.getRequestParams().size());478 NamedTypedValue p1 = endpoint.getRequestParams().get(0);479 checkConstrainedRequest(p1);480 checkConstrainedRequest(p1.copyStructureWithProperties());481 NamedTypedValue p2 = endpoint.getRequestParams().get(1);482 assertTrue(p2 instanceof StringParam);483 assertFalse(p2.isNullable());484 ConstrainedRequest input = new ConstrainedRequest();485 p1.setValueBasedOnInstance(input);486 ParamDto dto = p1.getDto();487 dto.innerContent.get(1).stringValue = null;488 dto.innerContent.get(2).stringValue = null;489 p1.setValueBasedOnDto(dto);490 List<String> javaCode = p1.newInstanceWithJava(0);491 assertEquals(13, javaCode.size());492 assertEquals("com.thrift.example.artificial.ConstrainedRequest arg0 = null;", javaCode.get(0));493 assertEquals("{", javaCode.get(1));494 assertEquals(" arg0 = new com.thrift.example.artificial.ConstrainedRequest();", javaCode.get(2));495 assertEquals(" arg0.list = null;", javaCode.get(3));496 assertEquals(" arg0.notBlankString = null;", javaCode.get(4));497 assertEquals(" arg0.nullableString = null;", javaCode.get(5));498 assertEquals(" arg0.stringSize = null;", javaCode.get(6));499 assertEquals(" arg0.listSize = null;", javaCode.get(7));500 assertEquals(" arg0.kind = null;", javaCode.get(8));501 assertEquals(" arg0.date = null;", javaCode.get(9));502 assertEquals(" arg0.longWithDecimalMinMax = 0L;", javaCode.get(10));503 assertEquals(" arg0.longWithInclusiveFDecimalMainMax = null;", javaCode.get(11));504 assertEquals("}", javaCode.get(12));505 }506 private void checkConstrainedRequest(NamedTypedValue p){507 assertTrue(p instanceof ObjectParam);508 assertTrue(p.isNullable());509 assertEquals(11, ((ObjectParam) p).getType().getFields().size());510 for (NamedTypedValue f : ((ObjectParam) p).getType().getFields()) {511 if (f.getName().equals("list")) {512 assertTrue(f instanceof ListParam);513 assertFalse(f.isNullable());514 assertEquals(1, ((ListParam) f).getMinSize());515 } else if (f.getName().equals("listSize")) {516 assertTrue(f instanceof ListParam);517 assertEquals(1, ((ListParam) f).getMinSize());518 assertEquals(10, ((ListParam) f).getMaxSize());519 assertFalse(f.isNullable());520 } else if (f.getName().equals("intWithMinMax")) {521 assertTrue(f instanceof IntParam);522 assertEquals(0, ((IntParam) f).getMin().intValue());523 assertEquals(100, ((IntParam) f).getMax().intValue());524 assertFalse(f.isNullable());525 } else if (f.getName().equals("longWithMinMax")) {526 assertTrue(f instanceof LongParam);527 assertEquals(-100L, ((LongParam) f).getMin());528 assertEquals(1000L, ((LongParam) f).getMax());529 assertFalse(f.isNullable());530 } else if (f.getName().equals("notBlankString")) {531 assertTrue(f instanceof StringParam);532 assertFalse(f.isNullable());533 assertEquals(1, ((StringParam) f).getMinSize());534 } else if (f.getName().equals("nullableString")) {535 assertTrue(f instanceof StringParam);536 assertTrue(f.isNullable());537 } else if (f.getName().equals("stringSize")) {538 assertTrue(f instanceof StringParam);539 assertEquals(2, ((StringParam) f).getMinSize());540 assertEquals(10, ((StringParam) f).getMaxSize());541 assertTrue(f.isNullable());542 } else if(f.getName().equals("kind")){543 assertTrue(f instanceof EnumParam);544 assertFalse(f.isNullable());545 } else if(f.getName().equals("date")){546 assertTrue(f instanceof StringParam);547 assertTrue(f.isNullable());548 assertNotNull(((StringParam) f).getPattern());549 }else if(f.getName().equals("longWithDecimalMinMax")){550 assertTrue(f instanceof LongParam);551 assertEquals(1L, ((LongParam) f).getMin());552 assertEquals(10L, ((LongParam) f).getMax());553 assertFalse(f.isNullable());554 }else if(f.getName().equals("longWithInclusiveFDecimalMainMax")){555 assertTrue(f instanceof LongParam);556 assertEquals(1L, ((LongParam) f).getMin());557 assertFalse(((LongParam) f).getMinInclusive());558 assertEquals(10L, ((LongParam) f).getMax());559 assertFalse(((LongParam) f).getMaxInclusive());560 assertTrue(f.isNullable());561 }else562 fail("do not handle param " + f.getName());563 }564 }565 @Test566 public void testAccessFieldDtoCheck(){567 EndpointSchema endpoint = getOneEndpoint("accessFieldDtoCheck");568 assertNotNull(endpoint.getResponse());569 assertEquals(1, endpoint.getRequestParams().size());570 NamedTypedValue p1 = endpoint.getRequestParams().get(0);571 assertTrue(p1 instanceof ObjectParam);572 assertTrue(p1.getType() instanceof ObjectType);573 List<NamedTypedValue> fs = ((ObjectType) p1.getType()).getFields();574 assertEquals(13, fs.size());575 for (NamedTypedValue f: fs){576 if (f.getName().equals("pubField")){577 assertTrue(f.accessibleSchema.isAccessible);578 }else if (f.getName().equals("priField")){579 assertFalse(f.accessibleSchema.isAccessible);580 assertEquals("setPriField", f.accessibleSchema.setterMethodName);581 assertEquals("getPriField", f.accessibleSchema.getterMethodName);582 } else if (f.getName().equals("stringList")){583 assertFalse(f.accessibleSchema.isAccessible);584 assertEquals("setStringList", f.accessibleSchema.setterMethodName);585 assertEquals("getStringList", f.accessibleSchema.getterMethodName);586 } else if (f.getName().equals("priEnum")){587 assertFalse(f.accessibleSchema.isAccessible);588 assertEquals("setPriEnum", f.accessibleSchema.setterMethodName);589 assertEquals("getPriEnum", f.accessibleSchema.getterMethodName);590 } else if (f.getName().equals("priBoolean")){591 assertFalse(f.accessibleSchema.isAccessible);592 assertEquals("setPriBoolean", f.accessibleSchema.setterMethodName);593 assertEquals("getPriBoolean", f.accessibleSchema.getterMethodName);594 } else if (f.getName().equals("pribool")){595 assertFalse(f.accessibleSchema.isAccessible);596 assertEquals("setPribool", f.accessibleSchema.setterMethodName);597 assertEquals("isPribool", f.accessibleSchema.getterMethodName);598 } else if (f.getName().equals("pribyte")){599 assertFalse(f.accessibleSchema.isAccessible);600 assertEquals("setPribyte", f.accessibleSchema.setterMethodName);601 assertEquals("getPribyte", f.accessibleSchema.getterMethodName);602 } else if (f.getName().equals("priBByte")){603 assertFalse(f.accessibleSchema.isAccessible);604 assertEquals("setPriBByte", f.accessibleSchema.setterMethodName);605 assertEquals("getPriBByte", f.accessibleSchema.getterMethodName);606 } else if (f.getName().equals("priCharacter")){607 assertFalse(f.accessibleSchema.isAccessible);608 assertEquals("setPriCharacter", f.accessibleSchema.setterMethodName);609 assertEquals("getPriCharacter", f.accessibleSchema.getterMethodName);610 } else if (f.getName().equals("priChar")){611 assertFalse(f.accessibleSchema.isAccessible);612 assertEquals("setPriChar", f.accessibleSchema.setterMethodName);613 assertEquals("getPriChar", f.accessibleSchema.getterMethodName);614 } else if (f.getName().equals("priSShort")){615 assertFalse(f.accessibleSchema.isAccessible);616 assertEquals("setPriSShort", f.accessibleSchema.setterMethodName);617 assertEquals("getPriSShort", f.accessibleSchema.getterMethodName);618 } else if (f.getName().equals("priShot")){619 assertFalse(f.accessibleSchema.isAccessible);620 assertEquals("setPriShot", f.accessibleSchema.setterMethodName);621 assertEquals("getPriShot", f.accessibleSchema.getterMethodName);622 } else if (f.getName().equals("priMap")){623 assertFalse(f.accessibleSchema.isAccessible);624 assertEquals("setPriMap", f.accessibleSchema.setterMethodName);625 assertEquals("getPriMap", f.accessibleSchema.getterMethodName);626 }627 }628 PrivateFieldInRequestDto p1Instance = new PrivateFieldInRequestDto(){{629 pubField = "foo";630 setPriField("bar");631 setStringList(Arrays.asList("1","2","3"));632 setPriEnum(EnumKind.ONE);633 setPriBoolean(true);634 setPriBByte((byte) 15);635 setPribyte((byte) 5);636 setPriChar('0');637 setPriCharacter('a');638 setPriShort((short) 2);639 setPriSShort((short) 42);640 setPriMap(new HashMap<String, String>(){{641 put("foo","foo");642 put("bar", "bar");643 }});644 }};645 p1.setValueBasedOnInstance(p1Instance);646 List<String> javaCodes = p1.newInstanceWithJava(0);647 assertEquals(37, javaCodes.size());648 assertEquals("com.thrift.example.artificial.PrivateFieldInRequestDto arg0 = null;", javaCodes.get(0));649 assertEquals("{", javaCodes.get(1));650 assertEquals(" arg0 = new com.thrift.example.artificial.PrivateFieldInRequestDto();", javaCodes.get(2));651 assertEquals(" arg0.pubField = \"foo\";", javaCodes.get(3));652 assertEquals(" arg0.setPriField(\"bar\");", javaCodes.get(4));653 assertEquals(" java.util.List<java.lang.String> arg0_stringList = null;", javaCodes.get(5));654 assertEquals(" {", javaCodes.get(6));655 assertEquals(" arg0_stringList = new java.util.ArrayList<>();", javaCodes.get(7));656 assertEquals(" java.lang.String arg0_stringList_e_0 = \"1\";", javaCodes.get(8));657 assertEquals(" arg0_stringList.add(arg0_stringList_e_0);", javaCodes.get(9));658 assertEquals(" java.lang.String arg0_stringList_e_1 = \"2\";", javaCodes.get(10));659 assertEquals(" arg0_stringList.add(arg0_stringList_e_1);", javaCodes.get(11));660 assertEquals(" java.lang.String arg0_stringList_e_2 = \"3\";", javaCodes.get(12));661 assertEquals(" arg0_stringList.add(arg0_stringList_e_2);", javaCodes.get(13));662 assertEquals(" }", javaCodes.get(14));663 assertEquals(" arg0.setStringList(arg0_stringList);", javaCodes.get(15));664 assertEquals(" arg0.setPriEnum(((com.thrift.example.artificial.EnumKind)(com.thrift.example.artificial.EnumKind.ONE)));", javaCodes.get(16));665 assertEquals(" arg0.setPriBoolean(true);", javaCodes.get(17));666 assertEquals(" arg0.setPribool(false);", javaCodes.get(18));667 assertEquals(" arg0.setPriBByte(((byte)(15)));", javaCodes.get(19));668 assertEquals(" arg0.setPribyte(((byte)(5)));", javaCodes.get(20));669 assertEquals(" arg0.setPriCharacter('\\u0061');", javaCodes.get(21));670 assertEquals(" arg0.setPriChar('\\u0030');", javaCodes.get(22));671 assertEquals(" arg0.setPriShort(((short)(2)));", javaCodes.get(23));672 assertEquals(" arg0.setPriSShort(((short)(42)));", javaCodes.get(24));673 assertEquals(" java.util.Map<java.lang.String,java.lang.String> arg0_priMap = null;", javaCodes.get(25));674 assertEquals(" {", javaCodes.get(26));675 assertEquals(" arg0_priMap = new java.util.HashMap<>();", javaCodes.get(27));676 assertEquals(" java.lang.String arg0_priMap_key_0 = \"bar\";", javaCodes.get(28));677 assertEquals(" java.lang.String arg0_priMap_value_0 = \"bar\";", javaCodes.get(29));678 assertEquals(" arg0_priMap.put(arg0_priMap_key_0,arg0_priMap_value_0);", javaCodes.get(30));679 assertEquals(" java.lang.String arg0_priMap_key_1 = \"foo\";", javaCodes.get(31));680 assertEquals(" java.lang.String arg0_priMap_value_1 = \"foo\";", javaCodes.get(32));681 assertEquals(" arg0_priMap.put(arg0_priMap_key_1,arg0_priMap_value_1);", javaCodes.get(33));682 assertEquals(" }", javaCodes.get(34));683 assertEquals(" arg0.setPriMap(arg0_priMap);", javaCodes.get(35));684 assertEquals("}", javaCodes.get(36));685 List<String> assertionJavaCode = p1.newAssertionWithJava(0, "res1", -1);686 assertEquals(18, assertionJavaCode.size());687 assertEquals("assertEquals(\"foo\", res1.pubField);", assertionJavaCode.get(0));688 assertEquals("assertEquals(\"bar\", res1.getPriField());", assertionJavaCode.get(1));689 assertEquals("assertEquals(3, res1.getStringList().size());", assertionJavaCode.get(2));690 assertEquals("assertEquals(\"1\", res1.getStringList().get(0));", assertionJavaCode.get(3));691 assertEquals("assertEquals(\"2\", res1.getStringList().get(1));", assertionJavaCode.get(4));692 assertEquals("assertEquals(\"3\", res1.getStringList().get(2));", assertionJavaCode.get(5));693 assertEquals("assertEquals(com.thrift.example.artificial.EnumKind.ONE, res1.getPriEnum());", assertionJavaCode.get(6));694 assertEquals("assertEquals(true, res1.getPriBoolean().booleanValue());", assertionJavaCode.get(7));695 assertEquals("assertEquals(false, res1.isPribool());", assertionJavaCode.get(8));696 assertEquals("assertEquals(15, res1.getPriBByte().byteValue());", assertionJavaCode.get(9));697 assertEquals("assertEquals(5, res1.getPribyte());", assertionJavaCode.get(10));698 assertEquals("assertEquals('\\u0061', res1.getPriCharacter().charValue());", assertionJavaCode.get(11));699 assertEquals("assertEquals('\\u0030', res1.getPriChar());", assertionJavaCode.get(12));700 assertEquals("assertEquals(2, res1.getPriShort());", assertionJavaCode.get(13));701 assertEquals("assertEquals(42, res1.getPriSShort().shortValue());", assertionJavaCode.get(14));702 assertEquals("assertEquals(2, res1.getPriMap().size());", assertionJavaCode.get(15));703 assertEquals("assertEquals(\"bar\", res1.getPriMap().get(\"bar\"));", assertionJavaCode.get(16));704 assertEquals("assertEquals(\"foo\", res1.getPriMap().get(\"foo\"));", assertionJavaCode.get(17));705 NamedTypedValue res = endpoint.getResponse();706 PrivateFieldInResponseDto resInstance = new PrivateFieldInResponseDto(){{707 pubField = 42;708 setPriRequest(p1Instance);709 }};710 res.setValueBasedOnInstance(resInstance);711 List<String> javaCodesForResponse = res.newInstanceWithJava(true, true, "tmp", 0);712 assertEquals(43, javaCodesForResponse.size());713 assertEquals("com.thrift.example.artificial.PrivateFieldInResponseDto tmp = null;", javaCodesForResponse.get(0));714 assertEquals("{", javaCodesForResponse.get(1));715 assertEquals(" tmp = new com.thrift.example.artificial.PrivateFieldInResponseDto();", javaCodesForResponse.get(2));716 assertEquals(" tmp.pubField = 42;", javaCodesForResponse.get(3));717 assertEquals(" com.thrift.example.artificial.PrivateFieldInRequestDto tmp_priRequest = null;", javaCodesForResponse.get(4));718 assertEquals(" {", javaCodesForResponse.get(5));719 assertEquals(" tmp_priRequest = new com.thrift.example.artificial.PrivateFieldInRequestDto();", javaCodesForResponse.get(6));720 assertEquals(" tmp_priRequest.pubField = \"foo\";", javaCodesForResponse.get(7));721 assertEquals(" tmp_priRequest.setPriField(\"bar\");", javaCodesForResponse.get(8));722 assertEquals(" java.util.List<java.lang.String> tmp_priRequest_stringList = null;", javaCodesForResponse.get(9));723 assertEquals(" {", javaCodesForResponse.get(10));724 assertEquals(" tmp_priRequest_stringList = new java.util.ArrayList<>();", javaCodesForResponse.get(11));725 assertEquals(" java.lang.String tmp_priRequest_stringList_e_0 = \"1\";", javaCodesForResponse.get(12));726 assertEquals(" tmp_priRequest_stringList.add(tmp_priRequest_stringList_e_0);", javaCodesForResponse.get(13));727 assertEquals(" java.lang.String tmp_priRequest_stringList_e_1 = \"2\";", javaCodesForResponse.get(14));728 assertEquals(" tmp_priRequest_stringList.add(tmp_priRequest_stringList_e_1);", javaCodesForResponse.get(15));729 assertEquals(" java.lang.String tmp_priRequest_stringList_e_2 = \"3\";", javaCodesForResponse.get(16));730 assertEquals(" tmp_priRequest_stringList.add(tmp_priRequest_stringList_e_2);", javaCodesForResponse.get(17));731 assertEquals(" }", javaCodesForResponse.get(18));732 assertEquals(" tmp_priRequest.setStringList(tmp_priRequest_stringList);", javaCodesForResponse.get(19));733 assertEquals(" tmp_priRequest.setPriEnum(((com.thrift.example.artificial.EnumKind)(com.thrift.example.artificial.EnumKind.ONE)));", javaCodesForResponse.get(20));734 assertEquals(" tmp_priRequest.setPriBoolean(true);", javaCodesForResponse.get(21));735 assertEquals(" tmp_priRequest.setPribool(false);", javaCodesForResponse.get(22));736 assertEquals(" tmp_priRequest.setPriBByte(((byte)(15)));", javaCodesForResponse.get(23));737 assertEquals(" tmp_priRequest.setPribyte(((byte)(5)));", javaCodesForResponse.get(24));738 assertEquals(" tmp_priRequest.setPriCharacter('\\u0061');", javaCodesForResponse.get(25));739 assertEquals(" tmp_priRequest.setPriChar('\\u0030');", javaCodesForResponse.get(26));740 assertEquals(" tmp_priRequest.setPriShort(((short)(2)));", javaCodesForResponse.get(27));741 assertEquals(" tmp_priRequest.setPriSShort(((short)(42)));", javaCodesForResponse.get(28));742 assertEquals(" java.util.Map<java.lang.String,java.lang.String> tmp_priRequest_priMap = null;", javaCodesForResponse.get(29));743 assertEquals(" {", javaCodesForResponse.get(30));744 assertEquals(" tmp_priRequest_priMap = new java.util.HashMap<>();", javaCodesForResponse.get(31));745 assertEquals(" java.lang.String tmp_priRequest_priMap_key_0 = \"bar\";", javaCodesForResponse.get(32));746 assertEquals(" java.lang.String tmp_priRequest_priMap_value_0 = \"bar\";", javaCodesForResponse.get(33));747 assertEquals(" tmp_priRequest_priMap.put(tmp_priRequest_priMap_key_0,tmp_priRequest_priMap_value_0);", javaCodesForResponse.get(34));748 assertEquals(" java.lang.String tmp_priRequest_priMap_key_1 = \"foo\";", javaCodesForResponse.get(35));749 assertEquals(" java.lang.String tmp_priRequest_priMap_value_1 = \"foo\";", javaCodesForResponse.get(36));750 assertEquals(" tmp_priRequest_priMap.put(tmp_priRequest_priMap_key_1,tmp_priRequest_priMap_value_1);", javaCodesForResponse.get(37));751 assertEquals(" }", javaCodesForResponse.get(38));752 assertEquals(" tmp_priRequest.setPriMap(tmp_priRequest_priMap);", javaCodesForResponse.get(39));753 assertEquals(" }", javaCodesForResponse.get(40));754 assertEquals(" tmp.setPriRequest(tmp_priRequest);", javaCodesForResponse.get(41));755 assertEquals("}", javaCodesForResponse.get(42));756 List<String> assertionJavaCodeForResponse = res.newAssertionWithJava(0, "res1", -1);757 assertEquals(19, assertionJavaCodeForResponse.size());758 assertEquals("assertEquals(42, res1.pubField);", assertionJavaCodeForResponse.get(0));759 assertEquals("assertEquals(\"foo\", res1.getPriRequest().pubField);", assertionJavaCodeForResponse.get(1));760 assertEquals("assertEquals(\"bar\", res1.getPriRequest().getPriField());", assertionJavaCodeForResponse.get(2));761 assertEquals("assertEquals(3, res1.getPriRequest().getStringList().size());", assertionJavaCodeForResponse.get(3));762 assertEquals("assertEquals(\"1\", res1.getPriRequest().getStringList().get(0));", assertionJavaCodeForResponse.get(4));763 assertEquals("assertEquals(\"2\", res1.getPriRequest().getStringList().get(1));", assertionJavaCodeForResponse.get(5));764 assertEquals("assertEquals(\"3\", res1.getPriRequest().getStringList().get(2));", assertionJavaCodeForResponse.get(6));765 assertEquals("assertEquals(com.thrift.example.artificial.EnumKind.ONE, res1.getPriRequest().getPriEnum());", assertionJavaCodeForResponse.get(7));766 assertEquals("assertEquals(true, res1.getPriRequest().getPriBoolean().booleanValue());", assertionJavaCodeForResponse.get(8));767 assertEquals("assertEquals(false, res1.getPriRequest().isPribool());", assertionJavaCodeForResponse.get(9));768 assertEquals("assertEquals(15, res1.getPriRequest().getPriBByte().byteValue());", assertionJavaCodeForResponse.get(10));769 assertEquals("assertEquals(5, res1.getPriRequest().getPribyte());", assertionJavaCodeForResponse.get(11));770 assertEquals("assertEquals('\\u0061', res1.getPriRequest().getPriCharacter().charValue());", assertionJavaCodeForResponse.get(12));771 assertEquals("assertEquals('\\u0030', res1.getPriRequest().getPriChar());", assertionJavaCodeForResponse.get(13));772 assertEquals("assertEquals(2, res1.getPriRequest().getPriShort());", assertionJavaCodeForResponse.get(14));773 assertEquals("assertEquals(42, res1.getPriRequest().getPriSShort().shortValue());", assertionJavaCodeForResponse.get(15));774 assertEquals("assertEquals(2, res1.getPriRequest().getPriMap().size());", assertionJavaCodeForResponse.get(16));775 assertEquals("assertEquals(\"bar\", res1.getPriRequest().getPriMap().get(\"bar\"));", assertionJavaCodeForResponse.get(17));776 assertEquals("assertEquals(\"foo\", res1.getPriRequest().getPriMap().get(\"foo\"));", assertionJavaCodeForResponse.get(18));777 }778 @Test779 public void testAccessFieldDtoWithNullCheck(){780 EndpointSchema endpoint = getOneEndpoint("accessFieldDtoCheck");781 assertNotNull(endpoint.getResponse());782 assertEquals(1, endpoint.getRequestParams().size());783 NamedTypedValue p1 = endpoint.getRequestParams().get(0);784 assertTrue(p1 instanceof ObjectParam);785 PrivateFieldInRequestDto p1Instance = new PrivateFieldInRequestDto();786 p1.setValueBasedOnInstance(p1Instance);787 List<String> javaCodes = p1.newInstanceWithJava(0);788 assertEquals(19, javaCodes.size());789 assertEquals("com.thrift.example.artificial.PrivateFieldInRequestDto arg0 = null;", javaCodes.get(0));790 assertEquals("{", javaCodes.get(1));791 assertEquals(" arg0 = new com.thrift.example.artificial.PrivateFieldInRequestDto();", javaCodes.get(2));792 assertEquals(" arg0.pubField = null;", javaCodes.get(3));793 assertEquals(" arg0.setPriField(null);", javaCodes.get(4));794 assertEquals(" java.util.List<java.lang.String> arg0_stringList = null;", javaCodes.get(5));795 assertEquals(" arg0.setStringList(arg0_stringList);", javaCodes.get(6));796 assertEquals(" arg0.setPriEnum(null);", javaCodes.get(7));797 assertEquals(" arg0.setPriBoolean(null);", javaCodes.get(8));798 assertEquals(" arg0.setPribool(false);", javaCodes.get(9));799 assertEquals(" arg0.setPriBByte(null);", javaCodes.get(10));800 assertEquals(" arg0.setPribyte(((byte)(0)));", javaCodes.get(11));801 assertEquals(" arg0.setPriCharacter(null);", javaCodes.get(12));802 assertEquals(" arg0.setPriChar('\\u0000');", javaCodes.get(13));803 assertEquals(" arg0.setPriShort(((short)(0)));", javaCodes.get(14));804 assertEquals(" arg0.setPriSShort(null);", javaCodes.get(15));805 assertEquals(" java.util.Map<java.lang.String,java.lang.String> arg0_priMap = null;", javaCodes.get(16));806 assertEquals(" arg0.setPriMap(arg0_priMap);", javaCodes.get(17));807 assertEquals("}", javaCodes.get(18));808 List<String> assertionJavaCode = p1.newAssertionWithJava(0, "res1", -1);809 assertEquals(13, assertionJavaCode.size());810 assertEquals("assertNull(res1.pubField);", assertionJavaCode.get(0));811 assertEquals("assertNull(res1.getPriField());", assertionJavaCode.get(1));812 assertEquals("assertNull(res1.getStringList());", assertionJavaCode.get(2));813 assertEquals("assertNull(res1.getPriEnum());", assertionJavaCode.get(3));814 assertEquals("assertNull(res1.getPriBoolean());", assertionJavaCode.get(4));815 assertEquals("assertEquals(false, res1.isPribool());", assertionJavaCode.get(5));816 assertEquals("assertNull(res1.getPriBByte());", assertionJavaCode.get(6));817 assertEquals("assertEquals(0, res1.getPribyte());", assertionJavaCode.get(7));818 assertEquals("assertNull(res1.getPriCharacter());", assertionJavaCode.get(8));819 assertEquals("assertEquals('\\u0000', res1.getPriChar());", assertionJavaCode.get(9));820 assertEquals("assertEquals(0, res1.getPriShort());", assertionJavaCode.get(10));821 assertEquals("assertNull(res1.getPriSShort());", assertionJavaCode.get(11));822 assertEquals("assertNull(res1.getPriMap());", assertionJavaCode.get(12));823 NamedTypedValue res = endpoint.getResponse();824 PrivateFieldInResponseDto resInstance = new PrivateFieldInResponseDto(){{825 pubField = 42;826 }};827 res.setValueBasedOnInstance(resInstance);828 List<String> javaCodesForResponse = res.newInstanceWithJava(true, true, "tmp", 0);829 assertEquals(7, javaCodesForResponse.size());830 assertEquals("com.thrift.example.artificial.PrivateFieldInResponseDto tmp = null;", javaCodesForResponse.get(0));831 assertEquals("{", javaCodesForResponse.get(1));832 assertEquals(" tmp = new com.thrift.example.artificial.PrivateFieldInResponseDto();", javaCodesForResponse.get(2));833 assertEquals(" tmp.pubField = 42;", javaCodesForResponse.get(3));834 assertEquals(" com.thrift.example.artificial.PrivateFieldInRequestDto tmp_priRequest = null;", javaCodesForResponse.get(4));835 assertEquals(" tmp.setPriRequest(tmp_priRequest);", javaCodesForResponse.get(5));836 assertEquals("}", javaCodesForResponse.get(6));837 List<String> assertionJavaCodeForResponse = res.newAssertionWithJava(0, "res1", -1);838 assertEquals(2, assertionJavaCodeForResponse.size());839 assertEquals("assertEquals(42, res1.pubField);", assertionJavaCodeForResponse.get(0));840 assertEquals("assertNull(res1.getPriRequest());", assertionJavaCodeForResponse.get(1));841 PrivateFieldInResponseDto resInstance2 = new PrivateFieldInResponseDto(){{842 pubField = 42;843 setPriRequest(new PrivateFieldInRequestDto());844 }};845 res.setValueBasedOnInstance(resInstance2);846 javaCodesForResponse = res.newInstanceWithJava(true, true, "tmp", 0);847 assertEquals(25, javaCodesForResponse.size());848 assertEquals("com.thrift.example.artificial.PrivateFieldInResponseDto tmp = null;", javaCodesForResponse.get(0));849 assertEquals("{", javaCodesForResponse.get(1));850 assertEquals(" tmp = new com.thrift.example.artificial.PrivateFieldInResponseDto();", javaCodesForResponse.get(2));851 assertEquals(" tmp.pubField = 42;", javaCodesForResponse.get(3));852 assertEquals(" com.thrift.example.artificial.PrivateFieldInRequestDto tmp_priRequest = null;", javaCodesForResponse.get(4));853 assertEquals(" {", javaCodesForResponse.get(5));854 assertEquals(" tmp_priRequest = new com.thrift.example.artificial.PrivateFieldInRequestDto();", javaCodesForResponse.get(6));855 assertEquals(" tmp_priRequest.pubField = null;", javaCodesForResponse.get(7));856 assertEquals(" tmp_priRequest.setPriField(null);", javaCodesForResponse.get(8));857 assertEquals(" java.util.List<java.lang.String> tmp_priRequest_stringList = null;", javaCodesForResponse.get(9));858 assertEquals(" tmp_priRequest.setStringList(tmp_priRequest_stringList);", javaCodesForResponse.get(10));859 assertEquals(" tmp_priRequest.setPriEnum(null);", javaCodesForResponse.get(11));860 assertEquals(" tmp_priRequest.setPriBoolean(null);", javaCodesForResponse.get(12));861 assertEquals(" tmp_priRequest.setPribool(false);", javaCodesForResponse.get(13));862 assertEquals(" tmp_priRequest.setPriBByte(null);", javaCodesForResponse.get(14));863 assertEquals(" tmp_priRequest.setPribyte(((byte)(0)));", javaCodesForResponse.get(15));864 assertEquals(" tmp_priRequest.setPriCharacter(null);", javaCodesForResponse.get(16));865 assertEquals(" tmp_priRequest.setPriChar('\\u0000');", javaCodesForResponse.get(17));866 assertEquals(" tmp_priRequest.setPriShort(((short)(0)));", javaCodesForResponse.get(18));867 assertEquals(" tmp_priRequest.setPriSShort(null);", javaCodesForResponse.get(19));868 assertEquals(" java.util.Map<java.lang.String,java.lang.String> tmp_priRequest_priMap = null;", javaCodesForResponse.get(20));869 assertEquals(" tmp_priRequest.setPriMap(tmp_priRequest_priMap);", javaCodesForResponse.get(21));870 assertEquals(" }", javaCodesForResponse.get(22));871 assertEquals(" tmp.setPriRequest(tmp_priRequest);", javaCodesForResponse.get(23));872 assertEquals("}", javaCodesForResponse.get(24));873 assertionJavaCodeForResponse = res.newAssertionWithJava(0, "res1", -1);874 assertEquals(14, assertionJavaCodeForResponse.size());875 assertEquals("assertEquals(42, res1.pubField);", assertionJavaCodeForResponse.get(0));876 assertEquals("assertNull(res1.getPriRequest().pubField);", assertionJavaCodeForResponse.get(1));877 assertEquals("assertNull(res1.getPriRequest().getPriField());", assertionJavaCodeForResponse.get(2));878 assertEquals("assertNull(res1.getPriRequest().getStringList());", assertionJavaCodeForResponse.get(3));879 assertEquals("assertNull(res1.getPriRequest().getPriEnum());", assertionJavaCodeForResponse.get(4));880 assertEquals("assertNull(res1.getPriRequest().getPriBoolean());", assertionJavaCodeForResponse.get(5));881 assertEquals("assertEquals(false, res1.getPriRequest().isPribool());", assertionJavaCodeForResponse.get(6));882 assertEquals("assertNull(res1.getPriRequest().getPriBByte());", assertionJavaCodeForResponse.get(7));883 assertEquals("assertEquals(0, res1.getPriRequest().getPribyte());", assertionJavaCodeForResponse.get(8));884 assertEquals("assertNull(res1.getPriRequest().getPriCharacter());", assertionJavaCodeForResponse.get(9));885 assertEquals("assertEquals('\\u0000', res1.getPriRequest().getPriChar());", assertionJavaCodeForResponse.get(10));886 assertEquals("assertEquals(0, res1.getPriRequest().getPriShort());", assertionJavaCodeForResponse.get(11));887 assertEquals("assertNull(res1.getPriRequest().getPriSShort());", assertionJavaCodeForResponse.get(12));888 assertEquals("assertNull(res1.getPriRequest().getPriMap());", assertionJavaCodeForResponse.get(13));889 }890 @Test891 public void testDateToString() throws ClassNotFoundException, ParseException {892 EndpointSchema endpoint = getOneEndpoint("dateToString");893 assertNotNull(endpoint.getResponse());894 assertEquals(1, endpoint.getRequestParams().size());895 NamedTypedValue p1 = endpoint.getRequestParams().get(0);896 assertTrue(p1 instanceof DateParam);897 //String stringDate = "2021-12-14 19:45:23.722 +0100";898 String stringDate = "2021-12-14 19:45:23";899 Date date = DateType.SIMPLE_DATE_FORMATTER.parse(stringDate);900 long time = date.getTime();901 p1.setValueBasedOnInstance(date);902 Object instance = p1.newInstance();903 assertTrue(instance instanceof Date);904 assertEquals(time, ((Date) instance).getTime());905 ParamDto dto = p1.getDto();906 assertEquals(6, dto.innerContent.size());907 assertEquals("2021", dto.innerContent.get(0).stringValue);908 assertEquals("12", dto.innerContent.get(1).stringValue);909 assertEquals("14", dto.innerContent.get(2).stringValue);910 assertEquals("19", dto.innerContent.get(3).stringValue);911 assertEquals("45", dto.innerContent.get(4).stringValue);912 assertEquals("23", dto.innerContent.get(5).stringValue);913// assertEquals("722", dto.innerContent.get(6).jsonValue);914// assertEquals("100", dto.innerContent.get(7).jsonValue);915 List<String> javacode = p1.newInstanceWithJava(0);916 assertEquals(5, javacode.size());917 assertEquals("java.util.Date arg0 = null;", javacode.get(0));918 assertEquals("{", javacode.get(1));919 assertEquals(" // Date is " + stringDate, javacode.get(2));920 assertEquals(" arg0 = new java.util.Date(" + time + "L);", javacode.get(3));921 assertEquals("}", javacode.get(4));922 List<String> assertions = p1.newAssertionWithJava(0, "res1", -1);923 assertEquals(1, assertions.size());924 assertTrue(assertions.get(0).contains("// runtime value is "));925 }926 @Test927 public void testSimplePrimitive() {928 EndpointSchema endpoint = getOneEndpoint("simplePrimitive");929 assertNotNull(endpoint.getResponse());930 assertEquals(8, endpoint.getRequestParams().size());931 assertTrue(endpoint.getRequestParams().get(0) instanceof IntParam);932 assertTrue(endpoint.getRequestParams().get(1) instanceof FloatParam);933 assertTrue(endpoint.getRequestParams().get(2) instanceof LongParam);934 assertTrue(endpoint.getRequestParams().get(3) instanceof DoubleParam);935 assertTrue(endpoint.getRequestParams().get(4) instanceof CharParam);936 assertTrue(endpoint.getRequestParams().get(5) instanceof ByteParam);937 assertTrue(endpoint.getRequestParams().get(6) instanceof BooleanParam);938 assertTrue(endpoint.getRequestParams().get(7) instanceof ShortParam);939 }940 @Test941 public void testSimplePrimitiveToFromDTO() throws ClassNotFoundException {942 EndpointSchema endpoint = getOneEndpoint("simplePrimitive");943 RPCActionDto dto = endpoint.getDto().copy();944 assertEquals(8, dto.requestParams.size());945 dto.requestParams.get(0).stringValue = "" + 42;946 dto.requestParams.get(1).stringValue = "" + 4.2f;947 dto.requestParams.get(2).stringValue = "" + 42L;948 dto.requestParams.get(3).stringValue = "" + 4.2;949 dto.requestParams.get(4).stringValue = "" + 'x';950 dto.requestParams.get(5).stringValue = "" + Byte.parseByte("42");951 dto.requestParams.get(6).stringValue = "" + false;952 dto.requestParams.get(7).stringValue = "" + Short.parseShort("42");953 endpoint.setValue(dto);954 assertEquals(42, endpoint.getRequestParams().get(0).newInstance());955 assertEquals(4.2f, endpoint.getRequestParams().get(1).newInstance());956 assertEquals(42L, endpoint.getRequestParams().get(2).newInstance());957 assertEquals(4.2, endpoint.getRequestParams().get(3).newInstance());958 assertEquals('x', endpoint.getRequestParams().get(4).newInstance());959 assertEquals(Byte.parseByte("42"), endpoint.getRequestParams().get(5).newInstance());960 assertEquals(false, endpoint.getRequestParams().get(6).newInstance());961 assertEquals(Short.parseShort("42"), endpoint.getRequestParams().get(7).newInstance());962 }963 @Test964 public void testSimpleWrapPrimitive() {965 EndpointSchema endpoint = getOneEndpoint("simpleWrapPrimitive");966 assertNotNull(endpoint.getResponse());967 assertEquals(8, endpoint.getRequestParams().size());968 assertTrue(endpoint.getRequestParams().get(0) instanceof IntParam);969 assertTrue(endpoint.getRequestParams().get(1) instanceof FloatParam);970 assertTrue(endpoint.getRequestParams().get(2) instanceof LongParam);971 assertTrue(endpoint.getRequestParams().get(3) instanceof DoubleParam);972 assertTrue(endpoint.getRequestParams().get(4) instanceof CharParam);973 assertTrue(endpoint.getRequestParams().get(5) instanceof ByteParam);974 assertTrue(endpoint.getRequestParams().get(6) instanceof BooleanParam);975 assertTrue(endpoint.getRequestParams().get(7) instanceof ShortParam);976 }977 @Test978 public void testSimpleWrapPrimitiveToFromDTO() throws ClassNotFoundException {979 EndpointSchema endpoint = getOneEndpoint("simpleWrapPrimitive");980 RPCActionDto dto = endpoint.getDto().copy();981 assertEquals(8, dto.requestParams.size());982 dto.requestParams.get(0).stringValue = "" + 42;983 dto.requestParams.get(1).stringValue = "" + 4.2f;984 dto.requestParams.get(2).stringValue = "" + 42L;985 dto.requestParams.get(3).stringValue = "" + 4.2;986 dto.requestParams.get(4).stringValue = "" + 'x';987 dto.requestParams.get(5).stringValue = "" + Byte.parseByte("42");988 dto.requestParams.get(6).stringValue = "" + false;989 dto.requestParams.get(7).stringValue = "" + Short.parseShort("42");990 endpoint.setValue(dto);991 assertEquals(42, endpoint.getRequestParams().get(0).newInstance());992 assertEquals(4.2f, endpoint.getRequestParams().get(1).newInstance());993 assertEquals(42L, endpoint.getRequestParams().get(2).newInstance());994 assertEquals(4.2, endpoint.getRequestParams().get(3).newInstance());995 assertEquals('x', endpoint.getRequestParams().get(4).newInstance());996 assertEquals(Byte.parseByte("42"), endpoint.getRequestParams().get(5).newInstance());997 assertEquals(false, endpoint.getRequestParams().get(6).newInstance());998 assertEquals(Short.parseShort("42"), endpoint.getRequestParams().get(7).newInstance());999 }1000 @Test1001 public void testArray() {1002 EndpointSchema endpoint = getOneEndpoint("array");1003 assertNotNull(endpoint.getResponse());1004 assertEquals(1, endpoint.getRequestParams().size());1005 NamedTypedValue param = endpoint.getRequestParams().get(0);1006 assertTrue(param instanceof ArrayParam);1007 assertTrue(param.getType() instanceof CollectionType);1008 NamedTypedValue template = ((CollectionType) param.getType()).getTemplate();1009 assertTrue(template instanceof ListParam);1010 assertTrue(template.getType() instanceof CollectionType);1011 assertTrue(((CollectionType) template.getType()).getTemplate() instanceof StringParam);1012 }1013 @Test1014 public void testArrayToFromDto() throws ClassNotFoundException {1015 EndpointSchema endpoint = getOneEndpoint("array");1016 RPCActionDto dto = endpoint.getDto();1017 assertEquals(1, dto.requestParams.size());1018 ParamDto paramDto = dto.requestParams.get(0);1019 assertEquals(RPCSupportedDataType.ARRAY, paramDto.type.type);1020 assertNotNull(paramDto.type.example);1021 ParamDto paramExampleDto = paramDto.type.example;1022 assertEquals(RPCSupportedDataType.LIST, paramExampleDto.type.type);1023 assertNotNull(paramExampleDto.type.example);1024 ParamDto paramExampleExampleDto = paramExampleDto.type.example;1025 assertEquals(RPCSupportedDataType.STRING, paramExampleExampleDto.type.type);1026 List<ParamDto> strs = IntStream.range(0, 3).mapToObj(i -> {1027 ParamDto p = paramExampleExampleDto.copy();1028 p.stringValue = "str_" + i;1029 return p;1030 }).collect(Collectors.toList());1031 ParamDto iList = paramExampleDto.copy();1032 iList.innerContent = strs;1033 paramDto.innerContent = Arrays.asList(iList);1034 endpoint.setValue(dto);1035 }1036 @Test1037 public void testArrayBoolean() {1038 EndpointSchema endpoint = getOneEndpoint("arrayboolean");1039 assertNotNull(endpoint.getResponse());1040 assertEquals(1, endpoint.getRequestParams().size());1041 NamedTypedValue param = endpoint.getRequestParams().get(0);1042 assertTrue(param instanceof ArrayParam);1043 assertTrue(param.getType() instanceof CollectionType);1044 NamedTypedValue template = ((CollectionType) param.getType()).getTemplate();1045 assertTrue(template instanceof BooleanParam);1046 }1047 @Test1048 public void testList() {1049 EndpointSchema endpoint = getOneEndpoint("list");1050 assertNotNull(endpoint.getResponse());1051 assertEquals(1, endpoint.getRequestParams().size());1052 NamedTypedValue param = endpoint.getRequestParams().get(0);1053 assertTrue(param instanceof ListParam);1054 assertTrue(param.getType() instanceof CollectionType);1055 NamedTypedValue template = ((CollectionType) param.getType()).getTemplate();1056 assertTrue(template instanceof StringParam);1057 }1058 @Test1059 public void testMap() {1060 EndpointSchema endpoint = getOneEndpoint("map");1061 assertNotNull(endpoint.getResponse());1062 assertEquals(1, endpoint.getRequestParams().size());1063 NamedTypedValue param = endpoint.getRequestParams().get(0);1064 assertTrue(param instanceof MapParam);1065 assertTrue(param.getType() instanceof MapType);1066 NamedTypedValue pairTemplate = ((MapType) param.getType()).getTemplate();1067 assertTrue(pairTemplate instanceof PairParam);1068 NamedTypedValue ktemplate = ((PairType) pairTemplate.getType()).getFirstTemplate();1069 assertTrue(ktemplate instanceof StringParam);1070 NamedTypedValue vtemplate = ((PairType) pairTemplate.getType()).getSecondTemplate();1071 assertTrue(vtemplate instanceof StringParam);1072 }1073 @Test1074 public void testListAndMap() {1075 EndpointSchema endpoint = getOneEndpoint("listAndMap");1076 assertNotNull(endpoint.getResponse());1077 assertEquals(1, endpoint.getRequestParams().size());1078 NamedTypedValue param = endpoint.getRequestParams().get(0);1079 assertTrue(param instanceof ListParam);1080 assertTrue(param.getType() instanceof CollectionType);1081 NamedTypedValue mapTemplate = ((CollectionType) param.getType()).getTemplate();1082 assertTrue(mapTemplate instanceof MapParam);1083 assertTrue(mapTemplate.getType() instanceof MapType);1084 NamedTypedValue pairTemplate = ((MapType) mapTemplate.getType()).getTemplate();1085 assertTrue(pairTemplate instanceof PairParam);1086 NamedTypedValue ktemplate = ((PairType) pairTemplate.getType()).getFirstTemplate();1087 assertTrue(ktemplate instanceof StringParam);1088 NamedTypedValue vtemplate = ((PairType) pairTemplate.getType()).getSecondTemplate();1089 assertTrue(vtemplate instanceof StringParam);1090 }1091 @Test1092 public void testObjResponse() {1093 EndpointSchema endpoint = getOneEndpoint("objResponse");1094 assertEquals(0, endpoint.getRequestParams().size());1095 assertNotNull(endpoint.getResponse());1096 NamedTypedValue param = endpoint.getResponse();1097 assertTrue(param instanceof ObjectParam);1098 assertTrue(param.getType() instanceof ObjectType);1099 List<NamedTypedValue> fs = ((ObjectType) param.getType()).getFields();1100 assertEquals(7, fs.size());1101 assertTrue(fs.get(0) instanceof StringParam);1102 assertTrue(fs.get(1) instanceof IntParam);1103 assertTrue(fs.get(2) instanceof DoubleParam);1104 assertTrue(fs.get(3) instanceof ObjectParam);1105 assertTrue(fs.get(4) instanceof ArrayParam);1106 assertTrue(fs.get(5) instanceof ArrayParam);1107 assertTrue(fs.get(6) instanceof LongParam);1108 assertTrue(fs.get(3).getType() instanceof CycleObjectType);1109 }1110 @Test1111 public void testObjCycleA() {1112 EndpointSchema endpoint = getOneEndpoint("objCycleA");1113 assertEquals(0, endpoint.getRequestParams().size());1114 assertNotNull(endpoint.getResponse());1115 NamedTypedValue param = endpoint.getResponse();1116 assertTrue(param instanceof ObjectParam);1117 assertTrue(param.getType() instanceof ObjectType);1118 List<NamedTypedValue> fs = ((ObjectType) param.getType()).getFields();1119 assertEquals(1, fs.size());1120 assertTrue(fs.get(0) instanceof ObjectParam);1121 assertEquals(1, ((ObjectParam) fs.get(0)).getType().getFields().size());1122 assertTrue(((ObjectParam) fs.get(0)).getType().getFields().get(0).getType() instanceof CycleObjectType);1123 }1124 @Test1125 public void testObjCycleB() {1126 EndpointSchema endpoint = getOneEndpoint("objCycleB");1127 assertEquals(0, endpoint.getRequestParams().size());1128 assertNotNull(endpoint.getResponse());1129 NamedTypedValue param = endpoint.getResponse();1130 assertTrue(param instanceof ObjectParam);1131 assertTrue(param.getType() instanceof ObjectType);1132 List<NamedTypedValue> fs = ((ObjectType) param.getType()).getFields();1133 assertEquals(1, fs.size());1134 assertTrue(fs.get(0) instanceof ObjectParam);1135 assertEquals(1, ((ObjectParam) fs.get(0)).getType().getFields().size());1136 assertTrue(((ObjectParam) fs.get(0)).getType().getFields().get(0).getType() instanceof CycleObjectType);1137 }1138}...
Source:ObjectParam.java
...17import java.util.stream.Collectors;18/**19 * object param20 */21public class ObjectParam extends NamedTypedValue<ObjectType, List<NamedTypedValue>> {22 public ObjectParam(String name, ObjectType type, AccessibleSchema accessibleSchema) {23 super(name, type, accessibleSchema);24 }25 @Override26 public Object newInstance() throws ClassNotFoundException {27 if (getValue() == null) return null;28 String clazzName = getType().getFullTypeName();29 Class<?> clazz = Class.forName(clazzName);30 try {31 Object instance = clazz.newInstance();32 for (NamedTypedValue v: getValue()){33 if (v.accessibleSchema == null || v.accessibleSchema.isAccessible){34 Field f = clazz.getField(v.getName());35 f.setAccessible(true);36 Object vins = v.newInstance();37 if (vins != null)38 f.set(instance, vins);39 } else if(v.accessibleSchema.setterMethodName != null){40 Method m = getSetter(clazz, v.accessibleSchema.setterMethodName, v.getType(), v.getType().getClazz(), 0);41 //clazz.getMethod(v.accessibleSchema.setterMethodName, v.getType().getClazz());42 m.invoke(instance, v.newInstance());43 }44 }45 return instance;46 } catch (InstantiationException e) {47 throw new RuntimeException("fail to construct the class:"+clazzName+" with error msg:"+e.getMessage());48 } catch (IllegalAccessException e) {49 throw new RuntimeException("fail to access the class:"+clazzName+" with error msg:"+e.getMessage());50 } catch (NoSuchFieldException e) {51 throw new RuntimeException("fail to access the field:"+clazzName+" with error msg:"+e.getMessage());52 } catch (NoSuchMethodException e) {53 throw new RuntimeException("fail to access the method:"+clazzName+" with error msg:"+e.getMessage());54 } catch (InvocationTargetException e) {55 throw new RuntimeException("fail to invoke the setter method:"+clazzName+" with error msg:"+e.getMessage());56 }57 }58 private Method getSetter(Class<?> clazz, String setterName, TypeSchema type, Class<?> typeClass, int attemptTimes) throws NoSuchMethodException {59 try {60 Method m = clazz.getMethod(setterName, type.getClazz());61 return m;62 } catch (NoSuchMethodException e) {63 if (type instanceof PrimitiveOrWrapperType && attemptTimes == 0){64 Type p = PrimitiveOrWrapperParam.getPrimitiveOrWrapper(type.getClazz());65 if (p instanceof Class){66 return getSetter(clazz, setterName, type, (Class)p, 1);67 }68 }69 throw e;70 }71 }72 @Override73 public ObjectParam copyStructure() {74 return new ObjectParam(getName(), getType(), accessibleSchema);75 }76 @Override77 public ParamDto getDto() {78 ParamDto dto = super.getDto();79 if (getValue() != null){80 dto.innerContent = getValue().stream().map(NamedTypedValue::getDto).collect(Collectors.toList());81 dto.stringValue = NOT_NULL_MARK_OBJ_DATE;82 } else83 dto.innerContent = getType().getFields().stream().map(NamedTypedValue::getDto).collect(Collectors.toList());84 return dto;85 }86 @Override87 public void setValueBasedOnDto(ParamDto dto) {88 if (dto.innerContent!=null && !dto.innerContent.isEmpty()){89 List<NamedTypedValue> fields = getType().getFields();90 List<NamedTypedValue> values = new ArrayList<>();91 for (ParamDto p: dto.innerContent){92 NamedTypedValue f = fields.stream().filter(s-> s.sameParam(p)).findFirst().get().copyStructureWithProperties();93 f.setValueBasedOnDto(p);94 values.add(f);95 }96 setValue(values);97 }98 }99 @Override100 protected void setValueBasedOnValidInstance(Object instance) {101 List<NamedTypedValue> values = new ArrayList<>();102 List<NamedTypedValue> fields = getType().getFields();103 Class<?> clazz;104 try {105 clazz = Class.forName(getType().getFullTypeName());106 } catch (ClassNotFoundException e) {107 throw new RuntimeException("ERROR: fail to get class with the name"+getType().getFullTypeName()+" Msg:"+e.getMessage());108 }109 for (NamedTypedValue f: fields){110 NamedTypedValue copy = f.copyStructureWithProperties();111 try {112 if (f.accessibleSchema == null || f.accessibleSchema.isAccessible){113 Field fi = clazz.getField(f.getName());114 fi.setAccessible(true);115 Object fiv = fi.get(instance);116 copy.setValueBasedOnInstance(fiv);117 } else if(f.accessibleSchema.getterMethodName != null){118 Method m = clazz.getMethod(f.accessibleSchema.getterMethodName);119 copy.setValueBasedOnInstance(m.invoke(instance));120 }121 } catch (NoSuchFieldException | IllegalAccessException e) {122 throw new RuntimeException("ERROR: fail to get value of the field with the name ("+ f.getName()+ ") and error Msg:"+e.getMessage());123 } catch (NoSuchMethodException | InvocationTargetException e) {124 throw new RuntimeException("ERROR: fail to get/invoke getter method for the field with the name ("+ f.getName()+ ") and error Msg:"+e.getMessage());125 }126 values.add(copy);127 }128 setValue(values);129 }130 @Override131 public void setValueBasedOnInstanceOrJson(Object json) throws JsonProcessingException {132 List<NamedTypedValue> values = new ArrayList<>();133 List<NamedTypedValue> fields = getType().getFields();134 if (isValidInstance(json)){135 setValueBasedOnInstance(json);136 }else {137 /*138 in jackson, object would be extracted as a map139 */140 assert json instanceof Map;141 for (NamedTypedValue f: fields){142 NamedTypedValue copy = f.copyStructureWithProperties();143 Object fiv = ((Map)json).get(f.getName());144 copy.setValueBasedOnInstanceOrJson(fiv);145 values.add(copy);146 }147 setValue(values);148 }149 }150 @Override151 public List<String> newInstanceWithJava(boolean isDeclaration, boolean doesIncludeName, String variableName, int indent) {152 String typeName = getType().getTypeNameForInstance();153 String varName = variableName;154 List<String> codes = new ArrayList<>();155 boolean isNull = (getValue() == null);156 String var = CodeJavaGenerator.oneLineInstance(isDeclaration, doesIncludeName, typeName, varName, null);157 CodeJavaGenerator.addCode(codes, var, indent);158 if (isNull) return codes;159 CodeJavaGenerator.addCode(codes, "{", indent);160 // new obj161 CodeJavaGenerator.addCode(codes, CodeJavaGenerator.setInstanceObject(typeName, varName), indent+1);162 for (NamedTypedValue f : getValue()){163 if (f.accessibleSchema == null || f.accessibleSchema.isAccessible){164 String fName = varName+"."+f.getName();165 codes.addAll(f.newInstanceWithJava(false, true, fName, indent+1));166 }else{167 String fName = varName;168 boolean fdeclar = false;169 if (f instanceof ObjectParam || f instanceof MapParam || f instanceof CollectionParam || f instanceof DateParam || f instanceof BigDecimalParam || f instanceof BigIntegerParam){170 fName = varName+"_"+f.getName();171 fdeclar = true;172 }173 codes.addAll(f.newInstanceWithJava(fdeclar, true, fName, indent+1));174 if (f instanceof ObjectParam || f instanceof MapParam || f instanceof CollectionParam || f instanceof DateParam || f instanceof BigDecimalParam || f instanceof BigIntegerParam){175 CodeJavaGenerator.addCode(codes, CodeJavaGenerator.methodInvocation(varName, f.accessibleSchema.setterMethodName, fName)+CodeJavaGenerator.appendLast(),indent+1);176 }177 }178 }179 CodeJavaGenerator.addCode(codes, "}", indent);180 return codes;181 }182 @Override183 public List<String> newAssertionWithJava(int indent, String responseVarName, int maxAssertionForDataInCollection) {184 List<String> codes = new ArrayList<>();185 if (getValue() == null){186 CodeJavaGenerator.addCode(codes, CodeJavaGenerator.junitAssertNull(responseVarName), indent);187 return codes;188 }...
ObjectParam
Using AI Code Generation
1package org.evomaster.client.java.controller.problem.rpc.schema.params;2import com.google.gson.JsonElement;3import com.google.gson.JsonNull;4import com.google.gson.JsonObject;5import com.google.gson.JsonParser;6import org.evomaster.client.java.controller.problem.ProblemInfo;7import org.evomaster.client.java.controller.problem.rpc.RpcCallResult;8import org.evomaster.client.java.controller.problem.rpc.RpcCallResultInfo;9import org.evomaster.client.java.controller.problem.rpc.RpcCallResultInfoExtractor;10import org.evomaster.client.java.controller.problem.rpc.RpcCallStatus;11import org.evomaster.client.java.controller.problem.rpc.schema.RpcSchema;12import org.evomaster.client.java.controller.problem.rpc.schema.RpcSchemaInfo;13import org.evomaster.client.java.controller.problem.rpc.schema.RpcSchemaInfoExtractor;14import org.evomaster.client.java.controller.problem.rpc.schema.RpcSchemaType;15import org.evomaster.client.java.controller.problem.rpc.schema.RpcType;16import org.evomaster.client.java.controller.problem.rpc.schema.RpcTypeExtractor;17import org.evomaster.client.java.controller.problem.rpc.schema.RpcTypeReference;18import org.evomaster.client.java.controller.problem.rest.ResourceCallResult;19import org.evomaster.client.java.controller.problem.rest.RestCallResult;20import org.evomaster.client.java.controller.problem.rest.RestCallResultInfo;21import org.evomaster.client.java.controller.problem.rest.RestCallResultInfoExtractor;22import org.evomaster.client.java.controller.problem.rest.RestCallStatus;23import org.evomaster.client.java.controller.problem.rest.schema.JsonSchema;24import org.evomaster.client.java.controller.problem.rest.schema.JsonSchemaInfo;25import org.evomaster.client.java.controller.problem.rest.schema.JsonSchemaInfoExtractor;26import org.evomaster.client.java.controller.problem.rest.schema.JsonSchemaType;27import org.evomaster.client.java.controller.problem.rest.schema.JsonType;28import org.evomaster.client.java.controller.problem.rest.schema.JsonTypeExtractor;29import org.evomaster.client.java.controller.problem.rest.schema.JsonTypeReference;30import org.evomaster.client.java.utils.SimpleLogger;31import org.evomaster.client.java.utils.SimpleParamNames;32import java.util.ArrayList;33import java.util.List;34import java.util.Map;35import java.util.Optional;36import java.util.stream.Collectors;37public class ObjectParam extends RpcParam {38 private final RpcSchema schema;39 public ObjectParam(String name, RpcSchema schema) {40 super(name, RpcParamType.OBJECT);41 this.schema = schema;
ObjectParam
Using AI Code Generation
1package org.evomaster.client.java.controller.problem.rpc.schema.params;2import com.fasterxml.jackson.annotation.JsonProperty;3import java.util.List;4public class ObjectParam {5 @JsonProperty(value = "name", required = true)6 private String name;7 @JsonProperty(value = "type", required = true)8 private String type;9 @JsonProperty(value = "value", required = true)10 private Object value;11 @JsonProperty(value = "fields", required = true)12 private List<ObjectParam> fields;13 public ObjectParam() {14 }15 public ObjectParam(String name, String type, Object value, List<ObjectParam> fields) {16 this.name = name;17 this.type = type;18 this.value = value;19 this.fields = fields;20 }21 public String getName() {22 return name;23 }24 public void setName(String name) {25 this.name = name;26 }27 public String getType() {28 return type;29 }30 public void setType(String type) {31 this.type = type;32 }33 public Object getValue() {34 return value;35 }36 public void setValue(Object value) {37 this.value = value;38 }39 public List<ObjectParam> getFields() {40 return fields;41 }42 public void setFields(List<ObjectParam> fields) {43 this.fields = fields;44 }45 public String toString() {46 return "ObjectParam{" +47 '}';48 }49}50package org.evomaster.client.java.controller.problem.rpc.schema.params;51import com.fasterxml.jackson.annotation.JsonProperty;52import java.util.List;53public class ObjectParam {54 @JsonProperty(value = "name", required = true)55 private String name;56 @JsonProperty(value = "type", required = true)57 private String type;58 @JsonProperty(value = "value", required = true)59 private Object value;60 @JsonProperty(value = "fields", required = true)61 private List<ObjectParam> fields;62 public ObjectParam() {63 }64 public ObjectParam(String name, String type, Object value, List<ObjectParam> fields) {65 this.name = name;
ObjectParam
Using AI Code Generation
1public class 2 extends ObjectParam {2 public 2() {3 super("2");4 }5 public 2(2 other) {6 super(other);7 }8 public 2 copy() {9 return new 2(this);10 }11}12public class 3 extends ArrayParam {13 public 3() {14 super("3");15 }16 public 3(3 other) {17 super(other);18 }19 public 3 copy() {20 return new 3(this);21 }22}23public class 4 extends ObjectParam {24 public 4() {25 super("4");26 }27 public 4(4 other) {28 super(other);29 }30 public 4 copy() {31 return new 4(this);32 }33}34public class 5 extends ObjectParam {35 public 5() {36 super("5");37 }38 public 5(5 other) {39 super(other);40 }41 public 5 copy() {42 return new 5(this);43 }44}45public class 6 extends ObjectParam {46 public 6() {47 super("6");48 }49 public 6(6 other) {50 super(other);51 }52 public 6 copy() {53 return new 6(this);54 }55}56public class 7 extends ObjectParam {57 public 7() {58 super("7");59 }60 public 7(7 other) {61 super(other);62 }63 public 7 copy() {64 return new 7(this);65 }66}
ObjectParam
Using AI Code Generation
1ObjectParam objectParam = new ObjectParam();2objectParam.addParam("name",new StringParam("name"));3objectParam.addParam("age",new IntegerParam(1));4objectParam.addParam("address",new StringParam("address"));5objectParam.addParam("email",new StringParam("email"));6objectParam.addParam("phone",new StringParam("phone"));7objectParam.addParam("password",new StringParam("password"));8objectParam.addParam("image",new StringParam("image"));9objectParam.addParam("imageContentType",new StringParam("imageContentType"));10objectParam.addParam("activated",new BooleanParam(true));11objectParam.addParam("langKey",new StringParam("langKey"));12objectParam.addParam("createdBy",new StringParam("createdBy"));13objectParam.addParam("createdDate",new StringParam("createdDate"));14objectParam.addParam("lastModifiedBy",new StringParam("lastModifiedBy"));15objectParam.addParam("lastModifiedDate",new StringParam("lastModifiedDate"));16objectParam.addParam("authorities",new ArrayParam());17objectParam.addParam("login",new StringParam("login"));18objectParam.addParam("id",new IntegerParam(1));19objectParam.addParam("resetDate",new StringParam("resetDate"));20objectParam.addParam("resetKey",new StringParam("resetKey"));21objectParam.addParam("activated",new BooleanParam(true));22objectParam.addParam("activationKey",new StringParam("activationKey"));23objectParam.addParam("resetKey",new StringParam("resetKey"));24objectParam.addParam("resetDate",new StringParam("resetDate"));25objectParam.addParam("login",new StringParam("login"));26objectParam.addParam("id",new IntegerParam(1));27objectParam.addParam("authorities",new ArrayParam());28objectParam.addParam("firstName",new StringParam("firstName"));29objectParam.addParam("lastName",new StringParam("lastName"));30objectParam.addParam("email",new StringParam("email"));31objectParam.addParam("imageUrl",new StringParam("imageUrl"));32objectParam.addParam("activated",new BooleanParam(true));33objectParam.addParam("langKey",new StringParam("langKey"));34objectParam.addParam("createdBy",new StringParam("createdBy"));35objectParam.addParam("createdDate",new StringParam("createdDate"));36objectParam.addParam("lastModifiedBy",new StringParam("lastModifiedBy"));37objectParam.addParam("
ObjectParam
Using AI Code Generation
1public class 2 {2 public static void main(String[] args) {3 ObjectParam objectParam = new ObjectParam();4 objectParam.put("param1", new StringParam("param1"));5 objectParam.put("param2", new IntegerParam(2));6 objectParam.put("param3", new BooleanParam(true));7 objectParam.put("param4", new DoubleParam(4.0));8 objectParam.put("param5", new LongParam(5L));9 objectParam.put("param6", new FloatParam(6.0f));10 objectParam.put("param7", new ShortParam((short) 7));11 objectParam.put("param8", new ByteParam((byte) 8));12 objectParam.put("param9", new CharacterParam('9'));13 objectParam.put("param10", new EnumParam("param10", "param10"));14 objectParam.put("param11", new ArrayParam(new StringParam("param11_1"), new StringParam("param11_2")));15 objectParam.put("param12", new ListParam(new StringParam("param12_1"), new StringParam("param12_2")));16 objectParam.put("param13", new SetParam(new StringParam("param13_1"), new StringParam("param13_2")));17 objectParam.put("param14", new MapParam(new StringParam("param14_1"), new StringParam("param14_2")));18 objectParam.put("param15", new ObjectParam(new StringParam("param15_1"), new StringParam("param15_2")));19 objectParam.put("param16", new ObjectParam(new StringParam("param16_1"), new StringParam("param16_2")));20 objectParam.put("param17", new ObjectParam(new StringParam("param17_1"), new StringParam("param17_2")));21 ObjectParam objectParam2 = new ObjectParam();22 objectParam2.put("param1", new StringParam("param1"));23 objectParam2.put("param2", new IntegerParam(2));24 objectParam2.put("param3", new BooleanParam(true));25 objectParam2.put("param4", new DoubleParam(4.0));26 objectParam2.put("param5", new LongParam(5L));27 objectParam2.put("param6", new FloatParam(6.0f));28 objectParam2.put("
ObjectParam
Using AI Code Generation
1public class 2 {2 public static void main(String[] args) {3 ObjectParam objectParam0 = new ObjectParam("objectParam0");4 objectParam0.setClassName("class_name");5 objectParam0.setField("field_name");6 objectParam0.setField("field_name", "field_value");7 objectParam0.setField("field_name", 0);8 objectParam0.setField("field_name", 0.0);9 objectParam0.setField("field_name", true);10 objectParam0.setField("field_name", new ObjectParam("objectParam1"));11 objectParam0.setField("field_name", new String[]{"string1", "string2"});12 objectParam0.setField("field_name", new Integer[]{0, 1});13 objectParam0.setField("field_name", new Double[]{0.0, 1.0});14 objectParam0.setField("field_name", new Boolean[]{true, false});15 objectParam0.setField("field_name", new ObjectParam[]{new ObjectParam("objectParam1"), new ObjectParam("objectParam2")});16 objectParam0.setField("field_name", new String[][]{{"string1", "string2"}, {"string3", "string4"}});17 objectParam0.setField("field_name", new Integer[][]{{0, 1}, {2, 3}});18 objectParam0.setField("field_name", new Double[][]{{0.0, 1.0}, {2.0, 3.0}});19 objectParam0.setField("field_name", new Boolean[][]{{true, false}, {false, true}});20 objectParam0.setField("field_name", new ObjectParam[][]{{new ObjectParam("objectParam1"), new ObjectParam("objectParam2")}, {new ObjectParam("objectParam3"), new ObjectParam("objectParam4")}});21 }22}23public class 3 {24 public static void main(String[] args) {25 ArrayParam arrayParam0 = new ArrayParam("arrayParam0");26 arrayParam0.setClassName("class_name");27 arrayParam0.setField("field_name");28 arrayParam0.setField("field_name", "field_value");29 arrayParam0.setField("field_name
ObjectParam
Using AI Code Generation
1public class 2 {2 public static void main(String[] args) throws Exception {3 String id = "1";4 String name = "2";5 String age = "3";6 String address = "4";7 String city = "5";8 String state = "6";9 String zip = "7";10 String phone = "8";11 String email = "9";12 String password = "10";13 String role = "11";14 String status = "12";15 String date = "13";16 String time = "14";17 String amount = "15";18 String type = "16";19 String description = "17";20 String cardNumber = "18";21 String cardType = "19";22 String cardExpiration = "20";23 String cardSecurityCode = "21";24 String cardName = "22";25 String cardAddress = "23";26 String cardCity = "24";27 String cardState = "25";28 String cardZip = "26";29 String cardPhone = "27";30 String cardEmail = "28";31 String cardDate = "29";32 String cardTime = "30";33 String cardAmount = "31";34 String cardType = "32";35 String cardDescription = "33";36 String cardId = "34";37 String cardId = "35";38 String cardId = "36";39 String cardId = "37";40 String cardId = "38";41 String cardId = "39";42 String cardId = "40";43 String cardId = "41";44 String cardId = "42";45 String cardId = "43";46 String cardId = "44";47 String cardId = "45";48 String cardId = "46";49 String cardId = "47";50 String cardId = "48";51 String cardId = "49";52 String cardId = "50";53 String cardId = "51";54 String cardId = "52";55 String cardId = "53";56 String cardId = "54";57 String cardId = "55";58 String cardId = "56";59 String cardId = "57";60 String cardId = "58";61 String cardId = "59";62 String cardId = "60";
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!!