Fixed invalid path parsing for stub dependencies

fixes #138
This commit is contained in:
Marcin Grzejszczak
2017-07-04 12:34:59 +02:00
parent 9ab76f4486
commit bf7b91eb2b
3 changed files with 32 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.zookeeper.discovery.dependency;
import java.util.Arrays;
import java.util.LinkedList;
import org.springframework.util.StringUtils;
@@ -26,20 +27,20 @@ public class StubsConfiguration {
}
public StubsConfiguration(String stubPath) {
String[] parsedPath = parsedPathEmptyByDefault(stubPath, STUB_COLON_DELIMITER);
String[] parsedPath = parsedStubPathEmptyByDefault(stubPath, STUB_COLON_DELIMITER);
this.stubsGroupId = parsedPath[0];
this.stubsArtifactId = parsedPath[1];
this.stubsClassifier = parsedPath[2];
}
public StubsConfiguration(DependencyPath path) {
String[] parsedPath = parsedPathEmptyByDefault(path.getPath(), PATH_SLASH_DELIMITER);
String[] parsedPath = parsedDependencyPathEmptyByDefault(path.getPath(), PATH_SLASH_DELIMITER);
this.stubsGroupId = parsedPath[0];
this.stubsArtifactId = parsedPath[1];
this.stubsClassifier = parsedPath[2];
}
private String[] parsedPathEmptyByDefault(String path, String delimiter) {
private String[] parsedStubPathEmptyByDefault(String path, String delimiter) {
String[] splitPath = path.split(delimiter);
String stubsGroupId = "";
String stubsArtifactId = "";
@@ -52,6 +53,22 @@ public class StubsConfiguration {
return new String[]{stubsGroupId, stubsArtifactId, stubsClassifier};
}
private String[] parsedDependencyPathEmptyByDefault(String path, String delimiter) {
String trimmedPath = path.startsWith(delimiter) ? path.substring(1) : path;
String[] splitPath = trimmedPath.split(delimiter);
String stubsGroupId = "";
String stubsArtifactId = "";
String stubsClassifier = "";
if (splitPath.length >= 2) {
LinkedList<String> list = new LinkedList<>(Arrays.asList(splitPath));
String lastElement = list.removeLast();
stubsGroupId = StringUtils.collectionToDelimitedString(list, ".");
stubsArtifactId = lastElement;
stubsClassifier = DEFAULT_STUBS_CLASSIFIER;
}
return new String[]{stubsGroupId, stubsArtifactId, stubsClassifier};
}
private boolean isDefined() {
return StringUtils.hasText(this.stubsGroupId) && StringUtils.hasText(this.stubsArtifactId);
}

View File

@@ -145,6 +145,16 @@ public class ZookeeperDiscoveryWithDependenciesIntegrationTests {
then(dependency.getPath()).isEqualTo("/myPath");
}
// #138
@Test public void should_parse_dependency_with_path() {
// given:
StubsConfiguration someServiceStub = this.zookeeperDependencies.getDependencyForAlias("some-service").getStubsConfiguration();
// expect:
then(someServiceStub.getStubsGroupId()).isEqualTo("io.company.department");
then(someServiceStub.getStubsArtifactId()).isEqualTo("some-service");
then(someServiceStub.getStubsClassifier()).isEqualTo("stubs");
}
private boolean callingServiceAtBeansEndpointIsNotEmpty() {
return !this.testRibbonClient.callService("someAlias", "beans").isEmpty();
}

View File

@@ -21,3 +21,5 @@ spring.cloud.zookeeper:
required: false
aliasIsPath: ''
anotherAlias: 'myPath'
some-service:
path: io/company/department/some-service