Skip to content

Commit 8c3911c

Browse files
stiv03theghost5800
authored andcommitted
fix wrong validation message for deep module properties
LMCROSSITXSADEPLOY-268
1 parent 086ec14 commit 8c3911c

File tree

5 files changed

+103
-28
lines changed

5 files changed

+103
-28
lines changed

multiapps-common-test/src/main/java/org/cloudfoundry/multiapps/common/test/Tester.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package org.cloudfoundry.multiapps.common.test;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
5-
import static org.junit.jupiter.api.Assertions.fail;
6-
73
import java.text.MessageFormat;
84
import java.util.List;
95
import java.util.Objects;
@@ -15,6 +11,10 @@
1511
import org.cloudfoundry.multiapps.common.util.JsonUtil;
1612
import org.junit.jupiter.api.Assertions;
1713

14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
import static org.junit.jupiter.api.Assertions.fail;
17+
1818
public class Tester {
1919

2020
private final Class<?> testedClass;
@@ -94,12 +94,13 @@ private void validateSuccessForSetExpectation(Set<Object> expectedSet, Set<Objec
9494
private void validateFailure(Expectation expectation, Exception e) {
9595
if (!expectation.expectsFailure()) {
9696
e.printStackTrace();
97-
fail("Test failed: " + e.toString());
97+
fail("Test failed: " + e);
9898
}
9999
String exceptionMessage = e.getMessage();
100100
assertTrue(exceptionMessage.contains(expectation.getExpectationAsString()),
101-
MessageFormat.format("Exception's message doesn't match up! Expected [{0}] to contain [{1}]!", exceptionMessage,
102-
expectation.getExpectationAsString()));
101+
MessageFormat.format("Exception's message doesn't match up! Expected [{0}] to contain [{1}]!",
102+
expectation.getExpectationAsString(), exceptionMessage)
103+
);
103104
}
104105

105106
private Object loadResourceAsJsonObject(String resource) {

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 referenceKey = 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, referenceKey);
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/v3/DescriptorReferenceResolverTest.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ 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")),
36+
// (5)
37+
Arguments.of("merged-05.yaml",
38+
new Expectation(Expectation.Type.EXCEPTION, "Unable to resolve \"module-a##module-b#invalid/0/url\"")));
3639
}
3740

3841
@ParameterizedTest
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
ID: com.sap.mta.v2.test.config-02
2+
_schema-version: 3.3.0
3+
version: 1.0.0
4+
5+
modules:
6+
- name: module-a
7+
type: com.sap.application.content
8+
parameters:
9+
content:
10+
subaccount:
11+
existing_destinations_policy: update
12+
destinations:
13+
- Name: destination-a
14+
URL: ~{module-b/invalid/0/url}
15+
forwardAuthToken: true
16+
ServiceInstanceName: "service-a"
17+
requires:
18+
- name: service-destination
19+
parameters:
20+
content-target: true
21+
- name: service-a
22+
- name: module-b
23+
24+
- name: module-b
25+
type: application
26+
path: "appBits.zip"
27+
requires:
28+
- name: service-a
29+
parameters:
30+
buildpack: staticfile_buildpack
31+
memory: 1G
32+
disk-quota: 1G
33+
provides:
34+
- name: module-b
35+
properties:
36+
default-url: ${default-url}
37+
38+
resources:
39+
- name: service-destination
40+
type: org.cloudfoundry.managed-service
41+
parameters:
42+
service: destination
43+
service-name: service-destination
44+
service-plan: lite
45+
46+
- name: service-a
47+
type: org.cloudfoundry.managed-service
48+
parameters:
49+
service: xsuaa
50+
service-plan: application

0 commit comments

Comments
 (0)