Convert from groovy to java (#1481)

This commit is contained in:
Anatolii Zhmaiev
2020-08-25 09:26:14 +03:00
committed by GitHub
parent d8cdf442a0
commit 130068ca41
5 changed files with 152 additions and 143 deletions

View File

@@ -1,125 +0,0 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.util
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
import java.util.function.Predicate
import groovy.transform.CompileStatic
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.spec.ContractConverter
import org.springframework.cloud.contract.verifier.converter.YamlContractConverter
import org.springframework.core.io.support.SpringFactoriesLoader
/**
* Scans through the given directory and converts all files for
* contract definitions.
*
* @author Marcin Grzejszczak
* @since 2.1.0
*/
@CompileStatic
final class ContractScanner {
private static final Log log = LogFactory.getLog(ContractScanner.class)
/**
* Traverses through the directories, applies converters
* to files that match them and converts the files to {@link Contract}.
* No additional file filtering takes place.
*
* @param rootDirectory - directory to traverse through
* @return collection of converted contracts
*/
@SuppressWarnings("unchecked")
static Collection<Contract> collectContractDescriptors(
final File rootDirectory) {
return collectContractDescriptors(rootDirectory, { true })
}
/**
* Traverses through the directories, applies converters
* to files that match them and converts the files to {@link Contract}.
* Filters out files not matching a predicate.
*
* @param rootDirectory - directory to traverse through
* @param predicate - test applied against a file
* @return collection of converted contracts
*/
@SuppressWarnings("unchecked")
static Collection<Contract> collectContractDescriptors(
final File rootDirectory, Predicate<File> predicate) {
final List<Contract> contractDescriptors = new ArrayList<>()
try {
Files.walkFileTree(Paths.get(rootDirectory.toURI()),
new SimpleFileVisitor<Path>() {
@Override
FileVisitResult visitFile(Path path,
BasicFileAttributes attrs) throws IOException {
File file = path.toFile()
ContractConverter converter = contractConverter(file)
if (predicate.test(file)) {
if (isContractDescriptor(file)) {
contractDescriptors
.addAll(ContractVerifierDslConverter
.convertAsCollection(
file.getParentFile(), file).findAll { it })
}
else if (converter != null
&& converter.isAccepted(file)) {
contractDescriptors
.addAll(converter.convertFrom(file))
}
else if (YamlContractConverter.INSTANCE
.isAccepted(file)) {
contractDescriptors
.addAll(YamlContractConverter.INSTANCE
.convertFrom(file))
}
}
return super.visitFile(path, attrs)
}
})
}
catch (IOException e) {
log.warn("Exception occurred while trying to parse file", e)
}
return contractDescriptors
}
private static ContractConverter contractConverter(File file) {
for (ContractConverter converter : SpringFactoriesLoader
.loadFactories(ContractConverter.class, null)) {
if (converter.isAccepted(file)) {
return converter
}
}
return null
}
private static boolean isContractDescriptor(File file) {
return ContractVerifierDslConverter.INSTANCE.isAccepted(file)
}
}

View File

@@ -14,25 +14,48 @@
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.util
package org.springframework.cloud.contract.verifier.util;
/**
* Represents content type. Used to pick the way bodies are parsed.
*
* @since 1.0.0
*/
enum ContentType {
public enum ContentType {
/**
* application/json.
*/
JSON("application/json"),
/**
* application/xml.
*/
XML("application/xml"),
/**
* text/plain.
*/
TEXT("text/plain"),
/**
* application/x-www-form-urlencoded.
*/
FORM("application/x-www-form-urlencoded"),
// the content-type was defined and we don't want to override it
/**
* The content-type was defined and we don't want to override it.
*/
DEFINED(""),
UNKNOWN("application/octet-stream")
/**
* application/octet-stream.
*/
UNKNOWN("application/octet-stream");
final String mimeType
private final String mimeType;
ContentType(String mimeType) {
this.mimeType = mimeType
this.mimeType = mimeType;
}
public final String getMimeType() {
return mimeType;
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.contract.spec.Contract;
import org.springframework.cloud.contract.spec.ContractConverter;
import org.springframework.cloud.contract.verifier.converter.YamlContractConverter;
import org.springframework.core.io.support.SpringFactoriesLoader;
/**
* Scans through the given directory and converts all files for contract definitions.
*
* @author Marcin Grzejszczak
* @author Anatolii Zhmaiev
* @since 2.1.0
*/
public final class ContractScanner {
private static final Log log = LogFactory.getLog(ContractScanner.class);
private ContractScanner() {
throw new IllegalStateException("Can't instantiate an utility class");
}
/**
* Traverses through the directories, applies converters to files that match them and
* converts the files to {@link Contract}. No additional file filtering takes place.
* @param rootDirectory - directory to traverse through
* @return collection of converted contracts
*/
public static Collection<Contract> collectContractDescriptors(File rootDirectory) {
return collectContractDescriptors(rootDirectory, (file) -> true);
}
/**
* Traverses through the directories, applies converters to files that match them and
* converts the files to {@link Contract}. Filters out files not matching a predicate.
* @param rootDirectory - directory to traverse through
* @param predicate - test applied against a file
* @return collection of converted contracts
*/
public static Collection<Contract> collectContractDescriptors(File rootDirectory,
Predicate<File> predicate) {
try {
return Files.walk(rootDirectory.toPath()).map(Path::toFile)
.filter(file -> !file.isDirectory()).filter(predicate)
.map(ContractScanner::doCollectContractDescriptors)
.flatMap(Collection::stream).collect(Collectors.toList());
}
catch (IOException e) {
log.warn("Exception occurred while trying to parse file", e);
return Collections.emptyList();
}
}
private static Collection<Contract> doCollectContractDescriptors(File file) {
if (isContractDescriptor(file)) {
return ContractVerifierDslConverter.convertAsCollection(file.getParentFile(),
file);
}
ContractConverter<?> converter = contractConverter(file);
if (converter != null && converter.isAccepted(file)) {
return converter.convertFrom(file);
}
if (YamlContractConverter.INSTANCE.isAccepted(file)) {
return YamlContractConverter.INSTANCE.convertFrom(file);
}
return Collections.emptyList();
}
private static ContractConverter<?> contractConverter(File file) {
return SpringFactoriesLoader.loadFactories(ContractConverter.class, null).stream()
.filter(converter -> converter.isAccepted(file)).findFirst().orElse(null);
}
private static boolean isContractDescriptor(File file) {
return ContractVerifierDslConverter.INSTANCE.isAccepted(file);
}
}

View File

@@ -14,16 +14,22 @@
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.util
import groovy.transform.InheritConstructors
package org.springframework.cloud.contract.verifier.util;
/**
* Exception occurring when we're trying to parse the DSL
* Exception occurring when we're trying to parse the DSL.
*
* @author Marcin Grzejszczak
* @since 1.1.0
*/
@InheritConstructors
class DslParseException extends RuntimeException {
public class DslParseException extends RuntimeException {
public DslParseException(String message) {
super(message);
}
public DslParseException(Throwable cause) {
super(cause);
}
}

View File

@@ -14,16 +14,15 @@
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.util
package org.springframework.cloud.contract.verifier.util;
import groovy.transform.CompileStatic
import java.util.LinkedHashSet;
/**
* Represents a set of Strings - set of method calls to assert a JSON
* Represents a set of Strings - set of method calls to assert a JSON.
*
* @since 1.0.0
*/
@CompileStatic
class JsonPaths extends LinkedHashSet<MethodBufferingJsonVerifiable> {
}
public class JsonPaths extends LinkedHashSet<MethodBufferingJsonVerifiable> {
}