Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add page item in as code v3 #29

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.neotys.neoload.model.v3.project.userpath;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.neotys.neoload.model.v3.project.SlaElement;
import com.neotys.neoload.model.v3.validation.constraints.RequiredCheck;
import com.neotys.neoload.model.v3.validation.groups.NeoLoad;
import org.immutables.value.Value;

import javax.validation.constraints.Pattern;
import java.util.List;
import java.util.Optional;

@Value.Immutable
@JsonSerialize(as = ImmutablePage.class)
@JsonDeserialize(as = ImmutablePage.class)
public interface Page extends Step, SlaElement {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests specific for this class are missing.

You can refer to what we did previously for testing a new as-code element.

For this class: com.neotys.neoload.model.v3.project.scenario.CustomPolicyStep
We made/updated these classes:

  • com.neotys.neoload.model.v3.project.scenario.CustomPolicyStepTest
  • com.neotys.neoload.model.v3.validation.validator.CustomPolicyStepTest
  • com.neotys.neoload.model.v3.validation.validator.ScenarioTest
  • com.neotys.neoload.model.v3.binding.io.IOScenariosTest

List<Request> getChildren();

@Value.Default
@RequiredCheck(groups = {NeoLoad.class})
default boolean isDynamic(){
return false;
}

@Value.Default
@RequiredCheck(groups = {NeoLoad.class})
default String getName() {
return "#page#";
}

@Value.Default
@RequiredCheck(groups = {NeoLoad.class})
default boolean isSequential(){
return false;
}

@Pattern(regexp = "(\\d+|\\$\\{\\w+\\})(-(\\d+|\\$\\{\\w+\\}))?", groups = {NeoLoad.class})
Optional<String> getThinkTime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import com.neotys.neoload.model.v3.validation.groups.NeoLoad;

@JsonInclude(value=Include.NON_DEFAULT)
@JsonPropertyOrder({Request.NAME, Request.URL, Request.SERVER, Request.METHOD, Request.HEADERS, Request.BODY, Request.EXTRACTORS, AssertionsElement.ASSERTIONS, Request.FOLLOW_REDIRECTS, SlaElement.SLA_PROFILE})
@JsonPropertyOrder({Request.NAME, Request.URL, Request.SERVER, Request.METHOD, Request.HEADERS, Request.BODY, Request.EXTRACTORS, AssertionsElement.ASSERTIONS, Request.FOLLOW_REDIRECTS, SlaElement.SLA_PROFILE, Request.DYNAMIC_RESOURCES})
@JsonSerialize(as = ImmutableRequest.class)
@JsonDeserialize(as = ImmutableRequest.class)
@Value.Immutable
@Value.Style(validationMethod = ValidationMethod.NONE)
public interface Request extends Step, SlaElement, AssertionsElement {
String NAME = "name";
String DYNAMIC_RESOURCES = "dynamicResources";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests are missing for this parameter and his getter, you can refer to the tests already existing for others parameters and getter of this class.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to update the doc with this new parameter

String URL = "url";
String SERVER = "server";
String METHOD = "method";
Expand Down Expand Up @@ -65,6 +66,12 @@ public static Method of(final String name) {
}
}

@JsonProperty(DYNAMIC_RESOURCES)
@Value.Default
default boolean isDynamic(){
return false;
}

@JsonProperty(NAME)
@RequiredCheck(groups={NeoLoad.class})
@Value.Default
Expand Down
16 changes: 15 additions & 1 deletion neoload-project/src/main/resources/as-code.latest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@
"delay": { "$ref": "#/definitions/user_paths/actions/delay" },
"think_time": { "$ref": "#/definitions/user_paths/actions/think_time" },
"javascript": { "$ref": "#/definitions/user_paths/actions/javascript" },
"if": { "$ref": "#/definitions/user_paths/actions/if" }
"if": { "$ref": "#/definitions/user_paths/actions/if" },
"page": { "$ref": "#/definitions/user_paths/actions/page" },
},
"additionalProperties": false
}
Expand Down Expand Up @@ -622,6 +623,7 @@
"additionalProperties": false
}
},
"dynamic": { "$ref": "#/definitions/common/text" },
"sla_profile": { "$ref": "#/definitions/common/text" },
"body": { "$ref": "#/definitions/common/textblob" },
"extractors": {
Expand Down Expand Up @@ -678,6 +680,18 @@
"then": { "$ref": "#/definitions/user_paths/container" },
"else": { "$ref": "#/definitions/user_paths/container" }
}
},
"page": {
"$id":"#/definitions/user_paths/actions/page",
"title":"Action: Page",
"type": "object",
"properties": {
"name": { "$ref": "#/definitions/common/text" },
"description": { "$ref": "#/definitions/common/text" },
"isDynamic": { "$ref": "#/definitions/common/text" },
"isSequential": { "$ref": "#/definitions/common/text" },
"thinkTime": { "$ref": "#/definitions/common/text" }
}
}

},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.neotys.neoload.model.v3.writers.neoload;

import com.google.common.annotations.VisibleForTesting;
import com.neotys.neoload.model.v3.project.Element;
import org.apache.commons.collections4.map.LazyMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

public class WriterUtils {

Expand Down Expand Up @@ -78,4 +76,9 @@ public static boolean isNLVariable(final String value){
public static String extractVariableName(final String value) {
return value.substring(NL_VARIABLE_START.length(), value.length()-NL_VARIABLE_END.length());
}

@VisibleForTesting
public static Set<Map.Entry<Element, String>> getGeneratedUids(){
return elementUids.entrySet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.neotys.neoload.model.v3.writers.neoload.userpath;

import com.neotys.neoload.model.v3.project.userpath.Page;
import com.neotys.neoload.model.v3.writers.neoload.ElementWriter;
import com.neotys.neoload.model.v3.writers.neoload.SlaElementWriter;
import com.neotys.neoload.model.v3.writers.neoload.WriterUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PageWriter extends ElementWriter {

public static final String XML_TAG_NAME = "http-page";
public static final String XML_ATTRIBUTE_THINK_TIME = "thinkTime";
public static final String XML_ATTRIBUTE_THINK_TIME_START = "thinkTimeRangeStart";
public static final String XML_ATTRIBUTE_THINK_TIME_STOP = "thinkTimeRangeEnd";
public static final String XML_ATTRIBUTE_THINK_TIME_MODE = "thinkTimeMode";

public static final String MODE_RANGE_THINK_TIME = "MODE_RANGE_THINK_TIME";

public static final String XML_ATTRIBUTE_EXECUTE_RESOURCES_DYNAMICALLY = "executeResourcesDynamically";

private static Pattern patternThinkTime = Pattern.compile("(\\d+|\\$\\{\\w+\\})-(\\d+|\\$\\{\\w+\\})");

public PageWriter(final Page page) {
super(page);
}

public static PageWriter of(final Page page){
return new PageWriter(page);
}

@Override
public void writeXML(final Document document, final Element currentElement, final String outputFolder) {
final Element xmlPage = document.createElement(XML_TAG_NAME);
super.writeXML(document, xmlPage, outputFolder);
final Page thePage = (Page) this.element;
thePage.getThinkTime().ifPresent(thinkTime -> writeDelay(xmlPage, thinkTime));
xmlPage.setAttribute(XML_ATTRIBUTE_EXECUTE_RESOURCES_DYNAMICALLY, Boolean.toString(thePage.isDynamic()));
currentElement.appendChild(xmlPage);
SlaElementWriter.of(thePage).writeXML(xmlPage);
thePage.getChildren().forEach(pageElem -> {
WriterUtils.generateEmbeddedAction(document, xmlPage, pageElem);
WriterUtils.<ElementWriter>getWriterFor(pageElem).writeXML(document, currentElement, outputFolder);
});
}

private void writeDelay(final Element xmlPage, final String thinkTime) {
final Matcher matcher = patternThinkTime.matcher(thinkTime);
if(matcher.matches()){
xmlPage.setAttribute(XML_ATTRIBUTE_THINK_TIME_MODE,MODE_RANGE_THINK_TIME);
xmlPage.setAttribute(XML_ATTRIBUTE_THINK_TIME_START, matcher.group(1));
xmlPage.setAttribute(XML_ATTRIBUTE_THINK_TIME_STOP, matcher.group(2));
}else{
xmlPage.setAttribute(XML_ATTRIBUTE_THINK_TIME,thinkTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import java.util.List;
import java.util.Optional;

import com.neotys.neoload.model.v3.project.userpath.*;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.google.common.net.MediaType;
import com.neotys.neoload.model.v3.project.userpath.Part;
import com.neotys.neoload.model.v3.project.userpath.Request;
import com.neotys.neoload.model.v3.project.userpath.assertion.Assertion;
import com.neotys.neoload.model.v3.util.Parameter;
import com.neotys.neoload.model.v3.util.RequestUtils;
Expand Down Expand Up @@ -49,8 +48,19 @@ public RequestWriter(Request request) {

@Override
public void writeXML(final Document document, final Element currentElement, final String outputFolder) {
final Element xmlRequest = document.createElement(XML_TAG_NAME);
final Request theRequest = (Request) this.element;
if (theRequest.isDynamic()){
final URL url = RequestUtils.parseUrl(Optional.ofNullable(theRequest.getUrl()).orElse("/"));
final Request requestUnDynamic = ImmutableRequest.copyOf(theRequest).withIsDynamic(false);
final ImmutablePage pageDynamic = ImmutablePage.builder()
.addChildren(requestUnDynamic)
.name(url.getPath())
.isDynamic(true)
.build();
PageWriter.of(pageDynamic).writeXML(document,currentElement,outputFolder);
return;
}
final Element xmlRequest = document.createElement(XML_TAG_NAME);
super.writeXML(document, xmlRequest, outputFolder);
fillXML(document, xmlRequest, theRequest);
SlaElementWriter.of(theRequest).writeXML(xmlRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package com.neotys.neoload.model.v3.writers.neoload.userpath;

import com.neotys.neoload.model.v3.project.userpath.ThinkTime;
import com.neotys.neoload.model.v3.writers.neoload.ElementWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ThinkTimeWriter extends ElementWriter {

public static final String XML_TAG_NAME = "delay-action";
public static final String XML_DURATION_ATT = "duration";
public static final String XML_ISTHINKTIME_ATT = "isThinkTime";
public class ThinkTimeWriter extends AbstractDelayActionWriter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is not part of the commit and is missing...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah bad cut it is on other pr. So I will merge.


public ThinkTimeWriter(ThinkTime thinktime) {
super(thinktime);
super(thinktime,thinktime.getValue(),true);
}

public static ThinkTimeWriter of(final ThinkTime thinktime) {
return new ThinkTimeWriter(thinktime);
}

@Override
public void writeXML(final Document document, final Element currentElement, final String outputFolder) {
Element xmlDelay = document.createElement(XML_TAG_NAME);
super.writeXML(document, xmlDelay, outputFolder);
xmlDelay.setAttribute(XML_DURATION_ATT, ((ThinkTime)element).getValue());
xmlDelay.setAttribute(XML_ISTHINKTIME_ATT, "true");
currentElement.appendChild(xmlDelay);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.neotys.neoload.model.v3.writers.neoload.userpath;

import com.google.common.io.Files;
import com.neotys.neoload.model.v3.project.userpath.ImmutablePage;
import com.neotys.neoload.model.v3.project.userpath.ImmutableRequest;
import com.neotys.neoload.model.v3.project.userpath.Page;
import com.neotys.neoload.model.v3.project.userpath.Request;
import com.neotys.neoload.model.v3.writers.neoload.WriterUtils;
import com.neotys.neoload.model.v3.writers.neoload.WrittingTestUtils;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xmlunit.assertj.XmlAssert;
import org.xmlunit.builder.Input;

import javax.xml.parsers.ParserConfigurationException;

public class PageWriterTest {

@Test
public void writePageTestClassic() throws ParserConfigurationException {
final Document doc = WrittingTestUtils.generateEmptyDocument();
final Element root = WrittingTestUtils.generateTestRootElement(doc);
final ImmutableRequest request = Request.builder().url("http://1.com").build();
final Page page = ImmutablePage.builder()
.name("myPage")
.description("myDescription")
.isDynamic(true)
.addChildren(request)
.build();
final String requestUid = WriterUtils.getElementUid(request);
final String expectedResult =
"<test-root>" +
"<http-page executeResourcesDynamically=\"true\" name=\"myPage\" uid=\""+ WriterUtils.getElementUid(page)+"\">" +
"<description>myDescription</description>" +
"<embedded-action>"+ requestUid +"</embedded-action>" +
"</http-page>" +
"<http-action actionType=\"1\" followRedirects=\"false\" method=\"GET\" name=\"#request#\" path=\"\" slaProfileEnabled=\"false\" uid=\""+requestUid+"\"/>" +
"</test-root>";
PageWriter.of(page).writeXML(doc, root, Files.createTempDir().getAbsolutePath());

XmlAssert.assertThat(Input.fromDocument(doc)).and(Input.fromString(expectedResult)).areSimilar();
}

@Test
public void writePageTest() throws ParserConfigurationException {
final Document doc = WrittingTestUtils.generateEmptyDocument();
final Element root = WrittingTestUtils.generateTestRootElement(doc);
final Request request1 = Request.builder().url("http://2.com").build();
final Request request2 = Request.builder().url("http://3.com").build();
final Page page = ImmutablePage.builder()
.name("myPage")
.description("myDescription")
.thinkTime("10-20")
.addChildren(request1)
.addChildren(request2)
.build();
final String request1Uid = WriterUtils.getElementUid(request1);
final String request2Uid = WriterUtils.getElementUid(request2);
final String expectedResult =
"<test-root>" +
"<http-page executeResourcesDynamically=\"false\" name=\"myPage\" thinkTimeMode=\"MODE_RANGE_THINK_TIME\" thinkTimeRangeEnd=\"20\" thinkTimeRangeStart=\"10\" uid=\""+ WriterUtils.getElementUid(page)+"\">" +
"<description>myDescription</description>" +
"<embedded-action>"+ request1Uid +"</embedded-action>" +
"<embedded-action>"+ request2Uid +"</embedded-action>" +
"</http-page>" +
"<http-action actionType=\"1\" followRedirects=\"false\" method=\"GET\" name=\"#request#\" path=\"\" slaProfileEnabled=\"false\" uid=\""+ request1Uid +"\"/>" +
"<http-action actionType=\"1\" followRedirects=\"false\" method=\"GET\" name=\"#request#\" path=\"\" slaProfileEnabled=\"false\" uid=\""+ request2Uid +"\"/>" +
"</test-root>";
PageWriter.of(page).writeXML(doc, root, Files.createTempDir().getAbsolutePath());

XmlAssert.assertThat(Input.fromDocument(doc)).and(Input.fromString(expectedResult)).areSimilar();
}

@Test
public void writePageSLATest() throws ParserConfigurationException {
final Document doc = WrittingTestUtils.generateEmptyDocument();
final Element root = WrittingTestUtils.generateTestRootElement(doc);
final Request request1 = Request.builder().url("http://2.com").build();
final Request request2 = Request.builder().url("http://3.com").build();
final Page page = ImmutablePage.builder()
.name("myPage")
.description("myDescription")
.thinkTime("10-20")
.addChildren(request1)
.addChildren(request2)
.slaProfile("toto")
.build();
final String request1Uid = WriterUtils.getElementUid(request1);
final String request2Uid = WriterUtils.getElementUid(request2);
final String expectedResult =
"<test-root>" +
"<http-page executeResourcesDynamically=\"false\" name=\"myPage\" slaProfileEnabled=\"true\" slaProfileName=\"toto\" thinkTimeMode=\"MODE_RANGE_THINK_TIME\" thinkTimeRangeEnd=\"20\" thinkTimeRangeStart=\"10\" uid=\""+ WriterUtils.getElementUid(page)+"\">" +
"<description>myDescription</description>" +
"<embedded-action>"+ request1Uid +"</embedded-action>" +
"<embedded-action>"+ request2Uid +"</embedded-action>" +
"</http-page>" +
"<http-action actionType=\"1\" followRedirects=\"false\" method=\"GET\" name=\"#request#\" path=\"\" slaProfileEnabled=\"false\" uid=\""+ request1Uid +"\"/>" +
"<http-action actionType=\"1\" followRedirects=\"false\" method=\"GET\" name=\"#request#\" path=\"\" slaProfileEnabled=\"false\" uid=\""+ request2Uid +"\"/>" +
"</test-root>";
PageWriter.of(page).writeXML(doc, root, Files.createTempDir().getAbsolutePath());

XmlAssert.assertThat(Input.fromDocument(doc)).and(Input.fromString(expectedResult)).areSimilar();
}
}
Loading