Skip to content

Commit

Permalink
implement Comparable interface for TagObject and ExternalDocumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed Jan 25, 2023
1 parent 8f454f3 commit 81b3e44
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@

package software.amazon.smithy.openapi.model;

import java.util.Comparator;
import java.util.Optional;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;

public final class ExternalDocumentation extends Component implements ToSmithyBuilder<ExternalDocumentation> {
public final class ExternalDocumentation extends Component
implements ToSmithyBuilder<ExternalDocumentation>, Comparable<ExternalDocumentation> {

private static final Comparator<String> STRING_COMPARATOR = Comparator
.nullsFirst(String::compareTo);

private final String description;
private final String url;

Expand Down Expand Up @@ -58,6 +64,13 @@ protected ObjectNode.Builder createNodeBuilder() {
.withMember("url", url);
}

@Override
public int compareTo(ExternalDocumentation that) {
return Comparator.comparing(ExternalDocumentation::getUrl, STRING_COMPARATOR)
.thenComparing(ed -> ed.description, STRING_COMPARATOR)
.compare(this, that);
}

public static final class Builder extends Component.Builder<Builder, ExternalDocumentation> {
private String description;
private String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@

package software.amazon.smithy.openapi.model;

import java.util.Comparator;
import java.util.Optional;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;

public final class TagObject extends Component implements ToSmithyBuilder<TagObject> {
public final class TagObject extends Component implements ToSmithyBuilder<TagObject>, Comparable<TagObject> {
private static final Comparator<String> STRING_COMPARATOR = Comparator
.nullsFirst(String::compareToIgnoreCase);

private static final Comparator<ExternalDocumentation> EXTERNAL_DOCUMENTATION_COMPARATOR = Comparator
.nullsFirst(ExternalDocumentation::compareTo);

private final String name;
private final String description;
private final ExternalDocumentation externalDocs;
Expand Down Expand Up @@ -66,6 +73,14 @@ protected ObjectNode.Builder createNodeBuilder() {
.withOptionalMember("externalDocs", getExternalDocs().map(ExternalDocumentation::toNode));
}

@Override
public int compareTo(TagObject that) {
return Comparator.comparing(TagObject::getName, STRING_COMPARATOR)
.thenComparing(to -> to.description, STRING_COMPARATOR)
.thenComparing(to -> to.externalDocs, EXTERNAL_DOCUMENTATION_COMPARATOR)
.compare(this, that);
}

public static final class Builder extends Component.Builder<Builder, TagObject> {
private String name;
private String description;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package software.amazon.smithy.openapi.model;

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

import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

class ExternalDocumentationTest {
private static final ExternalDocumentation DOC_1 =
ExternalDocumentation.builder()
.url("url1")
.description("description1")
.build();

private static Stream<Arguments> testData() {
return Stream.of(
Arguments.of(ExternalDocumentation.builder().url("url1").description("description1").build(), 0),
Arguments.of(ExternalDocumentation.builder().url("url1").description("description0").build(), 1),
Arguments.of(ExternalDocumentation.builder().url("url1").description("description2").build(), -1),
Arguments.of(ExternalDocumentation.builder().url("url0").description("description1").build(), 1),
Arguments.of(ExternalDocumentation.builder().url("url2").description("description1").build(), -1),
Arguments.of(ExternalDocumentation.builder().url("url1").build(), 1)
);
}

@ParameterizedTest
@MethodSource("testData")
void testCompareTo(ExternalDocumentation doc2, int expected) {
assertThat(DOC_1.compareTo(doc2), equalTo(expected));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package software.amazon.smithy.openapi.model;

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

import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

class TagObjectTest {
private static final TagObject TAG_OBJECT_1 =
TagObject.builder()
.name("tag1")
.description("description1")
.externalDocs(ExternalDocumentation.builder().url("url1").build())
.build();

private static Stream<Arguments> testData() {
return Stream.of(
Arguments.of("tag1", "description1", ExternalDocumentation.builder().url("url1").build(), 0),
Arguments.of("tag0", "description1", ExternalDocumentation.builder().url("url1").build(), 1),
Arguments.of("tag1", "description1", ExternalDocumentation.builder().url("url2").build(), -1),
Arguments.of("tag2", "description1", ExternalDocumentation.builder().url("url1").build(), -1),
Arguments.of("tag1", "description2", ExternalDocumentation.builder().url("url1").build(), -1),
Arguments.of("tag1", null, ExternalDocumentation.builder().url("url1").build(), 1),
Arguments.of("tag1", "description1", null, 1)
);
}

@ParameterizedTest
@MethodSource("testData")
void testCompareTo(String name, String description, ExternalDocumentation doc, int expected) {
TagObject tagObject2 = TagObject.builder().name(name).description(description).externalDocs(doc).build();
assertThat(TAG_OBJECT_1.compareTo(tagObject2), equalTo(expected));
}
}

0 comments on commit 81b3e44

Please sign in to comment.