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 |
2
1. getRealPath : negated conditional → NO_COVERAGE 2. getRealPath : negated conditional → KILLED |
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 |
2
1. getRealPath : negated conditional → NO_COVERAGE 2. getRealPath : negated conditional → KILLED |
if (extensions == null) { |
58 | throw new UnknownVariantException("Failed to resolve template due to unknown variant/extension", template.getPath(), template.getContext(), variant); | |
59 | } | |
60 | ||
61 |
2
1. getRealPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getRealPath → NO_COVERAGE 2. getRealPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getRealPath → KILLED |
return resolvePath(template, extensions); |
62 | } | |
63 | ||
64 | @Override | |
65 | public boolean variantExists(TemplateContent template) { | |
66 |
2
1. variantExists : negated conditional → NO_COVERAGE 2. variantExists : negated conditional → KILLED |
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 |
2
1. variantExists : negated conditional → NO_COVERAGE 2. variantExists : negated conditional → KILLED |
if (extensions == null) { |
72 |
2
1. variantExists : replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE 2. variantExists : replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → KILLED |
return false; |
73 | } | |
74 | ||
75 |
4
1. variantExists : replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → NO_COVERAGE 2. variantExists : negated conditional → NO_COVERAGE 3. variantExists : replaced boolean return with true for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::variantExists → KILLED 4. variantExists : negated conditional → KILLED |
return resolvePath(template, extensions) != null; |
76 | } | |
77 | ||
78 | @Override | |
79 | public List<ResourcePath> getPossiblePaths(TemplateContent template) { | |
80 |
2
1. getPossiblePaths : negated conditional → NO_COVERAGE 2. getPossiblePaths : negated conditional → KILLED |
if (!(template instanceof HasVariant)) { |
81 | return emptyList(); | |
82 | } | |
83 | ||
84 | Variant variant = ((HasVariant) template).getVariant(); | |
85 | List<String> extensions = mapping.get(variant); | |
86 |
2
1. getPossiblePaths : negated conditional → NO_COVERAGE 2. getPossiblePaths : negated conditional → KILLED |
if (extensions == null) { |
87 | return emptyList(); | |
88 | } | |
89 | ||
90 | // @formatter:off | |
91 |
2
1. getPossiblePaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getPossiblePaths → NO_COVERAGE 2. getPossiblePaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::getPossiblePaths → KILLED |
return extensions.stream() |
92 |
2
1. lambda$getPossiblePaths$0 : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$getPossiblePaths$0 → NO_COVERAGE 2. lambda$getPossiblePaths$0 : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$getPossiblePaths$0 → KILLED |
.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 |
4
1. lambda$register$1 : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$register$1 → NO_COVERAGE 2. lambda$register$1 : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$register$1 → KILLED 3. lambda$register$1 : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$register$1 → KILLED 4. lambda$register$1 : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::lambda$register$1 → KILLED |
List<String> extensions = mapping.computeIfAbsent(variant, k -> new ArrayList<>()); |
114 |
3
1. register : negated conditional → SURVIVED 2. register : negated conditional → NO_COVERAGE 3. register : negated conditional → KILLED |
String normalized = extension.startsWith(".") ? extension : ("." + extension); |
115 | extensions.add(normalized); | |
116 |
2
1. register : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::register → NO_COVERAGE 2. register : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::register → SURVIVED |
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 |
2
1. resolvePath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolvePath → NO_COVERAGE 2. resolvePath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolvePath → KILLED |
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 |
2
1. resolveVariantPath : negated conditional → NO_COVERAGE 2. resolveVariantPath : negated conditional → KILLED |
if (path != null) { |
138 |
2
1. resolveVariantPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → NO_COVERAGE 2. resolveVariantPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → KILLED |
return path; |
139 | } | |
140 |
2
1. resolveVariantPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → NO_COVERAGE 2. resolveVariantPath : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::resolveVariantPath → KILLED |
return resourceResolver.resolve(new UnresolvedPath(originalPath + extension)); |
141 | } | |
142 | ||
143 | private ResolvedPath useExplicitExtensionIfSameAsVariant(String originalPath, String extension) { | |
144 |
2
1. useExplicitExtensionIfSameAsVariant : negated conditional → NO_COVERAGE 2. useExplicitExtensionIfSameAsVariant : negated conditional → KILLED |
if (originalPath.endsWith(extension)) { |
145 |
2
1. useExplicitExtensionIfSameAsVariant : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::useExplicitExtensionIfSameAsVariant → NO_COVERAGE 2. useExplicitExtensionIfSameAsVariant : replaced return value with null for fr/sii/ogham/template/common/adapter/ExtensionMappingVariantResolver::useExplicitExtensionIfSameAsVariant → KILLED |
return resourceResolver.resolve(new UnresolvedPath(originalPath)); |
146 | } | |
147 | return null; | |
148 | } | |
149 | ||
150 | } | |
Mutations | ||
51 |
1.1 2.2 |
|
52 |
1.1 |
|
57 |
1.1 2.2 |
|
61 |
1.1 2.2 |
|
66 |
1.1 2.2 |
|
67 |
1.1 |
|
71 |
1.1 2.2 |
|
72 |
1.1 2.2 |
|
75 |
1.1 2.2 3.3 4.4 |
|
80 |
1.1 2.2 |
|
86 |
1.1 2.2 |
|
91 |
1.1 2.2 |
|
92 |
1.1 2.2 |
|
113 |
1.1 2.2 3.3 4.4 |
|
114 |
1.1 2.2 3.3 |
|
116 |
1.1 2.2 |
|
125 |
1.1 2.2 |
|
137 |
1.1 2.2 |
|
138 |
1.1 2.2 |
|
140 |
1.1 2.2 |
|
144 |
1.1 2.2 |
|
145 |
1.1 2.2 |