Skip to content

Commit d938b36

Browse files
committed
fix wrong validation message for deep module properties
LMCROSSITXSADEPLOY-268
1 parent 086ec14 commit d938b36

File tree

5 files changed

+103
-82
lines changed

5 files changed

+103
-82
lines changed

multiapps-mta/src/main/java/org/cloudfoundry/multiapps/mta/resolvers/PropertiesResolver.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
public class PropertiesResolver implements SimplePropertyVisitor, Resolver<Map<String, Object>> {
2323

24+
private static final String FULL_PATH_TEMPLATE = "%s%s%s";
25+
2426
private Map<String, Object> properties;
2527
private String prefix;
2628
private ProvidedValuesResolver valuesResolver;
@@ -150,7 +152,9 @@ private boolean referenceResolutionIsPossible(String referenceKey, Map<String, O
150152
private Object getObjectForLateResolveIfPresent(Reference reference) {
151153
String referenceKey = reference.getKey();
152154
if (isStrict && !dynamicResolvableParameters.contains(referenceKey)) {
153-
throw new ContentException(Messages.UNABLE_TO_RESOLVE, NameUtil.getPrefixedName(prefix, referenceKey));
155+
throw new ContentException(Messages.UNABLE_TO_RESOLVE,
156+
NameUtil.getPrefixedPath(prefix, buildFullQualifiedPath(reference)));
157+
154158
}
155159
if (dynamicResolvableParameters.contains(referenceKey) && prefix != null) {
156160
return MessageFormat.format(DynamicParameterUtil.PATTERN_FOR_DYNAMIC_PARAMETERS, prefix, reference.getKey());
@@ -174,7 +178,7 @@ protected Object resolveReferenceInDepth(Reference reference, Map<String, Object
174178
keyPart = addOldKeyAsPrefixIfUnresolved(keyPart, referencePartsMatcher.group(1));
175179

176180
if (currentProperty instanceof Collection) {
177-
currentProperty = resolveKeyInIterable(keyPart, (Collection<?>) currentProperty, deepReferenceKey);
181+
currentProperty = resolveKeyInIterable(reference, (Collection<?>) currentProperty, keyPart);
178182
keyPart = "";
179183
} else if (currentProperty instanceof Map) {
180184
@SuppressWarnings("unchecked")
@@ -184,12 +188,16 @@ protected Object resolveReferenceInDepth(Reference reference, Map<String, Object
184188
keyPart = "";
185189
}
186190
} else {
187-
throw new ContentException(Messages.UNABLE_TO_RESOLVE, NameUtil.getPrefixedName(prefix, deepReferenceKey));
191+
throw new ContentException(Messages.UNABLE_TO_RESOLVE,
192+
NameUtil.getPrefixedPath(prefix, buildFullQualifiedPath(reference)));
193+
188194
}
189195
}
190196

191197
if (!keyPart.isEmpty()) {
192-
throw new ContentException(Messages.UNABLE_TO_RESOLVE, NameUtil.getPrefixedName(prefix, deepReferenceKey));
198+
throw new ContentException(Messages.UNABLE_TO_RESOLVE,
199+
NameUtil.getPrefixedPath(prefix, buildFullQualifiedPath(reference)));
200+
193201
}
194202

195203
return currentProperty;
@@ -202,15 +210,22 @@ private String addOldKeyAsPrefixIfUnresolved(String oldKey, String newKey) {
202210
return oldKey + "/" + newKey;
203211
}
204212

205-
private Object resolveKeyInIterable(String key, Collection<?> listOfProperties, String longKey) {
206-
if (StringUtils.isNumeric(key)) {
213+
private Object resolveKeyInIterable(Reference reference, Collection<?> listOfProperties, String longKey) {
214+
if (StringUtils.isNumeric(longKey)) {
207215
try {
208-
return IterableUtils.get(listOfProperties, Integer.parseInt(key));
216+
return IterableUtils.get(listOfProperties, Integer.parseInt(longKey));
209217
} catch (IndexOutOfBoundsException e) {
210-
throw new ContentException(e, Messages.UNABLE_TO_RESOLVE, NameUtil.getPrefixedName(prefix, longKey));
218+
throw new ContentException(e, Messages.UNABLE_TO_RESOLVE, buildFullQualifiedPath(reference));
211219
}
212220
}
213-
throw new ContentException(Messages.UNABLE_TO_RESOLVE, NameUtil.getPrefixedName(prefix, longKey));
221+
throw new ContentException(Messages.UNABLE_TO_RESOLVE, buildFullQualifiedPath(reference));
222+
}
223+
224+
private String buildFullQualifiedPath(Reference reference) {
225+
String raw = reference.getDependencyName() != null
226+
? String.format(FULL_PATH_TEMPLATE, reference.getDependencyName(), NameUtil.DEFAULT_PREFIX_SEPARATOR, reference.getKey())
227+
: reference.getKey();
228+
return NameUtil.getPrefixedPath(prefix, raw);
214229
}
215230

216231
private String getReferencedPropertyKeyWithSuffix(Reference reference) {

multiapps-mta/src/main/java/org/cloudfoundry/multiapps/mta/util/NameUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ public static String getPrefixedName(String prefix, String name, String separato
2020
return prefix + separator + name;
2121
}
2222

23+
public static String getPrefixedPath(String prefix, String path) {
24+
if (StringUtils.isEmpty(prefix) || path.startsWith(prefix + DEFAULT_PREFIX_SEPARATOR)) {
25+
return path;
26+
}
27+
return prefix + DEFAULT_PREFIX_SEPARATOR + path;
28+
}
2329
}

multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/resolvers/PropertiesResolverTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ void testResolve(String parameterName, String parameterValue, Expectation expect
5151
tester.test(() -> testResolver.visit(parameterName, parameterValue), expectation);
5252
}
5353

54-
}
54+
}

multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/resolvers/v2/DescriptorPlaceholderResolverTest.java

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,66 @@ class DescriptorPlaceholderResolverTest {
2020

2121
static Stream<Arguments> testResolve() {
2222
return Stream.of(
23-
// (00)
24-
Arguments.of("mtad-with-placeholders-in-requires-dependency.yaml",
25-
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-requires-dependency.json")),
26-
// (01)
27-
Arguments.of("mtad-with-placeholders-in-provides-dependency.yaml",
28-
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-provides-dependency.json")),
29-
// (02)
30-
Arguments.of("mtad-with-placeholders-in-resource.yaml",
31-
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-resource.json")),
32-
// (03)
33-
Arguments.of("mtad-with-placeholders-in-module.yaml",
34-
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-module.json")),
35-
// (04)
36-
Arguments.of("mtad-with-placeholders-in-general-parameters.yaml",
37-
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-general-parameters.json")),
38-
// (05)
39-
Arguments.of("mtad-with-concatenated-placeholders.yaml",
40-
new Expectation(Expectation.Type.JSON, "result-from-concatenated-parameters.json")),
41-
// (06)
42-
Arguments.of("mtad-with-unresolvable-requires-dependency-properties.yaml",
43-
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#bar#non-existing\"")),
44-
// (07)
45-
Arguments.of("mtad-with-unresolvable-requires-dependency-parameters.yaml",
46-
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#bar#non-existing\"")),
47-
// (08)
48-
Arguments.of("mtad-with-unresolvable-general-parameters.yaml",
49-
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"non-existing\"")),
50-
// (09)
51-
Arguments.of("mtad-with-unresolvable-module-properties.yaml",
52-
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#non-existing\"")),
53-
// (10)
54-
Arguments.of("mtad-with-unresolvable-module-parameters.yaml",
55-
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#non-existing\"")),
56-
// (11)
57-
Arguments.of("mtad-preservation-of-types.yaml",
58-
new Expectation(Expectation.Type.JSON, "result-preservation-of-types.json")),
59-
// (12)
60-
Arguments.of("mtad-with-placeholder-in-a-nested-structure.yaml",
61-
new Expectation(Expectation.Type.JSON, "result-from-placeholder-in-a-nested-structure.json")),
62-
// (13)
63-
Arguments.of("mtad-with-circular-reference-in-requires-dependency.yaml",
64-
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"foo#bar#a\"")),
65-
// (14)
66-
Arguments.of("mtad-with-circular-reference-in-resource.yaml",
67-
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"bar#a\"")),
68-
// (15)
69-
Arguments.of("mtad-with-circular-reference-in-module.yaml",
70-
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"foo#a\"")),
71-
// (16)
72-
Arguments.of("mtad-with-complex-circular-reference.yaml",
73-
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"a\"")),
74-
// (17)
75-
Arguments.of("mtad-with-repeating-placeholder.yaml",
76-
new Expectation(Expectation.Type.JSON, "result-from-repeating-placeholder.json")),
77-
// (18)
78-
Arguments.of("mtad-with-placeholders-with-depth.yaml",
79-
new Expectation(Expectation.Type.JSON, "result-from-placeholders-with-depth.json")),
80-
// (19)
81-
Arguments.of("mtad-with-escaped-placeholders.yaml",
82-
new Expectation(Expectation.Type.JSON, "result-from-escaped-placeholders.json")));
23+
// (00)
24+
Arguments.of("mtad-with-placeholders-in-requires-dependency.yaml",
25+
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-requires-dependency.json")),
26+
// (01)
27+
Arguments.of("mtad-with-placeholders-in-provides-dependency.yaml",
28+
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-provides-dependency.json")),
29+
// (02)
30+
Arguments.of("mtad-with-placeholders-in-resource.yaml",
31+
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-resource.json")),
32+
// (03)
33+
Arguments.of("mtad-with-placeholders-in-module.yaml",
34+
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-module.json")),
35+
// (04)
36+
Arguments.of("mtad-with-placeholders-in-general-parameters.yaml",
37+
new Expectation(Expectation.Type.JSON, "result-from-placeholders-in-general-parameters.json")),
38+
// (05)
39+
Arguments.of("mtad-with-concatenated-placeholders.yaml",
40+
new Expectation(Expectation.Type.JSON, "result-from-concatenated-parameters.json")),
41+
// (06)
42+
Arguments.of("mtad-with-unresolvable-requires-dependency-properties.yaml",
43+
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#bar#non-existing\"")),
44+
// (07)
45+
Arguments.of("mtad-with-unresolvable-requires-dependency-parameters.yaml",
46+
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#bar#non-existing\"")),
47+
// (08)
48+
Arguments.of("mtad-with-unresolvable-general-parameters.yaml",
49+
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"non-existing\"")),
50+
// (09)
51+
Arguments.of("mtad-with-unresolvable-module-properties.yaml",
52+
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#non-existing\"")),
53+
// (10)
54+
Arguments.of("mtad-with-unresolvable-module-parameters.yaml",
55+
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"foo#non-existing\"")),
56+
// (11)
57+
Arguments.of("mtad-preservation-of-types.yaml",
58+
new Expectation(Expectation.Type.JSON, "result-preservation-of-types.json")),
59+
// (12)
60+
Arguments.of("mtad-with-placeholder-in-a-nested-structure.yaml",
61+
new Expectation(Expectation.Type.JSON, "result-from-placeholder-in-a-nested-structure.json")),
62+
// (13)
63+
Arguments.of("mtad-with-circular-reference-in-requires-dependency.yaml",
64+
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"foo#bar#a\"")),
65+
// (14)
66+
Arguments.of("mtad-with-circular-reference-in-resource.yaml",
67+
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"bar#a\"")),
68+
// (15)
69+
Arguments.of("mtad-with-circular-reference-in-module.yaml",
70+
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"foo#a\"")),
71+
// (16)
72+
Arguments.of("mtad-with-complex-circular-reference.yaml",
73+
new Expectation(Expectation.Type.EXCEPTION, "Circular reference detected in \"a\"")),
74+
// (17)
75+
Arguments.of("mtad-with-repeating-placeholder.yaml",
76+
new Expectation(Expectation.Type.JSON, "result-from-repeating-placeholder.json")),
77+
// (18)
78+
Arguments.of("mtad-with-placeholders-with-depth.yaml",
79+
new Expectation(Expectation.Type.JSON, "result-from-placeholders-with-depth.json")),
80+
// (19)
81+
Arguments.of("mtad-with-escaped-placeholders.yaml",
82+
new Expectation(Expectation.Type.JSON, "result-from-escaped-placeholders.json")));
8383
}
8484

8585
@ParameterizedTest

multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/resolvers/v3/DescriptorReferenceResolverTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ class DescriptorReferenceResolverTest {
2121

2222
static Stream<Arguments> testResolve() {
2323
return Stream.of(
24-
// (0) Resolve references in resources:
25-
Arguments.of("merged-01.yaml", new Expectation(Expectation.Type.JSON, "resolved-01.yaml.json")),
26-
// (1) Resolve references in resources - cyclic dependencies & corner cases:
27-
Arguments.of("merged-02.yaml", new Expectation(Expectation.Type.JSON, "resolved-02.yaml.json")),
28-
// (2) Test error reporting on failure to resolve value:
29-
Arguments.of("merged-03.yaml",
30-
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"baz##non-existing\"")),
31-
// (3) Resolve references in hooks:
32-
Arguments.of("merged-04.yaml", new Expectation(Expectation.Type.JSON, "resolved-03.yaml.json")),
33-
// (4)
34-
Arguments.of("mtad-with-escaped-references.yaml",
35-
new Expectation(Expectation.Type.JSON, "result-from-escaped-references.json")));
24+
// (0) Resolve references in resources:
25+
Arguments.of("merged-01.yaml", new Expectation(Expectation.Type.JSON, "resolved-01.yaml.json")),
26+
// (1) Resolve references in resources - cyclic dependencies & corner cases:
27+
Arguments.of("merged-02.yaml", new Expectation(Expectation.Type.JSON, "resolved-02.yaml.json")),
28+
// (2) Test error reporting on failure to resolve value:
29+
Arguments.of("merged-03.yaml",
30+
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"baz##bar#non-existing\"")),
31+
// (3) Resolve references in hooks:
32+
Arguments.of("merged-04.yaml", new Expectation(Expectation.Type.JSON, "resolved-03.yaml.json")),
33+
// (4)
34+
Arguments.of("mtad-with-escaped-references.yaml",
35+
new Expectation(Expectation.Type.JSON, "result-from-escaped-references.json")));
3636
}
3737

3838
@ParameterizedTest

0 commit comments

Comments
 (0)