How to use createRandomValueExpression method of com.consol.citrus.generate.javadsl.SwaggerJavaTestGenerator class

Best Citrus code snippet using com.consol.citrus.generate.javadsl.SwaggerJavaTestGenerator.createRandomValueExpression

Source:SwaggerJavaTestGenerator.java Github

copy

Full Screen

...99 if (operation.getValue().getParameters() != null) {100 operation.getValue().getParameters().stream()101 .filter(p -> p instanceof HeaderParameter)102 .filter(Parameter::getRequired)103 .forEach(p -> requestMessage.setHeader(p.getName(), getMode().equals(GeneratorMode.CLIENT) ? createRandomValueExpression(((HeaderParameter) p).getItems(), swagger.getDefinitions(), false) : createValidationExpression(((HeaderParameter) p).getItems(), swagger.getDefinitions(), false)));104 operation.getValue().getParameters().stream()105 .filter(p -> p instanceof PathParameter)106 .filter(Parameter::getRequired)107 .forEach(p -> requestMessage.setHeader(p.getName(), getMode().equals(GeneratorMode.CLIENT) ? createRandomValueExpression((PathParameter) p) : createValidationExpression((PathParameter) p)));108 operation.getValue().getParameters().stream()109 .filter(param -> param instanceof QueryParameter)110 .filter(Parameter::getRequired)111 .forEach(param -> requestMessage.queryParam(param.getName(), getMode().equals(GeneratorMode.CLIENT) ? createRandomValueExpression((QueryParameter) param) : createValidationExpression((QueryParameter) param)));112 operation.getValue().getParameters().stream()113 .filter(p -> p instanceof BodyParameter)114 .filter(Parameter::getRequired)115 .findFirst()116 .ifPresent(p -> requestMessage.setPayload(getMode().equals(GeneratorMode.CLIENT) ? createOutboundPayload(((BodyParameter) p).getSchema(), swagger.getDefinitions()) : createInboundPayload(((BodyParameter) p).getSchema(), swagger.getDefinitions())));117 }118 withRequest(requestMessage);119 HttpMessage responseMessage = new HttpMessage();120 if (operation.getValue().getResponses() != null) {121 Response response = pair.getValue();122 if (response != null) {123 if (code.equalsIgnoreCase("default")) {124 responseMessage.status(HttpStatus.OK);125 }126 else {127 try {128 int status = Integer.parseInt(code);129 responseMessage.status(HttpStatus.valueOf(status));130 } catch (NumberFormatException e) {131 responseMessage.status(HttpStatus.OK);132 log.warn("Cannot create new test case for status code: '" + code + "' Created by default");133 }134 }135 if (response.getHeaders() != null) {136 for (Map.Entry<String, Property> header : response.getHeaders().entrySet()) {137 responseMessage.setHeader(header.getKey(), getMode().equals(GeneratorMode.CLIENT) ? createValidationExpression(header.getValue(), swagger.getDefinitions(), false) : createRandomValueExpression(header.getValue(), swagger.getDefinitions(), false));138 }139 }140 if (response.getSchema() != null) {141 responseMessage.setPayload(getMode().equals(GeneratorMode.CLIENT) ? createInboundPayload(response.getSchema(), swagger.getDefinitions()) : createOutboundPayload(response.getSchema(), swagger.getDefinitions()));142 }143 }144 }145 withResponse(responseMessage);146 super.create();147 log.info("Successfully created new test case " + getTargetPackage() + "." + getName());148 }149 }150 }151 }152 /**153 * Creates payload from schema for outbound message.154 * @param model155 * @param definitions156 * @return157 */158 private String createOutboundPayload(Model model, Map<String, Model> definitions) {159 StringBuilder payload = new StringBuilder();160 if (model instanceof RefModel) {161 model = definitions.get(((RefModel) model).getSimpleRef());162 }163 if (model instanceof ArrayModel) {164 payload.append(createOutboundPayload(((ArrayModel) model).getItems(), definitions));165 } else {166 payload.append("{");167 if (model.getProperties() != null) {168 for (Map.Entry<String, Property> entry : model.getProperties().entrySet()) {169 payload.append("\"").append(entry.getKey()).append("\": ").append(createOutboundPayload(entry.getValue(), definitions)).append(",");170 }171 }172 if (payload.toString().endsWith(",")) {173 payload.replace(payload.length() - 1, payload.length(), "");174 }175 payload.append("}");176 }177 return payload.toString();178 }179 /**180 * Creates payload from property for outbound message.181 * @param property182 * @param definitions183 * @return184 */185 private String createOutboundPayload(Property property, Map<String, Model> definitions) {186 StringBuilder payload = new StringBuilder();187 if (property instanceof RefProperty) {188 Model model = definitions.get(((RefProperty) property).getSimpleRef());189 payload.append("{");190 if (model.getProperties() != null) {191 for (Map.Entry<String, Property> entry : model.getProperties().entrySet()) {192 payload.append("\"").append(entry.getKey()).append("\": ").append(createRandomValueExpression(entry.getValue(), definitions, true)).append(",");193 }194 }195 if (payload.toString().endsWith(",")) {196 payload.replace(payload.length() - 1, payload.length(), "");197 }198 payload.append("}");199 } else if (property instanceof ArrayProperty) {200 payload.append("[");201 payload.append(createRandomValueExpression(((ArrayProperty) property).getItems(), definitions, true));202 payload.append("]");203 } else {204 payload.append(createRandomValueExpression(property, definitions, true));205 }206 return payload.toString();207 }208 /**209 * Create payload from schema with random values.210 * @param property211 * @param definitions212 * @param quotes213 * @return214 */215 private String createRandomValueExpression(Property property, Map<String, Model> definitions, boolean quotes) {216 StringBuilder payload = new StringBuilder();217 if (property instanceof RefProperty) {218 payload.append(createOutboundPayload(property, definitions));219 } else if (property instanceof ArrayProperty) {220 payload.append(createOutboundPayload(property, definitions));221 } else if (property instanceof StringProperty || property instanceof DateProperty || property instanceof DateTimeProperty) {222 if (quotes) {223 payload.append("\"");224 }225 if (property instanceof DateProperty) {226 payload.append("citrus:currentDate()");227 } else if (property instanceof DateTimeProperty) {228 payload.append("citrus:currentDate('yyyy-MM-dd'T'hh:mm:ss')");229 } else if (!CollectionUtils.isEmpty(((StringProperty) property).getEnum())) {230 payload.append("citrus:randomEnumValue(").append(((StringProperty) property).getEnum().stream().map(value -> "'" + value + "'").collect(Collectors.joining(","))).append(")");231 } else if (Optional.ofNullable(property.getFormat()).orElse("").equalsIgnoreCase("uuid")) {232 payload.append("citrus:randomUUID()");233 } else {234 payload.append("citrus:randomString(").append(((StringProperty) property).getMaxLength() != null && ((StringProperty) property).getMaxLength() > 0 ? ((StringProperty) property).getMaxLength() : (((StringProperty) property).getMinLength() != null && ((StringProperty) property).getMinLength() > 0 ? ((StringProperty) property).getMinLength() : 10)).append(")");235 }236 if (quotes) {237 payload.append("\"");238 }239 } else if (property instanceof IntegerProperty || property instanceof LongProperty) {240 payload.append("citrus:randomNumber(10)");241 } else if (property instanceof FloatProperty || property instanceof DoubleProperty) {242 payload.append("citrus:randomNumber(10)");243 } else if (property instanceof BooleanProperty) {244 payload.append("citrus:randomEnumValue('true', 'false')");245 } else {246 if (quotes) {247 payload.append("\"\"");248 } else {249 payload.append("");250 }251 }252 return payload.toString();253 }254 /**255 * Creates control payload from property for validation.256 * @param property257 * @param definitions258 * @return259 */260 private String createInboundPayload(Property property, Map<String, Model> definitions) {261 StringBuilder payload = new StringBuilder();262 if (property instanceof RefProperty) {263 Model model = definitions.get(((RefProperty) property).getSimpleRef());264 payload.append("{");265 if (model.getProperties() != null) {266 for (Map.Entry<String, Property> entry : model.getProperties().entrySet()) {267 payload.append("\"").append(entry.getKey()).append("\": ").append(createValidationExpression(entry.getValue(), definitions, true)).append(",");268 }269 }270 if (payload.toString().endsWith(",")) {271 payload.replace(payload.length() - 1, payload.length(), "");272 }273 payload.append("}");274 } else if (property instanceof ArrayProperty) {275 payload.append("[");276 payload.append(createValidationExpression(((ArrayProperty) property).getItems(), definitions, true));277 payload.append("]");278 } else {279 payload.append(createValidationExpression(property, definitions, false));280 }281 return payload.toString();282 }283 /**284 * Creates control payload from schema for validation.285 * @param model286 * @param definitions287 * @return288 */289 private String createInboundPayload(Model model, Map<String, Model> definitions) {290 StringBuilder payload = new StringBuilder();291 if (model instanceof RefModel) {292 model = definitions.get(((RefModel) model).getSimpleRef());293 }294 if (model instanceof ArrayModel) {295 payload.append("[");296 payload.append(createValidationExpression(((ArrayModel) model).getItems(), definitions, true));297 payload.append("]");298 } else {299 payload.append("{");300 if (model.getProperties() != null) {301 for (Map.Entry<String, Property> entry : model.getProperties().entrySet()) {302 payload.append("\"").append(entry.getKey()).append("\": ").append(createValidationExpression(entry.getValue(), definitions, true)).append(",");303 }304 }305 if (payload.toString().endsWith(",")) {306 payload.replace(payload.length() - 1, payload.length(), "");307 }308 payload.append("}");309 }310 return payload.toString();311 }312 /**313 * Create validation expression using functions according to parameter type and format.314 * @param property315 * @param definitions316 * @param quotes317 * @return318 */319 private String createValidationExpression(Property property, Map<String, Model> definitions, boolean quotes) {320 StringBuilder payload = new StringBuilder();321 if (property instanceof RefProperty) {322 Model model = definitions.get(((RefProperty) property).getSimpleRef());323 payload.append("{");324 if (model.getProperties() != null) {325 for (Map.Entry<String, Property> entry : model.getProperties().entrySet()) {326 payload.append("\"").append(entry.getKey()).append("\": ").append(createValidationExpression(entry.getValue(), definitions, quotes)).append(",");327 }328 }329 if (payload.toString().endsWith(",")) {330 payload.replace(payload.length() - 1, payload.length(), "");331 }332 payload.append("}");333 } else if (property instanceof ArrayProperty) {334 if (quotes) {335 payload.append("\"");336 }337 payload.append("@ignore@");338 if (quotes) {339 payload.append("\"");340 }341 } else if (property instanceof StringProperty) {342 if (quotes) {343 payload.append("\"");344 }345 if (StringUtils.hasText(((StringProperty) property).getPattern())) {346 payload.append("@matches(").append(((StringProperty) property).getPattern()).append(")@");347 } else if (!CollectionUtils.isEmpty(((StringProperty) property).getEnum())) {348 payload.append("@matches(").append(((StringProperty) property).getEnum().stream().collect(Collectors.joining("|"))).append(")@");349 } else {350 payload.append("@notEmpty()@");351 }352 if (quotes) {353 payload.append("\"");354 }355 } else if (property instanceof DateProperty) {356 if (quotes) {357 payload.append("\"");358 }359 payload.append("@matchesDatePattern('yyyy-MM-dd')@");360 if (quotes) {361 payload.append("\"");362 }363 } else if (property instanceof DateTimeProperty) {364 if (quotes) {365 payload.append("\"");366 }367 payload.append("@matchesDatePattern('yyyy-MM-dd'T'hh:mm:ss')@");368 if (quotes) {369 payload.append("\"");370 }371 } else if (property instanceof IntegerProperty || property instanceof LongProperty) {372 if (quotes) {373 payload.append("\"");374 }375 payload.append("@isNumber()@");376 if (quotes) {377 payload.append("\"");378 }379 } else if (property instanceof FloatProperty || property instanceof DoubleProperty) {380 if (quotes) {381 payload.append("\"");382 }383 payload.append("@isNumber()@");384 if (quotes) {385 payload.append("\"");386 }387 } else if (property instanceof BooleanProperty) {388 if (quotes) {389 payload.append("\"");390 }391 payload.append("@matches(true|false)@");392 if (quotes) {393 payload.append("\"");394 }395 } else {396 if (quotes) {397 payload.append("\"");398 }399 payload.append("@ignore@");400 if (quotes) {401 payload.append("\"");402 }403 }404 return payload.toString();405 }406 /**407 * Create validation expression using functions according to parameter type and format.408 * @param parameter409 * @return410 */411 private String createValidationExpression(AbstractSerializableParameter parameter) {412 switch (parameter.getType()) {413 case "integer":414 return "@isNumber()@";415 case "string":416 if (parameter.getFormat() != null && parameter.getFormat().equals("date")) {417 return "\"@matchesDatePattern('yyyy-MM-dd')@\"";418 } else if (parameter.getFormat() != null && parameter.getFormat().equals("date-time")) {419 return "\"@matchesDatePattern('yyyy-MM-dd'T'hh:mm:ss')@\"";420 } else if (StringUtils.hasText(parameter.getPattern())) {421 return "\"@matches(" + parameter.getPattern() + ")@\"";422 } else if (!CollectionUtils.isEmpty(parameter.getEnum())) {423 return "\"@matches(" + (parameter.getEnum().stream().collect(Collectors.joining("|"))) + ")@\"";424 } else {425 return "@notEmpty()@";426 }427 case "boolean":428 return "@matches(true|false)@";429 default:430 return "@ignore@";431 }432 }433 /**434 * Create random value expression using functions according to parameter type and format.435 * @param parameter436 * @return437 */438 private String createRandomValueExpression(AbstractSerializableParameter parameter) {439 switch (parameter.getType()) {440 case "integer":441 return "citrus:randomNumber(10)";442 case "string":443 if (parameter.getFormat() != null && parameter.getFormat().equals("date")) {444 return "\"citrus:currentDate('yyyy-MM-dd')\"";445 } else if (parameter.getFormat() != null && parameter.getFormat().equals("date-time")) {446 return "\"citrus:currentDate('yyyy-MM-dd'T'hh:mm:ss')\"";447 } else if (StringUtils.hasText(parameter.getPattern())) {448 return "\"citrus:randomValue(" + parameter.getPattern() + ")\"";449 } else if (!CollectionUtils.isEmpty(parameter.getEnum())) {450 return "\"citrus:randomEnumValue(" + (parameter.getEnum().stream().collect(Collectors.joining(","))) + ")\"";451 } else if (Optional.ofNullable(parameter.getFormat()).orElse("").equalsIgnoreCase("uuid")){452 return "citrus:randomUUID()";...

Full Screen

Full Screen

createRandomValueExpression

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.generate.javadsl.SwaggerJavaTestGenerator;2import com.consol.citrus.http.message.HttpMessage;3import com.consol.citrus.message.MessageType;4import com.consol.citrus.testng.AbstractTestNGCitrusTest;5import org.springframework.http.HttpStatus;6import org.testng.annotations.Test;7public class SwaggerJavaTestGeneratorIT extends AbstractTestNGCitrusTest {8 public void test() {9 variable("random", createRandomValueExpression(5));10 http().client("httpClient")11 .send()12 .get("/api/endpoint/${random}");13 http().client("httpClient")14 .receive()15 .response(HttpStatus.OK)16 .messageType(MessageType.JSON)17 .payload("{\"random\": \"${random}\"}");18 echo("random value: ${random}");19 }20}

Full Screen

Full Screen

createRandomValueExpression

Using AI Code Generation

copy

Full Screen

1public class SwaggerJavaTestGeneratorTest {2 public void testCreateRandomValueExpression() {3 SwaggerJavaTestGenerator swaggerJavaTestGenerator = new SwaggerJavaTestGenerator();4 String randomValueExpression = swaggerJavaTestGenerator.createRandomValueExpression("string");5 assertEquals("citrus:randomString(10)", randomValueExpression);6 }7}

Full Screen

Full Screen

createRandomValueExpression

Using AI Code Generation

copy

Full Screen

1SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();2generator.createRandomValueExpression(parameter);3SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();4generator.createRandomValueExpression("parameter", "path", "integer");5SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();6generator.createRandomValueExpression("parameter", "path", "integer", "int32");7SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();8generator.createRandomValueExpression("parameter", "path", "integer", "int32", "5");9SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();10generator.createRandomValueExpression("parameter", "path", "integer", "int32", "5", "10");11SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();12generator.createRandomValueExpression("parameter", "path", "integer", "int32", "5", "10", "2");13SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();14generator.createRandomValueExpression("parameter", "path", "integer", "int32", "5", "10", "2", "10");

Full Screen

Full Screen

createRandomValueExpression

Using AI Code Generation

copy

Full Screen

1public class SwaggerJavaTestGenerator {2 private static final Logger LOG = LoggerFactory.getLogger(SwaggerJavaTestGenerator.class);3 private static final String DEFAULT_BASE_PACKAGE = "com.consol.citrus.test";4 private static final String DEFAULT_TEST_NAME = "CitrusSwaggerTest";5 private static final String DEFAULT_TEST_PACKAGE = DEFAULT_BASE_PACKAGE + ".swagger";6 private static final String DEFAULT_TEST_TARGET_PATH = "src/test/java";7 private static final String DEFAULT_TEST_TARGET_PACKAGE = DEFAULT_TEST_PACKAGE;8 private static final String DEFAULT_TEST_TARGET_NAME = DEFAULT_TEST_NAME + ".java";9 private static final String DEFAULT_TEST_TARGET_RESOURCE_PATH = "src/test/resources";10 private static final String DEFAULT_TEST_TARGET_RESOURCE_PACKAGE = DEFAULT_TEST_PACKAGE;11 private static final String DEFAULT_TEST_TARGET_RESOURCE_NAME = DEFAULT_TEST_NAME + ".properties";12 private static final String DEFAULT_TEST_TARGET_RESOURCE_CONTENT = "citrus.http.server.port=${http.server.port}";13 private static final String DEFAULT_TEST_TARGET_TESTNG_NAME = "testng.xml";14 "\t\t\t<class name=\"${test.class}\"/>\r\n" +15 "</suite>\r\n";16 private static final String DEFAULT_TEST_TARGET_POM_NAME = "pom.xml";

Full Screen

Full Screen

createRandomValueExpression

Using AI Code Generation

copy

Full Screen

1SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();2String randomValueExpression = generator.createRandomValueExpression(schema);3String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema);4String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, 123456789L);5String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, 123456789L, RandomValueExpressionType.JSON_PATH);6String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, RandomValueExpressionType.JSON_PATH);7String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, 123456789L, RandomValueExpressionType.JSON_PATH);8String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, 123456789L, RandomValueExpressionType.JSON_PATH, RandomValueExpressionFormat.SIMPLE);9String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, RandomValueExpressionType.JSON_PATH, RandomValueExpressionFormat.SIMPLE);10String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, 123456789L, RandomValueExpressionType.JSON_PATH, RandomValueExpressionFormat.SIMPLE);11String randomValueExpression = SwaggerJavaTestGenerator.createRandomValueExpression(schema, RandomValueExpressionType.JSON_PATH, RandomValueExpressionFormat.SIMPLE);

Full Screen

Full Screen

createRandomValueExpression

Using AI Code Generation

copy

Full Screen

1public class RandomValueExpressionTest {2 public void testRandomValueExpression() {3 SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();4 System.out.println(generator.createRandomValueExpression("age", "number", "int32", null));5 System.out.println(generator.createRandomValueExpression("name", "string", null, null));6 System.out.println(generator.createRandomValueExpression("date", "string", "date", null));7 System.out.println(generator.createRandomValueExpression("date-time", "string", "date-time", null));8 }9}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful