Skip to content

Commit

Permalink
Use ParameterizedTest where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
drumonii committed Oct 22, 2019
1 parent 3c175a2 commit b73b83c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.drumonii.loltrollbuild.model.Version;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -37,17 +40,15 @@ void buildsFromlolpatchPatch() {
assertThat(version.getRevision()).as("Revision").isEqualTo(0);
}

@Test
void throwsIllegalArgumentExceptionFromBuildingWithInvalidPatches() {
List<String> patches = Arrays.asList("a.b.c", "", null);

for (String patch : patches) {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new VersionBuilder()
.fromPatch(patch)
.build())
.withMessage("Failed to build the Version from the provided patch: '%s'", patch);
}
@ParameterizedTest(name = "patch=''{0}''")
@NullAndEmptySource
@ValueSource(strings = { "a.b.c" })
void throwsIllegalArgumentExceptionFromBuildingWithInvalidPatches(String patch) {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new VersionBuilder()
.fromPatch(patch)
.build())
.withMessage("Failed to build the Version from the provided patch: '%s'", patch);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import static com.drumonii.loltrollbuild.util.GameMapUtil.*;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -27,64 +30,64 @@ abstract class ItemsRepositoryTest {

private ItemsResponse itemsResponse;

private static final List<Integer> MAPS = List.of(TWISTED_TREELINE_ID, SUMMONERS_RIFT_ID, HOWLING_ABYSS_ID);

protected abstract ItemsResponse getItemsResponse();

static Stream<Integer> maps() {
return Stream.of(TWISTED_TREELINE_ID, SUMMONERS_RIFT_ID, HOWLING_ABYSS_ID);
}

@BeforeEach
void beforeEach() {
itemsResponse = getItemsResponse();
itemsRepository.saveAll(itemsResponse.getItems().values());
}

@Test
void boots() {
@ParameterizedTest(name = "map id=''{0}''")
@MethodSource("maps")
void boots(Integer map) {
Item bootsOfSpeed = itemsResponse.getItems().get("1001");

for (Integer map : MAPS) {
List<Item> boots = itemsRepository.boots(map);
assertThat(boots).as("Boots should be empty for map: " + GameMapUtil.getNameFromId(map) + "(" + map + ")")
.isNotEmpty();
assertThat(boots).doesNotHaveDuplicates();
assertThat(boots).doesNotContain(bootsOfSpeed);
assertThat(boots).flatExtracting(Item::getFrom)
.contains(1001);
assertThat(boots).extracting(Item::getDescription).allSatisfy(description -> {
assertThat(description).isNotNull();
assertThat(description).contains("Movement");
List<Item> boots = itemsRepository.boots(map);
assertThat(boots).as("Boots should be empty for map: " + GameMapUtil.getNameFromId(map) + "(" + map + ")")
.isNotEmpty();
assertThat(boots).doesNotHaveDuplicates();
assertThat(boots).doesNotContain(bootsOfSpeed);
assertThat(boots).flatExtracting(Item::getFrom)
.contains(1001);
assertThat(boots).extracting(Item::getDescription).allSatisfy(description -> {
assertThat(description).isNotNull();
assertThat(description).contains("Movement");
});
assertThat(boots).extracting(Item::getMaps)
.extracting(input -> input.get(map))
.containsOnly(true);
}

@ParameterizedTest(name = "map id=''{0}''")
@MethodSource("maps")
void trinkets(Integer map) {
List<Item> trinkets = itemsRepository.trinkets(map);
assertThat(trinkets).isNotEmpty();
assertThat(trinkets).doesNotHaveDuplicates();
if (map == TWISTED_TREELINE_ID) {
assertThat(trinkets).extracting(Item::getName).containsOnly("Arcane Sweeper");
} else if (map == HOWLING_ABYSS_ID) {
assertThat(trinkets).extracting(Item::getName).containsOnly("Poro-Snax");
} else {
assertThat(trinkets).extracting(Item::getGold).extracting(ItemGold::getTotal)
.containsOnly(0);
assertThat(trinkets).extracting(Item::getGold).extracting("purchasable", Boolean.class)
.containsOnly(true);
assertThat(trinkets).extracting(Item::getTags).allSatisfy(tags -> {
assertThat(tags).contains("Trinket");
});
assertThat(boots).extracting(Item::getMaps)
assertThat(trinkets).extracting(Item::getMaps)
.extracting(input -> input.get(map))
.containsOnly(true);
}
}

@Test
void trinkets() {
for (Integer map : MAPS) {
List<Item> trinkets = itemsRepository.trinkets(map);
assertThat(trinkets).isNotEmpty();
assertThat(trinkets).doesNotHaveDuplicates();
if (map == TWISTED_TREELINE_ID) {
assertThat(trinkets).extracting(Item::getName).containsOnly("Arcane Sweeper");
} else if (map == HOWLING_ABYSS_ID) {
assertThat(trinkets).extracting(Item::getName).containsOnly("Poro-Snax");
} else {
assertThat(trinkets).extracting(Item::getGold).extracting(ItemGold::getTotal)
.containsOnly(0);
assertThat(trinkets).extracting(Item::getGold).extracting("purchasable", Boolean.class)
.containsOnly(true);
assertThat(trinkets).extracting(Item::getTags).allSatisfy(tags -> {
assertThat(tags).contains("Trinket");
});
assertThat(trinkets).extracting(Item::getMaps)
.extracting(input -> input.get(map))
.containsOnly(true);
assertThat(trinkets).extracting(Item::getDescription).allSatisfy(description -> {
assertThat(description).isNotNull();
assertThat(description).contains("Trinket");
});
}
assertThat(trinkets).extracting(Item::getDescription).allSatisfy(description -> {
assertThat(description).isNotNull();
assertThat(description).contains("Trinket");
});
}
}

Expand All @@ -106,54 +109,53 @@ void viktorOnly() {
.contains(true);
}

@Test
void forTrollBuild() {
for (Integer map : MAPS) {
List<Item> forTrollBuild = itemsRepository.forTrollBuild(map);
assertThat(forTrollBuild).as("Items for Troll Build should not be empty for map: " + GameMapUtil.getNameFromId(map) + "(" + map + ")")
.isNotEmpty();
assertThat(forTrollBuild).doesNotHaveDuplicates();
assertThat(forTrollBuild).extracting(Item::getMaps)
.extracting(input -> input.get(map))
.contains(true);
assertThat(forTrollBuild).extracting(Item::getGold)
.extracting("purchasable", Boolean.class)
.containsOnly(true);
assertThat(forTrollBuild).extracting(Item::getConsumed)
.containsNull();
assertThat(forTrollBuild).filteredOn(item -> item.getInto() != null).flatExtracting(Item::getInto)
.isEmpty();
assertThat(forTrollBuild).doesNotContain(itemsResponse.getItems().get("1001"));
assertThat(forTrollBuild).extracting(Item::getDescription).allSatisfy(description -> {
assertThat(description).isNotNull();
assertThat(description).doesNotContain("Movement");
assertThat(description).doesNotContain("Potion");
assertThat(description).doesNotContain("Trinket");
});
assertThat(forTrollBuild).extracting(Item::getRequiredChampion).allSatisfy(requiredChampion -> {
assertThat(requiredChampion).isNull();
});
assertThat(forTrollBuild).extracting(Item::getRequiredAlly).allSatisfy(requiredAlly -> {
assertThat(requiredAlly).isNull();
});
assertThat(forTrollBuild).extracting(Item::getName).allSatisfy(name -> {
assertThat(name).isNotNull();
assertThat(name).doesNotContain("Movement");
assertThat(name).doesNotContain("Potion");
assertThat(name).doesNotContain("Trinket");
assertThat(name).doesNotContain("Viktor");
assertThat(name).doesNotContain("Flask");
assertThat(name).doesNotContain("Enchantment");
assertThat(name).doesNotContain("Doran");
assertThat(name).doesNotContain("Quick");
});
assertThat(forTrollBuild).extracting(Item::getGroup)
.filteredOn(Objects::nonNull)
.allSatisfy(group -> {
assertThat(group).doesNotContain("FlaskGroup");
assertThat(group).doesNotContain("RelicBase");
});
}
@ParameterizedTest(name = "map id=''{0}''")
@MethodSource("maps")
void forTrollBuild(Integer map) {
List<Item> forTrollBuild = itemsRepository.forTrollBuild(map);
assertThat(forTrollBuild).as("Items for Troll Build should not be empty for map: " + GameMapUtil.getNameFromId(map) + "(" + map + ")")
.isNotEmpty();
assertThat(forTrollBuild).doesNotHaveDuplicates();
assertThat(forTrollBuild).extracting(Item::getMaps)
.extracting(input -> input.get(map))
.contains(true);
assertThat(forTrollBuild).extracting(Item::getGold)
.extracting("purchasable", Boolean.class)
.containsOnly(true);
assertThat(forTrollBuild).extracting(Item::getConsumed)
.containsNull();
assertThat(forTrollBuild).filteredOn(item -> item.getInto() != null).flatExtracting(Item::getInto)
.isEmpty();
assertThat(forTrollBuild).doesNotContain(itemsResponse.getItems().get("1001"));
assertThat(forTrollBuild).extracting(Item::getDescription).allSatisfy(description -> {
assertThat(description).isNotNull();
assertThat(description).doesNotContain("Movement");
assertThat(description).doesNotContain("Potion");
assertThat(description).doesNotContain("Trinket");
});
assertThat(forTrollBuild).extracting(Item::getRequiredChampion).allSatisfy(requiredChampion -> {
assertThat(requiredChampion).isNull();
});
assertThat(forTrollBuild).extracting(Item::getRequiredAlly).allSatisfy(requiredAlly -> {
assertThat(requiredAlly).isNull();
});
assertThat(forTrollBuild).extracting(Item::getName).allSatisfy(name -> {
assertThat(name).isNotNull();
assertThat(name).doesNotContain("Movement");
assertThat(name).doesNotContain("Potion");
assertThat(name).doesNotContain("Trinket");
assertThat(name).doesNotContain("Viktor");
assertThat(name).doesNotContain("Flask");
assertThat(name).doesNotContain("Enchantment");
assertThat(name).doesNotContain("Doran");
assertThat(name).doesNotContain("Quick");
});
assertThat(forTrollBuild).extracting(Item::getGroup)
.filteredOn(Objects::nonNull)
.allSatisfy(group -> {
assertThat(group).doesNotContain("FlaskGroup");
assertThat(group).doesNotContain("RelicBase");
});
}

}

0 comments on commit b73b83c

Please sign in to comment.