Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/86 #101

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/com/graqr/threshr/model/redsky/product/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.serde.annotation.Serdeable;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

@Serdeable
public record Metadata(
Expand All @@ -24,4 +26,28 @@ public record Metadata(
int totalResults,
@JsonProperty("sort_options")
Map<String, String> sortOptions) {

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (Objects.isNull(object) || getClass() != object.getClass()) return false;
Metadata castObject = (Metadata) object;
return Objects.equals(count, castObject.count()) &&
Arrays.equals(responseIds, castObject.responseIds()) &&
Objects.equals(currentPage, castObject.currentPage()) &&
Objects.equals(keyword, castObject.keyword()) &&
Objects.equals(offset, castObject.offset()) &&
Objects.equals(sortBy, castObject.sortBy()) &&
Objects.equals(totalPages, castObject.totalPages()) &&
Objects.equals(totalResults, castObject.totalResults()) &&
Objects.equals(sortOptions, castObject.sortOptions());
}

@Override
public int hashCode() {
int result = Objects.hash(count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions);
result = 31 * result + Arrays.hashCode(responseIds);
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.graqr.threshr.model.redsky.product

import net.datafaker.Faker
import spock.lang.Shared
import spock.lang.Specification

import java.util.function.Function
import java.util.stream.Collectors

class MetadataSpec extends Specification {

@Shared
Faker faker
@Shared
String[] responseIds
@Shared
int count, currentPage, offset, totalPages, totalResults
@Shared
String keyword, sortBy
@Shared
Map<String, String> sortOptions

@Shared
Function<Integer, Integer> randIntNot = num -> {
def value = randInt
return value != num ? value : randIntNot.apply(num as Integer)
}

int getRandInt() {
return faker.number().randomDigitNotZero()
}

int getRandInt(int exclude) {
def value = getRandInt()
return value != exclude ? value : getRandInt()
}

void setupSpec() {
faker = new Faker()
def starWars = faker.starWars()
responseIds = Collections.nCopies(getRandInt(), faker.idNumber().valid()).toArray(String[]::new)
count = getRandInt()
currentPage = getRandInt()
keyword = starWars.wookieWords()
offset = getRandInt()
sortBy = starWars.quotes()
totalPages = getRandInt()
totalResults = getRandInt()
sortOptions = Arrays.stream(Collections.nCopies(getRandInt(), starWars.character())).collect(
Collectors.toMap(person -> (String) person, person -> starWars.quotes()))
}

void "equals validates self and identical metaData object match"() {
given:
Metadata metadata = new Metadata(
responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions)

when:
metadata == metadata

and:
metadata == new Metadata(
responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions)

then:
noExceptionThrown()
}

void "equals comparison between an object and itself fails after changing #changedObject"() {
given:
Metadata metadata = new Metadata(
responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions)

when:
metadata != nonMatchingMetada

then:
noExceptionThrown()

where:
nonMatchingMetada | changedObject
new Metadata(Collections.nCopies(faker.number().randomDigitNotZero(), faker.idNumber().valid()).toArray(String[]::new),
count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions) | "responseIds"
new Metadata(responseIds, (int) randIntNot.apply(count),
currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions) | "count"
new Metadata(responseIds, count, getRandInt(currentPage),
keyword, offset, sortBy, totalPages, totalResults, sortOptions) | "currentPage"
new Metadata(responseIds, count, currentPage, faker.chuckNorris().fact(),
offset, sortBy, totalPages, totalResults, sortOptions) | "keyword"
new Metadata(responseIds, count, currentPage, keyword, getRandInt(offset),
sortBy, (totalPages), totalResults, sortOptions) | "offset"
new Metadata(responseIds, count, currentPage, keyword, offset, faker.studioGhibli().quote(),
totalPages, (totalResults), sortOptions) | "sortBy"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, getRandInt(totalPages),
totalResults, sortOptions) | "totalPages"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, getRandInt(totalResults),
sortOptions) | "totalResults"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults,
Arrays.stream(Collections.nCopies(getRandInt(), faker.studioGhibli().character())).collect(
Collectors.toMap(person -> (String) person, person -> faker.chuckNorris().fact()))) | "sortOptions"
Map.of("responseIds", responseIds,
"count", count,
"currentPage", currentPage,
"keyword", keyword,
"offset", offset,
"sortBy", sortBy,
"totalPages", totalPages,
"totalResults", totalResults,
"sortOptions", sortOptions) | "everything to a Map of values matching the original object"
null | "to a null value"
}

void "generated hashes of self and identical metaData object match"() {
given:
Metadata metadata = new Metadata(
responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions)

when:
metadata.hashCode() == metadata.hashCode()

and:
metadata.hashCode() == new Metadata(
responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions).hashCode()

then:
noExceptionThrown()
}

void "generated hashes of self and foreign object altered in #changedObject do NOT match"() {
given:
int ogHash = new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults,
sortOptions).hashCode()

when:
ogHash != nonMatchingMetada.hashCode()

then:
noExceptionThrown()

where:
nonMatchingMetada | changedObject
new Metadata(Collections.nCopies(faker.number().randomDigitNotZero(), faker.idNumber().valid()).toArray(String[]::new),
count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions) | "responseIds"
new Metadata(Collections.nCopies(faker.number().randomDigitNotZero(), null).toArray(String[]::new),
count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions) | "responseIds as null"
new Metadata(responseIds, (int) randIntNot.apply(count),
currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions) | "count"
new Metadata(responseIds, count, getRandInt(currentPage),
keyword, offset, sortBy, totalPages, totalResults, sortOptions) | "currentPage"
new Metadata(responseIds, count, currentPage, faker.chuckNorris().fact(),
offset, sortBy, totalPages, totalResults, sortOptions) | "keyword"
new Metadata(responseIds, count, currentPage, keyword, getRandInt(offset),
sortBy, (totalPages), totalResults, sortOptions) | "offset"
new Metadata(responseIds, count, currentPage, keyword, offset, faker.studioGhibli().quote(),
totalPages, (totalResults), sortOptions) | "sortBy"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, getRandInt(totalPages),
totalResults, sortOptions) | "totalPages"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, getRandInt(totalResults),
sortOptions) | "totalResults"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults, sortOptions
.replace(((Map<String, String>) sortOptions)
.keySet()
.last(), faker.chuckNorris()
.fact()) as Map<String, String>) | "sortOptions via edit"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults,
sortOptions.remove(sortOptions.keySet().last()) as Map<String, String>) | "sortOptions via trim"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults,
sortOptions.put(faker.studioGhibli().character(), faker.chuckNorris().fact()) as Map<String, String>) | "sortOptions via appending"
new Metadata(responseIds, count, currentPage, keyword, offset, sortBy, totalPages, totalResults,
Arrays.stream(Collections.nCopies(getRandInt(), faker.studioGhibli().character())).collect(
Collectors.toMap(person -> (String) person, person -> faker.chuckNorris().fact()))) | "all of sort options"
Map.of("responseIds", responseIds,
"count", count,
"currentPage", currentPage,
"keyword", keyword,
"offset", offset,
"sortBy", sortBy,
"totalPages", totalPages,
"totalResults", totalResults,
"sortOptions", sortOptions) | "everything to a Map of values matching the original object"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ class SearchRecommendationsSpec extends Specification {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
when:
recommendations == new SearchRecommendations(testCategories.first().title(), testCategories)

then:
noExceptionThrown()
}

void "equals function fails appropriately with single field differences"() {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
when:
recommendations != new SearchRecommendations(title, categories as RelatedCategory[])

then:
noExceptionThrown()

where:
title | categories
testCategories.first().title() | testCategories[0..3].toArray(RelatedCategory[]::new) //bad categories
Expand All @@ -56,17 +62,23 @@ class SearchRecommendationsSpec extends Specification {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
when:
recommendations.hashCode() == new SearchRecommendations(testCategories.first().title(), testCategories).hashCode()

then:
noExceptionThrown()
}

void "HashCode method fails appropriately with singe field differences"() {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
when:
recommendations != new SearchRecommendations(title, categories as RelatedCategory[])

then:
noExceptionThrown()

where:
title | categories
testCategories.first().title() | testCategories[0..3].toArray(RelatedCategory[]::new) //bad categories
Expand All @@ -84,9 +96,12 @@ class SearchRecommendationsSpec extends Specification {
}
.collect(Collectors.joining(", "))

expect:
when:
recommendations.toString() == "SearchRecommendations[categoryId=${expectedTitle}, relatedCategories=[${expectedCategories}]]"

then:
noExceptionThrown()


where:
title | categories
Expand Down
Loading