Skip to content
Permalink
Browse files

handle openapi singleton reset on config reset

  • Loading branch information...
MykolaGolubyev committed May 10, 2019
1 parent 9fd1b7f commit d203c846efb2f55f2a786257b84baffce6dedb64
@@ -38,7 +38,7 @@ import java.nio.file.Paths

import static com.twosigma.webtau.cfg.WebTauConfig.getCfg

class WebTauTestRunner implements StepReporter, StandaloneTestListener {
class WebTauEndToEndTestRunner implements StepReporter, StandaloneTestListener {
private static final String RUN_DETAILS_FILE_NAME = 'run-details'
private static final String EXPECTATIONS_DIR_NAME = 'test-expectations'

@@ -47,7 +47,7 @@ class WebTauTestRunner implements StepReporter, StandaloneTestListener {

TestServer testServer

WebTauTestRunner() {
WebTauEndToEndTestRunner() {
testServer = new TestServer()
}

@@ -116,7 +116,7 @@ class WebTauTestRunner implements StepReporter, StandaloneTestListener {
ConsoleOutputs.out('reports are different, you can use IDE to compare files: ', Color.PURPLE, actualPath,
Color.BLUE, ' and ', Color.PURPLE, expectedPath)
FileUtils.writeTextContent(actualPath, serializedTestDetails)
throw new AssertionError(comparator.generateNotEqualMatchReport())
throw new AssertionError(comparator.generateEqualMismatchReport())
} else {
Files.deleteIfExists(actualPath)
}
@@ -23,11 +23,11 @@ import org.junit.Test
import static com.twosigma.webtau.cfg.WebTauConfig.cfg

class WebTauConceptFeaturesTest {
private static WebTauTestRunner testRunner
private static WebTauEndToEndTestRunner testRunner

@BeforeClass
static void init() {
testRunner = new WebTauTestRunner()
testRunner = new WebTauEndToEndTestRunner()
}

@Before
@@ -29,7 +29,7 @@ import static com.twosigma.webtau.WebTauDsl.http
import static com.twosigma.webtau.featuretesting.FeaturesDocArtifactsExtractor.extractCodeSnippets

class WebTauRestFeaturesTest {
private static WebTauTestRunner testRunner
private static WebTauEndToEndTestRunner testRunner

static void registerEndPoints(TestServer testServer) {
def temperature = [temperature: 88]
@@ -42,7 +42,7 @@ class WebTauRestFeaturesTest {

@BeforeClass
static void init() {
testRunner = new WebTauTestRunner()
testRunner = new WebTauEndToEndTestRunner()

def testServer = testRunner.testServer
registerEndPoints(testServer)
@@ -30,7 +30,7 @@ import java.nio.file.Paths
import static com.twosigma.webtau.featuretesting.FeaturesDocArtifactsExtractor.*

class WebTauUiFeaturesTest {
private static WebTauTestRunner testRunner
private static WebTauEndToEndTestRunner testRunner

static void registerEndPoints(TestServer testServer) {
testServer.registerGet("/search", htmlResponse('search.html'))
@@ -47,7 +47,7 @@ class WebTauUiFeaturesTest {

@BeforeClass
static void init() {
testRunner = new WebTauTestRunner()
testRunner = new WebTauEndToEndTestRunner()

def testServer = testRunner.testServer
registerEndPoints(testServer)
@@ -17,10 +17,21 @@
package com.twosigma.webtau.openapi;

public class OpenApi {
public static final OpenApiSpec spec = new OpenApiSpec(OpenApiSpecConfig.specFullPath());
public static final OpenApiSpecValidator validator = new OpenApiSpecValidator(spec, validationConfig());
private static OpenApiSpec spec;
private static OpenApiSpecValidator validator;
private static OpenApiCoverage coverage;

public static final OpenApiCoverage coverage = new OpenApiCoverage(spec);
static OpenApiSpec getSpec() {
return spec;
}

static OpenApiSpecValidator getValidator() {
return validator;
}

static OpenApiCoverage getCoverage() {
return coverage;
}

public static void withoutValidation(Runnable code) {
OpenApiResponseValidator.withMode(ValidationMode.NONE, code);
@@ -34,10 +45,17 @@ public static void requestOnlyValidation(Runnable code) {
OpenApiResponseValidator.withMode(ValidationMode.REQUEST_ONLY, code);
}

static void reset() {
spec = new OpenApiSpec(OpenApiSpecConfig.specFullPath());
validator = new OpenApiSpecValidator(spec, validationConfig());
coverage = new OpenApiCoverage(spec);
}

private static OpenApiValidationConfig validationConfig() {
OpenApiValidationConfig config = new OpenApiValidationConfig();
config.setIgnoreAdditionalProperties(OpenApiSpecConfig.ignoreAdditionalProperties.getAsBoolean());

return config;
}

}
@@ -22,6 +22,6 @@
public class OpenApiCoverageRecorder implements HttpValidationHandler {
@Override
public void validate(HttpValidationResult validationResult) {
OpenApi.coverage.recordOperation(validationResult);
OpenApi.getCoverage().recordOperation(validationResult);
}
}
@@ -28,18 +28,18 @@
public class OpenApiReportDataProvider implements ReportDataProvider {
@Override
public Stream<ReportCustomData> provide(ReportTestEntries testEntries) {
List<? extends Map<String, ?>> nonCovered = OpenApi.coverage.nonCoveredOperations()
List<? extends Map<String, ?>> nonCovered = OpenApi.getCoverage().nonCoveredOperations()
.map(OpenApiOperation::toMap)
.collect(Collectors.toList());

List<? extends Map<String, ?>> covered = OpenApi.coverage.coveredOperations()
List<? extends Map<String, ?>> covered = OpenApi.getCoverage().coveredOperations()
.map(OpenApiOperation::toMap)
.collect(Collectors.toList());

return Stream.of(
new ReportCustomData("openApiSkippedOperations", nonCovered),
new ReportCustomData("openApiCoveredOperations", covered),
new ReportCustomData("openApiHttpCallIdsPerOperation",
OpenApi.coverage.httpCallIdsByOperationAsMap()));
OpenApi.getCoverage().httpCallIdsByOperationAsMap()));
}
}
@@ -44,15 +44,15 @@ public void validate(HttpValidationResult validationResult) {
return;
}

if (!OpenApi.validator.isSpecDefined()) {
if (!OpenApi.getValidator().isSpecDefined()) {
return;
}

String modeLabel = validationModeLabel(mode);
TestStep.createAndExecuteStep(null,
tokenizedMessage(action("validating"), classifier(modeLabel)),
() -> tokenizedMessage(action("validated"), classifier(modeLabel)),
() -> OpenApi.validator.validateApiSpec(validationResult, mode));
() -> OpenApi.getValidator().validateApiSpec(validationResult, mode));
}

private static String validationModeLabel(ValidationMode mode) {
@@ -17,6 +17,7 @@
package com.twosigma.webtau.openapi;

import com.twosigma.webtau.cfg.ConfigValue;
import com.twosigma.webtau.cfg.WebTauConfig;
import com.twosigma.webtau.cfg.WebTauConfigHandler;

import java.util.stream.Stream;
@@ -31,14 +32,17 @@
static final ConfigValue ignoreAdditionalProperties = declare("openApiIgnoreAdditionalProperties",
"ignore additional OpenAPI properties ", () -> false);

public static String specFullPath() {
if (specUrl.getAsString().isEmpty()) {
return "";
}
private static String fullPath;

return getCfg().getWorkingDir()
.resolve(specUrl.getAsString())
.toString();
static String specFullPath() {
return fullPath;
}

@Override
public void onAfterCreate(WebTauConfig cfg) {
fullPath = specUrl.getAsString().isEmpty() ? "" :
cfg.getWorkingDir().resolve(specUrl.getAsString()).toString();
OpenApi.reset();
}

@Override
@@ -16,6 +16,7 @@

package com.twosigma.webtau.openapi

import com.twosigma.webtau.cfg.WebTauConfig
import com.twosigma.webtau.http.HttpHeader
import com.twosigma.webtau.http.HttpResponse
import com.twosigma.webtau.http.json.JsonRequestBody
@@ -41,6 +42,7 @@ class OpenApiResponseValidatorTest implements StepReporter {
static void init() {
OpenApiSpecConfig.specUrl.set('test-manual', Paths.get(
ResourceUtils.resourceUrl("test-spec.json").toURI()))
WebTauConfig.cfg.get('openApiSpecUrl').toString().should contain('test-spec.json')
}

@Before

0 comments on commit d203c84

Please sign in to comment.
You can’t perform that action at this time.