| 1 | package fr.sii.ogham.template.common.adapter; | |
| 2 | ||
| 3 | import static java.util.Arrays.asList; | |
| 4 | import static java.util.Collections.emptyList; | |
| 5 | ||
| 6 | import java.util.ArrayList; | |
| 7 | import java.util.List; | |
| 8 | ||
| 9 | import fr.sii.ogham.core.message.capability.HasVariant; | |
| 10 | import fr.sii.ogham.core.message.content.TemplateContent; | |
| 11 | import fr.sii.ogham.core.message.content.Variant; | |
| 12 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
| 13 | import fr.sii.ogham.template.exception.TemplateVariantNotFoundException; | |
| 14 | import fr.sii.ogham.template.exception.VariantResolutionException; | |
| 15 | ||
| 16 | /** | |
| 17 | * Improved implementation that fails but also provide more information to the | |
| 18 | * end-user. | |
| 19 | * | |
| 20 | * @author Aurélien Baudet | |
| 21 | * | |
| 22 | */ | |
| 23 | public class FailIfNotFoundWithTestedPathsVariantResolver implements VariantResolver { | |
| 24 | /** | |
| 25 | * The list of previously used resolvers to retrieve the paths that didn't | |
| 26 | * match | |
| 27 | */ | |
| 28 | private List<VariantResolver> delegates; | |
| 29 | ||
| 30 | public FailIfNotFoundWithTestedPathsVariantResolver() { | |
| 31 | this(new ArrayList<>()); | |
| 32 | } | |
| 33 | ||
| 34 | public FailIfNotFoundWithTestedPathsVariantResolver(List<VariantResolver> delegates) { | |
| 35 | super(); | |
| 36 | this.delegates = delegates; | |
| 37 | } | |
| 38 | ||
| 39 | @Override | |
| 40 | public ResourcePath getRealPath(TemplateContent template) throws VariantResolutionException { | |
| 41 |
2
1. getRealPath : negated conditional → NO_COVERAGE 2. getRealPath : negated conditional → KILLED |
if (!(template instanceof HasVariant)) { |
| 42 |
1
1. getRealPath : replaced return value with null for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::getRealPath → NO_COVERAGE |
return template.getPath(); |
| 43 | } | |
| 44 | List<ResourcePath> testedPaths = getTestedPaths(template); | |
| 45 | Variant variant = ((HasVariant) template).getVariant(); | |
| 46 | throw new TemplateVariantNotFoundException("Failed to resolve variant (" + variant + ") for template " + template.getPath().getOriginalPath(), template, testedPaths); | |
| 47 | } | |
| 48 | ||
| 49 | private List<ResourcePath> getTestedPaths(TemplateContent template) { | |
| 50 | List<ResourcePath> testedPaths = new ArrayList<>(); | |
| 51 | for (VariantResolver delegate : delegates) { | |
| 52 | testedPaths.addAll(getTestedPaths(template, delegate)); | |
| 53 | } | |
| 54 |
2
1. getTestedPaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::getTestedPaths → NO_COVERAGE 2. getTestedPaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::getTestedPaths → KILLED |
return testedPaths; |
| 55 | } | |
| 56 | ||
| 57 | private List<ResourcePath> getTestedPaths(TemplateContent template, VariantResolver delegate) { | |
| 58 | // if information an be provided, use that information | |
| 59 |
2
1. getTestedPaths : negated conditional → NO_COVERAGE 2. getTestedPaths : negated conditional → KILLED |
if (delegate instanceof CanProvidePossiblePaths) { |
| 60 |
2
1. getTestedPaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::getTestedPaths → NO_COVERAGE 2. getTestedPaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::getTestedPaths → KILLED |
return ((CanProvidePossiblePaths) delegate).getPossiblePaths(template); |
| 61 | } | |
| 62 | // otherwise call again to give a path if possible | |
| 63 | ResourcePath testedPath = null; | |
| 64 | try { | |
| 65 | testedPath = delegate.getRealPath(template); | |
| 66 | } catch (VariantResolutionException e) { | |
| 67 | // skip because there is no useful information | |
| 68 | } | |
| 69 |
2
1. getTestedPaths : replaced return value with Collections.emptyList for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::getTestedPaths → NO_COVERAGE 2. getTestedPaths : negated conditional → NO_COVERAGE |
return testedPath==null ? emptyList() : asList(testedPath); |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Returns true to make it fail | |
| 74 | */ | |
| 75 | @Override | |
| 76 | public boolean variantExists(TemplateContent template) { | |
| 77 |
1
1. variantExists : replaced boolean return with false for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::variantExists → NO_COVERAGE |
return true; |
| 78 | } | |
| 79 | ||
| 80 | public FailIfNotFoundWithTestedPathsVariantResolver addVariantResolver(VariantResolver variantResolver) { | |
| 81 | delegates.add(variantResolver); | |
| 82 |
2
1. addVariantResolver : replaced return value with null for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::addVariantResolver → SURVIVED 2. addVariantResolver : replaced return value with null for fr/sii/ogham/template/common/adapter/FailIfNotFoundWithTestedPathsVariantResolver::addVariantResolver → NO_COVERAGE |
return this; |
| 83 | } | |
| 84 | ||
| 85 | } | |
Mutations | ||
| 41 |
1.1 2.2 |
|
| 42 |
1.1 |
|
| 54 |
1.1 2.2 |
|
| 59 |
1.1 2.2 |
|
| 60 |
1.1 2.2 |
|
| 69 |
1.1 2.2 |
|
| 77 |
1.1 |
|
| 82 |
1.1 2.2 |