diff --git a/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfiguration.java b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfiguration.java index 6d874b00..234ac70e 100644 --- a/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfiguration.java +++ b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfiguration.java @@ -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 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); } diff --git a/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDiscoveryWithDependenciesIntegrationTests.java b/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDiscoveryWithDependenciesIntegrationTests.java index db96f77d..c523d357 100644 --- a/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDiscoveryWithDependenciesIntegrationTests.java +++ b/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDiscoveryWithDependenciesIntegrationTests.java @@ -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(); } diff --git a/spring-cloud-zookeeper-discovery/src/test/resources/application-dependencies.yml b/spring-cloud-zookeeper-discovery/src/test/resources/application-dependencies.yml index 3419b1bd..dcb05d91 100644 --- a/spring-cloud-zookeeper-discovery/src/test/resources/application-dependencies.yml +++ b/spring-cloud-zookeeper-discovery/src/test/resources/application-dependencies.yml @@ -21,3 +21,5 @@ spring.cloud.zookeeper: required: false aliasIsPath: '' anotherAlias: 'myPath' + some-service: + path: io/company/department/some-service