Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Add support for relative id uri
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Oct 25, 2017
1 parent 1bd71a6 commit 2de894f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ public void shouldUseIdToConstructPackageNameAndClassName() throws Exception {
.withGeneratorProperties(generatorProperties)
.generateAndCompileJavaSource(
JSON_SCHEMA_FILE,
"",
"uk.gov.justice.standards.events",
outputDirectories);

assertThat(newClasses.size(), is(1));
assertThat(newClasses.size(), is(4));

final Class<?> pojo = newClasses.get(0);
final Class<?> otherStuff = newClasses.get(0);
final Class<?> age = newClasses.get(1);
final Class<?> pocketMoney = newClasses.get(2);
final Class<?> pojo = newClasses.get(3);

assertThat(age.getName(), is("uk.gov.justice.standards.events.Age"));
assertThat(pocketMoney.getName(), is("uk.gov.justice.standards.events.common.PocketMoney"));
assertThat(otherStuff.getName(), is("uk.gov.justice.standards.events.OtherStuff"));
assertThat(pojo.getName(), is("uk.gov.justice.standards.events.Pojo"));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@
},
"age": {
"id": "http://justice.gov.uk/standards/events/pojo.schema.json#age",
"type": "integer"
"type": "object",
"properties": {
"value": {
"type": "integer"
}
}
},
"pocketMoney": {
"type": "number"
"id": "common/pocketMoney.json",
"type": "object",
"properties": {
"value": {
"type": "number"
}
}
},
"likesJustinBieber": {
"type": "boolean"
"otherStuff": {
"id": "other-stuff.json",
"type": "object",
"properties": {
"value": {
"type": "boolean"
}
}
}
},
"additionalProperties": false,
"required": [
"name",
"age",
"pocketMoney",
"likesJustinBieber"
"otherStuff"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ public class PackageNameParser {
private static final int BEGIN_INDEX = 0;
private static final String HASH = "#";

public String appendToBasePackage(final String uri, final String basePackage) {
final Optional<URI> validUri = validUri(uri);
final Optional<String> packageName = packageNameFrom(uri);

if (validUri.isPresent() && validUri.get().isAbsolute() && packageName.isPresent()) {
return packageName.get();
}

return packageName
.map(packageNameValue -> basePackage + PACKAGE_SEPERATOR + packageNameValue)
.orElse(basePackage);
}

public Optional<String> packageNameFrom(final String uri) {
final Optional<URI> validUri = validUri(uri);

Expand Down Expand Up @@ -51,10 +64,10 @@ private Optional<String> convertPathToPackage(final URI uri) {

final String[] parts = removeNamePartFrom(path).split(URI_SEPERATOR);

if (parts.length > 1) {
if (parts.length > 0) {
final StringBuilder builder = new StringBuilder();

for (int index = 1; index < parts.length; index++) {
for (int index = 0; index < parts.length; index++) {

if (builder.length() > 0) {
builder.append(PACKAGE_SEPERATOR);
Expand Down Expand Up @@ -100,7 +113,7 @@ private String removeNamePartFrom(final String uri) {
return uri.substring(BEGIN_INDEX, uri.lastIndexOf(URI_SEPERATOR));
}

return uri;
return "";
}

private Function<Optional<String>, Stream<? extends String>> filterOptionalEmpty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public ClassName typeNameForClass(final Definition definition) {

if (id.isPresent()) {
final String uri = id.get();
final String packageName = packageNameParser.packageNameFrom(uri)
.orElse(generationContext.getPackageName());
final String packageName = packageNameParser.appendToBasePackage(uri, generationContext.getPackageName());

return get(packageName, classNameParser.simpleClassNameFrom(uri));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ public void shouldParsePackageNameIfThereIsNoHost() throws Exception {
assertThat(packageName, is(Optional.of("standards.events")));
}

@Test
public void shouldNotParsePackageNameIfThereIsNoProtocolOrHostOrPath() throws Exception {
final Optional<String> packageName = new PackageNameParser().packageNameFrom("address");

assertThat(packageName, is(Optional.empty()));
}

@Test
public void shouldReturnOptionalEmptyIfHashName() throws Exception {
final Optional<String> packageName = new PackageNameParser().packageNameFrom("#address");
Expand Down Expand Up @@ -75,4 +68,34 @@ public void shouldReturnOptionalEmptyIfInvalidUri() throws Exception {

assertThat(packageName, is(Optional.empty()));
}

@Test
public void shouldParsePackageNameAndNotAppendToBasePackageIfAbsoluteUri() throws Exception {
final String uri = "http://justice.gov.uk/standards/events/address.schema.json";

final String packageName = new PackageNameParser().appendToBasePackage(uri, "uk.gov.something.else");

assertThat(packageName, is("uk.gov.justice.standards.events"));
}

@Test
public void shouldParsePackageNameAndAppendToBasePackage() throws Exception {
final String packageName = new PackageNameParser().appendToBasePackage("events/address.schema.json", "uk.gov.justice.standards");

assertThat(packageName, is("uk.gov.justice.standards.events"));
}

@Test
public void shouldParsePackageNameIfEmptyReturnBasePackage() throws Exception {
final String packageName = new PackageNameParser().appendToBasePackage("address.schema.json", "uk.gov.justice.standards");

assertThat(packageName, is("uk.gov.justice.standards"));
}

@Test
public void shouldReturnBasePackageNameIfInvalidUri() throws Exception {
final String packageName = new PackageNameParser().appendToBasePackage("://justice.gov.uk", "uk.gov.justice.standards");

assertThat(packageName, is("uk.gov.justice.standards"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ public void shouldUseGenerationContextForPackageAndFieldNameForClassNameIfIdIsNo
public void shouldParseIdForPackageAndClassNameIfIdIsPresentForClasses() throws Exception {

final String id = "http://fred.bloggs.org/person.schema.json";
final String basePackageName = "org.bloggs.fred";
final ClassDefinition classDefinition = mock(ClassDefinition.class);

when(classDefinition.getId()).thenReturn(Optional.of(id));
when(packageNameParser.packageNameFrom(id)).thenReturn(Optional.of("org.bloggs.fred"));
when(generationContext.getPackageName()).thenReturn(basePackageName);
when(packageNameParser.appendToBasePackage(id, basePackageName)).thenReturn(basePackageName);
when(classNameParser.simpleClassNameFrom(id)).thenReturn("Person");

final TypeName typeName = typeNameProvider.typeNameForClass(classDefinition);
Expand Down

0 comments on commit 2de894f

Please sign in to comment.