package de.zalando.toga.generator;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.zalando.toga.generator.scenarios.SingleEnum;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
import static de.zalando.toga.generator.GeneratorTest.generateOutput;
import static de.zalando.toga.generator.GeneratorTest.verifyFile;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class EnumDimensionTest {
@Test
public void enumShouldBeReturnedCompletely() throws IOException {
verifyFile("enum/single_enum", SingleEnum.class);
}
@Test
public void enumShouldBeRandomized() throws IOException {
String scenario = "enum/random_enum";
File output = generateOutput(scenario);
ObjectMapper mapper = new ObjectMapper();
List<SingleEnum> referenceObjects = mapper.readValue(output, mapper.getTypeFactory().constructCollectionType(List.class, SingleEnum.class));
List<SingleEnum.Fruits> values = newArrayList(SingleEnum.Fruits.values());
// check that all values are generated
referenceObjects.stream().
forEach(element -> assertThat("Enum does not contain [" + element.getFruit() + "].",
values.contains(element.getFruit()), is(true)));
values.stream().
forEach(element -> assertThat("Generated list does not contain [" + element + "].",
referenceObjects.contains(new SingleEnum(element)), is(true)));
// verify that the ordering is not the same
boolean isSameOrdering = true;
for (int i = 0; i < referenceObjects.size(); i++) {
SingleEnum ref = referenceObjects.get(i);
SingleEnum.Fruits fruit = values.get(i);
if (ref.getFruit() != fruit) {
isSameOrdering = false;
break;
}
}
assertThat("The ordering of the enum and the reference objects is the same.", isSameOrdering, is(false));
}
@Test
public void exampleCountShouldLimitResultSet() throws IOException {
String scenario = "enum/count_enum";
File output = generateOutput(scenario);
ObjectMapper mapper = new ObjectMapper();
List<SingleEnum> referenceObjects = mapper.readValue(output, mapper.getTypeFactory().constructCollectionType(List.class, SingleEnum.class));
assertThat(referenceObjects.size(), is(2));
}
@Test
public void exampleCountShouldExtendResultSet() throws IOException {
String scenario = "enum/large_count_enum";
File output = generateOutput(scenario);
ObjectMapper mapper = new ObjectMapper();
List<SingleEnum> referenceObjects = mapper.readValue(output, mapper.getTypeFactory().constructCollectionType(List.class, SingleEnum.class));
assertThat(referenceObjects.size(), is(20));
}
@Test
public void includeEmptyShouldResultInNullValue() throws IOException {
String scenario = "enum/null_enum";
File output = generateOutput(scenario);
ObjectMapper mapper = new ObjectMapper();
List<SingleEnum> referenceObjects = mapper.readValue(output, mapper.getTypeFactory().constructCollectionType(List.class, SingleEnum.class));
SingleEnum reference = referenceObjects.get(0);
assertThat(reference.getFruit(), is(nullValue()));
}
}