Skip to content

Commit

Permalink
further #65 work - config xml generator is working with minimal capab…
Browse files Browse the repository at this point in the history
…ilities
  • Loading branch information
tkohegyi committed Feb 9, 2016
1 parent bfff025 commit ecc41c8
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,19 @@ public RequestCondition build() {
return new RequestCondition(configurationString);
}

public RequestConditionBuilder withHeader(String blah) {
public RequestConditionBuilder withHeader(String name, String value) {
String conditionString = "<condition class=\"HeaderParameterChecker\">\n" +
" <param name=\"" + name + "\" value=\"" + value + "\" />\n" +
"</condition>\n";
configurationString += conditionString;
return this;
}

public RequestConditionBuilder withHeader(String pattern) {
String conditionString = "<condition class=\"AndHeaderPatternChecker\">\n" +
" <param name=\"dummy\" value=\"" + pattern + "\" />\n" +
"</condition>\n";
configurationString += conditionString;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.epam.wilma.service.unit.helper.ConfigurationParameter;
import com.epam.wilma.service.unit.helper.StubConfigurationException;
import com.epam.wilma.service.unit.helper.Template;
import com.epam.wilma.service.unit.helper.TemplateFormatter;
import com.epam.wilma.service.unit.request.RequestCondition;
import com.epam.wilma.service.unit.response.ResponseDescriptor;
Expand All @@ -40,20 +41,21 @@ public class ResponseDescriptorBuilder {
private String code = "200";
private String delay = "0";
private String mimeType = "text/plain";
private String templateType = "text";
private String templateResource = "Wilma response";
private Template template = new Template("text", "Wilma default response");
private LinkedList<TemplateFormatter> templateFormatters = new LinkedList<>();

public ResponseDescriptorBuilder(RequestCondition requestCondition) {
this.requestCondition = requestCondition;
}

public ResponseDescriptorBuilder plainTextResponse(String plainTextResponse) {
mimeType = "text/plain";
template = new Template("text", plainTextResponse);
return this;
}

public ResponseDescriptor buildResponseDescriptor() {
return new ResponseDescriptor();
return new ResponseDescriptor(delay, code, mimeType, template, templateFormatters);
}

public Stub build() {
Expand Down Expand Up @@ -84,4 +86,12 @@ public ResponseDescriptorBuilder applyFormatter(String formatterClass, Configura
public ResponseDescriptorBuilder generatedResponse() {
return this;
}

public ResponseDescriptorBuilder withDelay(int i) {
if (i < 0) {
throw new StubConfigurationException("Given Response Delay (" + i + ") is invalid.");
}
delay = String.valueOf(i);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public Stub(RequestCondition requestCondition, ResponseDescriptor responseDescri
public String toString() {
String generatedName = "generated name";
String conditionContent = requestCondition.toString();
String responseContent = responseDescriptor.toString();
String usedTemplateAndFormatter = responseDescriptor.toString();
String responseContent = responseDescriptor.responseDescriptorToString();
String usedTemplateAndFormatter = responseDescriptor.templateToString();
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format(STUB_CONFIGURATION_FORMATTER, generatedName, conditionContent, responseContent, usedTemplateAndFormatter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.epam.wilma.service.unit.helper;
/*==========================================================================
Copyright 2013-2016 EPAM Systems
This file is part of Wilma.
Wilma is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wilma is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wilma. If not, see <http://www.gnu.org/licenses/>.
===========================================================================*/

/**
* Class that holds a single response template information.
*
* @author Tamas_Kohegyi
*
*/
public class Template {
private String name;
private String type;
private String resource;

/**
* General constructor of a Template class.
*
* @param type can be text|xmlfile|htmlfile|textfile|external
* @param resource is the template itself, or filename to the template or a classname that generates the template
*/
public Template(String type, String resource) {
this.name = "template-" + IdGenerator.getNextGeneratedId();
this.type = type;
this.resource = resource;
}
/**
* Generates String value for the template.
* @return with the config string
*/
public String toString() {

String templateString = "<template name=\"" + name + "\" type=\"" + type + "\" resource=\"" + resource + "\">\n";
return templateString;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String toString() {
if (configurationParameters != null) {
//we have parameters too
for (ConfigurationParameter configurationParameter : configurationParameters) {
templateFormatterString += configurationParameter.toString() + "\n";
templateFormatterString += " " + configurationParameter.toString() + "\n";
}
}
templateFormatterString += "</template-formatter>\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,44 @@
along with Wilma. If not, see <http://www.gnu.org/licenses/>.
===========================================================================*/

import com.epam.wilma.service.unit.helper.Template;
import com.epam.wilma.service.unit.helper.TemplateFormatter;

import java.util.LinkedList;

/**
* A Response Descriptor class.
*
* @author Tamas_Kohegyi
*
*/
public class ResponseDescriptor {
private String delay;
private String code;
private String mimeType;
private Template template;
private LinkedList<TemplateFormatter> templateFormatters;


public ResponseDescriptor(String delay, String code, String mimeType, Template template, LinkedList<TemplateFormatter> templateFormatters) {
this.delay = delay;
this.code = code;
this.mimeType = mimeType;
this.template = template;
this.templateFormatters = templateFormatters;
}
public String responseDescriptorToString() {
String responseDescriptor = "<response-descriptor delay=\"" + delay + "\" "
+ "code=\"" + code + "\" mimetype=\"" + mimeType + "\" template=\"" + template.getName() + "\" >\n";
for (TemplateFormatter templateFormatter: templateFormatters) {
responseDescriptor += templateFormatter.toString();
}
responseDescriptor += "</response-descriptor>\n";
return responseDescriptor;
}

public String templateToString() {
return template.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public void testCreateStubExtremelyComplex() {
.forRequestsLike()
.notStart()
.orStart()
.andStart().withHeader("blah").withHeader("blah2").condition("AlwaysTrueChecker").andEnd()
.andStart().withHeader("blah").withHeader("blah2","blah2").condition("AlwaysTrueChecker").andEnd()
.comingFrom("localhost")
.comingFrom("192.168.0.1")
.negatedCondition("AlwaysFalseChecker")
.orEnd()
.notEnd()
.willResponseWith().plainTextResponse("{ \"ERROR\":\"fromtext\" }").withStatus(404)
.willResponseWith().plainTextResponse("{ \"ERROR\":\"fromtext\" }").withStatus(404).withDelay(1000)
.applyFormatter("StringReplaceTemplateFormatter", formatterParameters).applyFormatter("JsonTemplateFormatter")
.build();
stub.start();
Expand Down

0 comments on commit ecc41c8

Please sign in to comment.