Save point: [skip ci]

* Testing the controller
This commit is contained in:
alandavis
2022-07-18 11:15:31 +01:00
parent e5728d5d44
commit d312a31c21
30 changed files with 541 additions and 222 deletions

View File

@@ -80,6 +80,9 @@ public interface RequestParamMap
String ENDPOINT_TEST = "/test";
String ENDPOINT_TRANSFORM_CONFIG = "/transform/config";
String ENDPOINT_TRANSFORM_CONFIG_LATEST = ENDPOINT_TRANSFORM_CONFIG + "?" + CONFIG_VERSION + "=" + CONFIG_VERSION_LATEST;
String ENDPOINT_VERSION = "/version";
String ENDPOINT_READY = "/ready";
String ENDPOINT_LIVE = "/live";
String ENDPOINT_ERROR = "/error";
String ENDPOINT_LOG = "/log";
String ENDPOINT_ROOT = "/";

View File

@@ -52,6 +52,11 @@ public enum CoreFunction
return comparableVersion.compareTo(fromVersion) >= 0 && comparableVersion.compareTo(toVersion) <= 0;
}
public static String standardizeCoreVersion(String version)
{
return newComparableVersion(version, NO_VERSION).toString();
}
CoreFunction(String fromVersion, String toVersion)
{
this.fromVersion = newComparableVersion(fromVersion, NO_VERSION);

View File

@@ -67,14 +67,15 @@ public class CoreVersionDecorator
Set.of(new TransformOptionValue(false, DIRECT_ACCESS_URL));
/**
* Returns a {@link TransformConfig} that includes or excludes the {@code coreVersion} field.
* Returns a new {@link TransformConfig} that includes or excludes the {@code coreVersion} field and
* associated elements like directAccessUrl.
*/
public static TransformConfig setOrClearCoreVersion(TransformConfig transformConfig, int configVersion)
{
boolean includeCoreVersion = configVersion >= 2;
Map<String, Set<TransformOption>> transformOptions = new HashMap<>(transformConfig.getTransformOptions());
return TransformConfig.builder()
transformConfig = TransformConfig.builder()
// We may need to create new Transformers as we must not change the original.
.withTransformers(transformConfig.getTransformers().stream()
.map(transformer -> {
@@ -85,7 +86,7 @@ public class CoreVersionDecorator
.withCoreVersion(includeCoreVersion ? transformer.getCoreVersion() : null)
.withTransformOptions(setOrClearCoreTransformOptions(
includeCoreVersion ? transformer.getCoreVersion() : null,
transformOptions, transformer.getTransformOptions()))
transformer.getTransformOptions()))
// Original values
.withTransformerName(transformer.getTransformerName())
.withTransformerPipeline(transformer.getTransformerPipeline())
@@ -104,19 +105,20 @@ public class CoreVersionDecorator
.withOverrideSupported(transformConfig.getOverrideSupported())
.withSupportedDefaults(transformConfig.getSupportedDefaults())
.build();
addOrRemoveDirectAccessUrlOption(transformConfig.getTransformOptions(), transformConfig.getTransformers());
return transformConfig;
}
public static void setCoreVersionOnSingleStepTransformers(TransformConfig transformConfig, String coreVersion)
{
Map<String, Set<TransformOption>> transformOptions = transformConfig.getTransformOptions();
List<Transformer> transformers = transformConfig.getTransformers();
transformers.stream()
.filter(CoreVersionDecorator::isSingleStep)
.forEach(transformer -> {
.filter(CoreVersionDecorator::isSingleStep)
.forEach(transformer -> {
transformer.setCoreVersion(coreVersion);
transformer.setTransformOptions(setOrClearCoreTransformOptions(coreVersion,
transformOptions, transformer.getTransformOptions()));
transformer.setTransformOptions(setOrClearCoreTransformOptions(coreVersion, transformer.getTransformOptions()));
});
addOrRemoveDirectAccessUrlOption(transformConfig.getTransformOptions(), transformers);
}
/**
@@ -147,33 +149,43 @@ public class CoreVersionDecorator
String coreVersion = NO_VERSION.equals(minCoreVersion) ? null : minCoreVersion.toString();
transformer.setCoreVersion(coreVersion);
transformer.setTransformOptions(setOrClearCoreTransformOptions(transformer.getCoreVersion(),
transformOptions, transformer.getTransformOptions()));
transformer.getTransformOptions()));
});
addOrRemoveDirectAccessUrlOption(transformOptions, transformers);
}
private static Set<String> setOrClearCoreTransformOptions(String coreVersion, Map<String,
Set<TransformOption>> transformOptions, Set<String> transformerTransformOptions)
private static Set<String> setOrClearCoreTransformOptions(String coreVersion, Set<String> transformerTransformOptions)
{
// If we have more options being added in future, consider adding an interface that will be implemented by
// If we have more options being added in the future, consider adding an interface that will be implemented by
// different implementations for each coreVersion and then iterate over them here.
transformerTransformOptions = new HashSet<>(transformerTransformOptions);
if (CoreFunction.DIRECT_ACCESS_URL.isSupported(coreVersion))
{
// Added to the Transform config if any Transformers support it.
transformOptions.put(DIRECT_ACCESS_URL, DIRECT_ACCESS_URL_TRANSFORM_OPTIONS);
// Add DIRECT_ACCESS_URL to a copy of this Transformer's transform options.
transformerTransformOptions.add(DIRECT_ACCESS_URL);
}
else
{
transformOptions.remove(DIRECT_ACCESS_URL);
transformerTransformOptions.remove(DIRECT_ACCESS_URL);
}
return transformerTransformOptions;
}
private static void addOrRemoveDirectAccessUrlOption(Map<String, Set<TransformOption>> transformOptions,
List<Transformer> transformers)
{
if (transformers.stream()
.anyMatch(transformer -> CoreFunction.DIRECT_ACCESS_URL.isSupported(transformer.getCoreVersion())))
{
transformOptions.put(DIRECT_ACCESS_URL, DIRECT_ACCESS_URL_TRANSFORM_OPTIONS);
}
else
{
transformOptions.remove(DIRECT_ACCESS_URL);
}
}
private static boolean isSingleStep(Transformer transformer)
{
return (transformer.getTransformerFailover() == null || transformer.getTransformerFailover().isEmpty()) &&

View File

@@ -56,7 +56,7 @@ public class TransformConfig
public void setTransformOptions(Map<String, Set<TransformOption>> transformOptions)
{
this.transformOptions = transformOptions;
this.transformOptions = new HashMap<>(transformOptions);
}
public List<Transformer> getTransformers()
@@ -143,7 +143,7 @@ public class TransformConfig
public Builder withTransformOptions(final Map<String, Set<TransformOption>> transformOptions)
{
transformConfig.transformOptions = transformOptions;
transformConfig.setTransformOptions(transformOptions);
return this;
}

View File

@@ -212,7 +212,7 @@ public class Transformer
public Builder withCoreVersion(final String coreVersion)
{
transformer.coreVersion = coreVersion;
transformer.setCoreVersion(coreVersion);
return this;
}

View File

@@ -32,6 +32,7 @@ import java.util.Set;
import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_DEFAULT;
import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL;
import static org.alfresco.transform.config.CoreFunction.standardizeCoreVersion;
import static org.alfresco.transform.config.CoreVersionDecorator.CONFIG_VERSION_INCLUDES_CORE_VERSION;
import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnMultiStepTransformers;
import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers;
@@ -213,4 +214,13 @@ class CoreVersionDecoratorTest
true, true),
setOrClearCoreVersion(transformConfigWithCoreVersion, CONFIG_VERSION_INCLUDES_CORE_VERSION));
}
@Test
void standardizeCoreVersionTest()
{
assertEquals("2.5.7", standardizeCoreVersion("2.5.7"));
assertEquals("2.5.7", standardizeCoreVersion("2.5.7-SNAPSHOT"));
assertEquals("2", standardizeCoreVersion("2"));
assertEquals("2.5.7", standardizeCoreVersion("2.5.7-A-SNAPSHOT"));
}
}