ExtensionMappingVariantResolver.java

1
package fr.sii.ogham.template.common.adapter;
2
3
import static java.util.Collections.emptyList;
4
import static java.util.stream.Collectors.toList;
5
6
import java.util.ArrayList;
7
import java.util.HashMap;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.Objects;
11
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14
15
import fr.sii.ogham.core.exception.resource.ResourceResolutionException;
16
import fr.sii.ogham.core.message.capability.HasVariant;
17
import fr.sii.ogham.core.message.content.TemplateContent;
18
import fr.sii.ogham.core.message.content.Variant;
19
import fr.sii.ogham.core.resource.path.ResolvedPath;
20
import fr.sii.ogham.core.resource.path.ResourcePath;
21
import fr.sii.ogham.core.resource.path.UnresolvedPath;
22
import fr.sii.ogham.core.resource.resolver.ResourceResolver;
23
import fr.sii.ogham.template.exception.UnknownVariantException;
24
import fr.sii.ogham.template.exception.VariantResolutionException;
25
26
/**
27
 * Simple implementation that maps a variant instance to an extension.
28
 * 
29
 * @author Aurélien Baudet
30
 *
31
 */
32
public class ExtensionMappingVariantResolver implements VariantResolver, CanProvidePossiblePaths {
33
	private static final Logger LOG = LoggerFactory.getLogger(ExtensionMappingVariantResolver.class);
34
35
	private final ResourceResolver resourceResolver;
36
	private final Map<Variant, List<String>> mapping;
37
38
	public ExtensionMappingVariantResolver(ResourceResolver resourceResolver) {
39
		this(resourceResolver, new HashMap<>());
40
	}
41
42
	public ExtensionMappingVariantResolver(ResourceResolver resourceResolver, Map<Variant, List<String>> mapping) {
43
		super();
44
		this.resourceResolver = resourceResolver;
45
		this.mapping = mapping;
46
	}
47
48
	@Override
49
	public ResourcePath getRealPath(TemplateContent template) throws VariantResolutionException {
50
		String originalPath = template.getPath().getOriginalPath();
51 1 1. getRealPath : negated conditional → NO_COVERAGE
		if (!(template instanceof HasVariant)) {
52 1 1. getRealPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getRealPath → NO_COVERAGE
			return resourceResolver.resolve(new UnresolvedPath(originalPath));
53
		}
54
55
		Variant variant = ((HasVariant) template).getVariant();
56
		List<String> extensions = mapping.get(variant);
57 1 1. getRealPath : negated conditional → NO_COVERAGE
		if (extensions == null) {
58
			throw new UnknownVariantException("Failed to resolve template due to unknown variant/extension", template.getPath(), template.getContext(), variant);
59
		}
60
61 1 1. getRealPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getRealPath → NO_COVERAGE
		return resolvePath(template, extensions);
62
	}
63
64
	@Override
65
	public boolean variantExists(TemplateContent template) {
66 1 1. variantExists : negated conditional → NO_COVERAGE
		if (!(template instanceof HasVariant)) {
67 1 1. variantExists : replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE
			return false;
68
		}
69
70
		List<String> extensions = mapping.get(((HasVariant) template).getVariant());
71 1 1. variantExists : negated conditional → NO_COVERAGE
		if (extensions == null) {
72 1 1. variantExists : replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE
			return false;
73
		}
74
75 2 1. variantExists : negated conditional → NO_COVERAGE
2. variantExists : replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE
		return resolvePath(template, extensions) != null;
76
	}
77
78
	@Override
79
	public List<ResourcePath> getPossiblePaths(TemplateContent template) {
80 1 1. getPossiblePaths : negated conditional → NO_COVERAGE
		if (!(template instanceof HasVariant)) {
81
			return emptyList();
82
		}
83
84
		Variant variant = ((HasVariant) template).getVariant();
85
		List<String> extensions = mapping.get(variant);
86 1 1. getPossiblePaths : negated conditional → NO_COVERAGE
		if (extensions == null) {
87
			return emptyList();
88
		}
89
90
		// @formatter:off
91 1 1. getPossiblePaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getPossiblePaths → NO_COVERAGE
		return extensions.stream()
92 1 1. lambda$getPossiblePaths$0 : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$getPossiblePaths$0 → NO_COVERAGE
				.map(extension -> resolveVariantPath(template.getPath().getOriginalPath(), extension))
93
				.filter(Objects::nonNull)
94
				.collect(toList());
95
		// @formatter:on
96
	}
97
98
	/**
99
	 * Register a mapping between a variant and an extension.
100
	 * 
101
	 * <p>
102
	 * If a variant is already registered, the new extension is appended to the
103
	 * list of extensions associated to the variant. The registration order of
104
	 * extensions for a variant is important.
105
	 * 
106
	 * @param variant
107
	 *            the variant
108
	 * @param extension
109
	 *            the extension to associate with the variant
110
	 * @return this instance for fluent chaining
111
	 */
112
	public ExtensionMappingVariantResolver register(Variant variant, String extension) {
113 1 1. lambda$register$1 : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$register$1 → NO_COVERAGE
		List<String> extensions = mapping.computeIfAbsent(variant, k -> new ArrayList<>());
114 1 1. register : negated conditional → NO_COVERAGE
		String normalized = extension.startsWith(".") ? extension : ("." + extension);
115
		extensions.add(normalized);
116 1 1. register : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::register → NO_COVERAGE
		return this;
117
	}
118
119
	private ResourcePath resolvePath(TemplateContent template, List<String> extensions) {
120
		ResourcePath templatePath = template.getPath();
121
		for (String extension : extensions) {
122
			try {
123
				ResourcePath path = resolveVariantPath(template.getPath().getOriginalPath(), extension);
124
				resourceResolver.getResource(path);
125 1 1. resolvePath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolvePath → NO_COVERAGE
				return path;
126
			} catch (ResourceResolutionException e) {
127
				LOG.trace("template {}.{} not found", templatePath, extension, e);
128
			}
129
		}
130
		return null;
131
	}
132
133
	private ResourcePath resolveVariantPath(String originalPath, String extension) {
134
		// if extension already explicitly set, try without adding the extension
135
		// provided by variant
136
		ResolvedPath path = useExplicitExtensionIfSameAsVariant(originalPath, extension);
137 1 1. resolveVariantPath : negated conditional → NO_COVERAGE
		if (path != null) {
138 1 1. resolveVariantPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → NO_COVERAGE
			return path;
139
		}
140 1 1. resolveVariantPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → NO_COVERAGE
		return resourceResolver.resolve(new UnresolvedPath(originalPath + extension));
141
	}
142
143
	private ResolvedPath useExplicitExtensionIfSameAsVariant(String originalPath, String extension) {
144 1 1. useExplicitExtensionIfSameAsVariant : negated conditional → NO_COVERAGE
		if (originalPath.endsWith(extension)) {
145 1 1. useExplicitExtensionIfSameAsVariant : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::useExplicitExtensionIfSameAsVariant → NO_COVERAGE
			return resourceResolver.resolve(new UnresolvedPath(originalPath));
146
		}
147
		return null;
148
	}
149
150
}

Mutations

51

1.1
Location : getRealPath
Killed by :
negated conditional → NO_COVERAGE

52

1.1
Location : getRealPath
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getRealPath → NO_COVERAGE

57

1.1
Location : getRealPath
Killed by :
negated conditional → NO_COVERAGE

61

1.1
Location : getRealPath
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getRealPath → NO_COVERAGE

66

1.1
Location : variantExists
Killed by :
negated conditional → NO_COVERAGE

67

1.1
Location : variantExists
Killed by :
replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE

71

1.1
Location : variantExists
Killed by :
negated conditional → NO_COVERAGE

72

1.1
Location : variantExists
Killed by :
replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE

75

1.1
Location : variantExists
Killed by :
negated conditional → NO_COVERAGE

2.2
Location : variantExists
Killed by :
replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE

80

1.1
Location : getPossiblePaths
Killed by :
negated conditional → NO_COVERAGE

86

1.1
Location : getPossiblePaths
Killed by :
negated conditional → NO_COVERAGE

91

1.1
Location : getPossiblePaths
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getPossiblePaths → NO_COVERAGE

92

1.1
Location : lambda$getPossiblePaths$0
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$getPossiblePaths$0 → NO_COVERAGE

113

1.1
Location : lambda$register$1
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$register$1 → NO_COVERAGE

114

1.1
Location : register
Killed by :
negated conditional → NO_COVERAGE

116

1.1
Location : register
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::register → NO_COVERAGE

125

1.1
Location : resolvePath
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolvePath → NO_COVERAGE

137

1.1
Location : resolveVariantPath
Killed by :
negated conditional → NO_COVERAGE

138

1.1
Location : resolveVariantPath
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → NO_COVERAGE

140

1.1
Location : resolveVariantPath
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → NO_COVERAGE

144

1.1
Location : useExplicitExtensionIfSameAsVariant
Killed by :
negated conditional → NO_COVERAGE

145

1.1
Location : useExplicitExtensionIfSameAsVariant
Killed by :
replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::useExplicitExtensionIfSameAsVariant → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1