Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Doroshin committed Feb 20, 2018
2 parents 3c90184 + 91e08de commit 137653d
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 63 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
*.iml
target
target
src/main/resources/velocity.properties
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@
<version>2.4.10</version>
</dependency>

</dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.1</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
</project>
9 changes: 4 additions & 5 deletions src/main/java/ru/bsc/filter/CorsFilter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.bsc.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
Expand All @@ -8,16 +9,15 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* Created by sdoroshin on 03.08.2017.
*
*/
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {

public void init(FilterConfig filterConfig) {
//no initialization logic
}

@Override
Expand All @@ -28,14 +28,13 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
((HttpServletResponse)response).addHeader("Allow", "GET, POST, PUT, DELETE, OPTIONS");
if ("OPTIONS".equals(((HttpServletRequest)request).getMethod())) {
((HttpServletResponse)response).setStatus(200);

return;
}
chain.doFilter(request, response);
}

@Override
public void destroy() {

//no destroy logic
}
}
9 changes: 9 additions & 0 deletions src/main/java/ru/bsc/wiremock/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.bsc.wiremock;

/**
* Created by smakarov
* 13.02.2018 11:15
*/
public interface Constants {
String VELOCITY_PROPERTIES = "/velocity.properties";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.bsc.wiremock.velocity.directive;

import java.io.Writer;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.apache.velocity.context.InternalContextAdapter;
Expand All @@ -9,9 +11,6 @@
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;

import java.io.IOException;
import java.io.Writer;

/**
* Created by sdoroshin on 14.08.2017.
*
Expand All @@ -28,7 +27,7 @@ public int getType() {
}

@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String groovyScript = node.jjtGetChild(0).getFirstToken().image;

Binding binding = new Binding();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package ru.bsc.wiremock.velocity.directive;

import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;
import org.xml.sax.InputSource;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;

/**
* Created by sdoroshin on 26.07.2017.
*
Expand All @@ -32,7 +29,7 @@ public int getType() {
}

@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node ) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
public boolean render(InternalContextAdapter context, Writer writer, Node node ) throws IOException {
String variableName = node.jjtGetChild(0).getFirstToken().image.substring(1);
String xmlDocument = String.valueOf(node.jjtGetChild(1).value(context));
String xpathString = String.valueOf(node.jjtGetChild(2).value(context));
Expand All @@ -42,7 +39,7 @@ public boolean render(InternalContextAdapter context, Writer writer, Node node )
String evaluatedValue = xpath.evaluate(xpathString, new InputSource(new StringReader(xmlDocument)));
context.put(variableName, evaluatedValue);
} catch (XPathExpressionException e) {
throw new IOException( "cannot evaluate xpath: " + e.getMessage() );
throw new IOException("cannot evaluate xpath: " + e.getMessage(), e);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package ru.bsc.wiremock.webcontextlistener;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import com.github.tomakehurst.wiremock.common.Notifier;
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.core.WireMockApp;
import com.github.tomakehurst.wiremock.http.AdminRequestHandler;
import com.github.tomakehurst.wiremock.http.StubRequestHandler;
import com.github.tomakehurst.wiremock.servlet.NotImplementedContainer;
import com.github.tomakehurst.wiremock.servlet.WireMockWebContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.bsc.wiremock.webcontextlistener.configuration.CustomWarConfiguration;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import static com.google.common.base.MoreObjects.firstNonNull;
import static ru.bsc.wiremock.Constants.VELOCITY_PROPERTIES;

/**
* Created by sdoroshin on 24.07.2017.
*
*/
public class CustomWireMockWebContextListener extends WireMockWebContextListener {
private final Logger logger = LoggerFactory.getLogger(CustomWireMockWebContextListener.class);

public static final String APP_CONTEXT_KEY = "WireMockApp";
private static final String APP_CONTEXT_KEY = "WireMockApp";

@Override
public void contextInitialized(ServletContextEvent sce) {
Expand All @@ -34,11 +38,11 @@ public void contextInitialized(ServletContextEvent sce) {

Properties properties = new Properties();
String wireMockMappingPath = "";
try (final InputStream stream = this.getClass().getResourceAsStream("/velocity.properties")) {
try (final InputStream stream = this.getClass().getResourceAsStream(VELOCITY_PROPERTIES)) {
properties.load(stream);
wireMockMappingPath = properties.getProperty("wiremock.mapping.path");
} catch (IOException e) {
e.printStackTrace();
logger.error("Error while loading properties", e);
}

WireMockApp wireMockApp = new WireMockApp(new CustomWarConfiguration(context, wireMockMappingPath), new NotImplementedContainer());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
package ru.bsc.wiremock.webcontextlistener.configuration;

import com.github.tomakehurst.wiremock.core.MappingsSaver;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.google.common.io.Files;
import org.apache.commons.codec.Charsets;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Logger;

import com.github.tomakehurst.wiremock.core.MappingsSaver;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.google.common.io.Files;
import org.apache.commons.codec.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static ru.bsc.wiremock.Constants.VELOCITY_PROPERTIES;

/**
* Created by sdoroshin on 04.08.2017.
*
*/
class BscMappingSaver implements MappingsSaver {
private final Logger logger = LoggerFactory.getLogger(BscMappingSaver.class);

private String mappingPath = null;
private String velocityPath = null;
private final String mappingPath;
private final SimpleDateFormat dateFormat;

BscMappingSaver() {
Properties properties = new Properties();
try (final InputStream stream = this.getClass().getResourceAsStream("/velocity.properties")) {
try (final InputStream stream = this.getClass().getResourceAsStream(VELOCITY_PROPERTIES)) {
properties.load(stream);
} catch (IOException e) {
e.printStackTrace();
logger.error("Error while loading properties", e);
}
mappingPath = properties.getProperty("wiremock.mapping.path");
velocityPath = properties.getProperty("file.resource.loader.path");
dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS", Locale.ENGLISH);
}

private static Logger logger = Logger.getLogger(BscMappingSaver.class.getName());

@Override
public void save(List<StubMapping> stubMappings) {
try {
File mappingActual = new File(mappingPath + "/mappings/");

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS");
mappingActual.renameTo(new File(mappingPath + "/mappings_" + format.format(Calendar.getInstance().getTime()) + "/"));
//noinspection ResultOfMethodCallIgnored
mappingActual.renameTo(new File(mappingPath + "/mappings_" + dateFormat.format(Calendar.getInstance().getTime()) + "/"));
for (StubMapping stubMapping: stubMappings) {
String fileName = "";
if (stubMapping.getRequest().getUrl() != null) {
Expand All @@ -57,19 +59,19 @@ public void save(List<StubMapping> stubMappings) {
Files.write(stubMapping.toString(), new File(file, stubMapping.getUuid() + ".json"), Charsets.UTF_8);
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Error while save subMapping list", e);
}
logger.info("Save list: " + stubMappings.toString());
logger.info("Save list: {}", stubMappings.toString());
}

@Override
public void save(StubMapping stubMapping) {
logger.info("Save one: " + stubMapping.toString());
logger.info("Save one: {}", stubMapping.toString());
}

@Override
public void remove(StubMapping stubMapping) {
logger.info("Remove: " + stubMapping.toString());
logger.info("Remove: {}", stubMapping.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package ru.bsc.wiremock.webcontextlistener.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;

import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.core.MappingsSaver;
import com.github.tomakehurst.wiremock.extension.Extension;
import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
import com.github.tomakehurst.wiremock.servlet.WarConfiguration;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.bsc.wiremock.transformers.CustomVelocityResponseTransformer;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import static ru.bsc.wiremock.Constants.VELOCITY_PROPERTIES;

/**
* Created by sdoroshin on 04.08.2017.
*
*/
public class CustomWarConfiguration extends WarConfiguration {
private final Logger logger = LoggerFactory.getLogger(CustomWarConfiguration.class);

private String fileSourceRoot;
private final String fileSourceRoot;

public CustomWarConfiguration(ServletContext servletContext, String fileSourceRoot) {
super(servletContext);
Expand All @@ -37,10 +42,10 @@ public <T extends Extension> Map<String, T> extensionsOfType(Class<T> extensionT

// VelocityResponseTransformer configuration
Properties properties = new Properties();
try (final InputStream stream = this.getClass().getResourceAsStream("/velocity.properties")) {
try (final InputStream stream = this.getClass().getResourceAsStream(VELOCITY_PROPERTIES)) {
properties.load(stream);
} catch (IOException e) {
e.printStackTrace();
logger.error("Error while loading properties", e);
}
Velocity.init(properties);

Expand All @@ -59,4 +64,4 @@ public FileSource filesRoot() {
public MappingsSaver mappingsSaver() {
return new BscMappingSaver();
}
}
}
24 changes: 24 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<!-- relative logs path for tomcat -->
<file>..\logs\wire-mock.log</file>
<append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>

0 comments on commit 137653d

Please sign in to comment.