Skip to content

Commit

Permalink
bumping easy-random
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Oct 14, 2024
1 parent 676ca7c commit 1927db5
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ocadotechnology.gembus</groupId>
<artifactId>test-arranger</artifactId>
<version>1.6.2</version>
<version>1.6.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-arranger</name>
<description>A tool for arranging test data with pseudo-random values.</description>
Expand Down Expand Up @@ -60,7 +60,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<kotlin.version>1.9.22</kotlin.version>
<easy-random.version>7.0.0</easy-random.version>
<easy-random.version>7.0.1-SNAPSHOT</easy-random.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright © 2020 Ocado ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ocadotechnology.gembus.test;

import org.jeasy.random.ObjectCreationException;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static com.ocadotechnology.gembus.test.Arranger.some;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class ArrangerSealedInterfacesTest {

@Test
void should_arrangeSealedInterface() {
//when
SealedInterfaceData actual = some(SealedInterfaceData.class);

//then
assertThat(actual).isInstanceOfAny(ConcreteData1.class, ConcreteData2.class);
}

@Test
void should_arrangeSealedInterfaceWithCustomArranger() {
//when
SealedInterfaceWithCustomArranger actual = some(SealedInterfaceWithCustomArranger.class);

//then
assertThat(actual).isEqualTo(new ConcreteDataWithCustomArranger("expected-test-string"));
}

@Test
void should_arrangeDataWithSealedInterfaceField() {
//when
DataWithAbstract actual = some(DataWithAbstract.class, "excludedSealedInterface", "nonSealedInterface");

//then
assertThat(actual.sealedInterfaceData()).isInstanceOfAny(ConcreteData1.class, ConcreteData2.class);
assertThat(actual.excludedSealedInterface()).isNull();
}

@Test
void should_arrangeDataWithSealedInterfaceFieldOverride() {
//when
ConcreteData1 expected = new ConcreteData1("expected-data");
DataWithAbstract actual = some(DataWithAbstract.class, Map.of(
"sealedInterfaceData", () -> expected,
"excludedSealedInterface", () -> null,
"nonSealedInterface", () -> null));

//then
assertThat(actual.sealedInterfaceData()).isEqualTo(expected);
assertThat(actual.excludedSealedInterface()).isNull();
}

@Test
void should_arrangeDataWithNestedSealedInterfaceField() {
//when
NestedSealedInterfaceData actual = some(NestedSealedInterfaceData.class);

//then
assertThat(actual).isInstanceOf(ConcreteNested1.class);
assertThat((ConcreteNested1) actual)
.extracting(ConcreteNested1::sealedInterfaceData)
.isInstanceOfAny(ConcreteData1.class, ConcreteData2.class);

}

@Test
void should_arrangeRecordWithListWithSealedInterfaceField() {
//when
RootRecordWithNestedList actual = some(RootRecordWithNestedList.class);

//then
assertThat(actual.nestedRecordWithSealedInterfaces().get(0))
.extracting(NestedRecordWithSealedInterface::sealedInterfaceData)
.isInstanceOfAny(ConcreteData1.class, ConcreteData2.class);
}

@Test
void should_failForNonSealedInterface() {
//when
assertThatThrownBy(() -> some(DataWithAbstract.class, "excludedSealedInterface"))

//then
.isInstanceOf(ObjectCreationException.class);

}

}

record DataWithAbstract(Integer value,
SealedInterfaceData sealedInterfaceData,
ExcludedSealedInterface excludedSealedInterface,
NonSealedInterface nonSealedInterface) {
DataWithAbstract {
}

DataWithAbstract(String name) {
this(0, new ConcreteData1(""), new ExcludedData1(), new NonSealedInterface() {
});
}
}

sealed interface SealedInterfaceData permits ConcreteData1, ConcreteData2 {
}

record ConcreteData1(String value) implements SealedInterfaceData {
}

final class ConcreteData2 implements SealedInterfaceData {

Integer value;

}

sealed interface ExcludedSealedInterface permits ExcludedData1 {
}

record ExcludedData1() implements ExcludedSealedInterface {
}

interface NonSealedInterface {
}

sealed interface NestedSealedInterfaceData permits ConcreteNested1 {
}

record ConcreteNested1(SealedInterfaceData sealedInterfaceData) implements NestedSealedInterfaceData {
}

sealed interface SealedInterfaceWithCustomArranger permits ConcreteDataWithCustomArranger {
}

record ConcreteDataWithCustomArranger(String test) implements SealedInterfaceWithCustomArranger {
}

class CustomSealedInterfaceArranger extends CustomArranger<SealedInterfaceWithCustomArranger> {

@Override
protected SealedInterfaceWithCustomArranger instance() {
return new ConcreteDataWithCustomArranger("expected-test-string");
}
}

record RootRecordWithNestedList(List<NestedRecordWithSealedInterface> nestedRecordWithSealedInterfaces) {
}

record NestedRecordWithSealedInterface(SealedInterfaceData sealedInterfaceData) {
}

0 comments on commit 1927db5

Please sign in to comment.