Skip to content

Commit

Permalink
Merge pull request #57 from epam/main
Browse files Browse the repository at this point in the history
Trim Urn Before Parsing: MaintainableArtefactReference + SdmxUrn (#56)
  • Loading branch information
buhaiovos authored Jan 7, 2025
2 parents 9e82839 + 028a44e commit 4d8a668
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -34,11 +35,12 @@ public MaintainableArtefactReference(String id, String organisationId, String ve
}

public MaintainableArtefactReference(String urn) {
final UrnComponents urnComponents = SdmxUrn.getUrnComponents(urn);
final String trimmedUrn = StringUtils.trim(urn);
final UrnComponents urnComponents = SdmxUrn.getUrnComponents(trimmedUrn);
this.organisationId = urnComponents.getAgency();
this.id = urnComponents.getId();
this.version = VersionReference.createFromString(urnComponents.getVersion());
this.structureClass = getType(urn);
this.structureClass = getType(trimmedUrn);
}

public MaintainableArtefactReference(ArtefactReference from) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ public static boolean isUrn(String candidate) {
}

public static UrnComponents getUrnComponents(String urn) {
final Matcher fullUrnMatcher = FULL_URN_PATTERN.matcher(urn);
final String trimmedUrn = StringUtils.trim(urn);
final Matcher fullUrnMatcher = FULL_URN_PATTERN.matcher(trimmedUrn);
if (fullUrnMatcher.matches()) {
return buildFromFullUrn(fullUrnMatcher);
}

final Matcher shortUrnMatcher = SHORT_URN_PATTERN.matcher(urn);
final Matcher shortUrnMatcher = SHORT_URN_PATTERN.matcher(trimmedUrn);
if (shortUrnMatcher.matches()) {
return buildFromShortUrn(shortUrnMatcher);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.epam.jsdmx.infomodel.sdmx30;


import static org.assertj.core.api.Assertions.assertThat;

import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class MaintainableArtefactReferenceTest {

@ParameterizedTest
@MethodSource
void shouldTrimUrnOnInit(String urn) {
var res = new MaintainableArtefactReference("\nurn:sdmx:org.sdmx.infomodel.codelist.Codelist=EPM:CL(1.0)");
assertThat(res).isNotNull();
}

private static Stream<Arguments> shouldTrimUrnOnInit() {
return Stream.of(
Arguments.of("\nurn:sdmx:org.sdmx.infomodel.codelist.Codelist=EPM:CL(1.0)"),
Arguments.of("\n urn:sdmx:org.sdmx.infomodel.codelist.Codelist=EPM:CL(1.0)"),
Arguments.of("\nurn:sdmx:org.sdmx.infomodel.codelist.Codelist=EPM:CL(1.0) "),
Arguments.of("\n urn:sdmx:org.sdmx.infomodel.codelist.Codelist=EPM:CL(1.0) "),
Arguments.of("\n urn:sdmx:org.sdmx.infomodel.codelist.Codelist=EPM:CL(1.0)\n ")
);
}

}

0 comments on commit 4d8a668

Please sign in to comment.