33import com .google .inject .Guice ;
44import com .google .inject .Injector ;
55import com .google .inject .Module ;
6+ import lombok .RequiredArgsConstructor ;
67import org .junit .Rule ;
78import org .junit .Test ;
9+ import org .junit .rules .ExpectedException ;
810import org .mockito .Mock ;
11+ import org .mockito .invocation .InvocationOnMock ;
912import org .mockito .junit .MockitoJUnit ;
1013import org .mockito .junit .MockitoRule ;
14+ import org .mockito .stubbing .Answer ;
15+ import pl .wavesoftware .eid .exceptions .EidIllegalStateException ;
16+ import pl .wavesoftware .test .TestRepository .Database ;
17+ import pl .wavesoftware .test .TestRepository .Example ;
18+ import pl .wavesoftware .test .TestRepository .Execution ;
1119import pl .wavesoftware .test .entity .Pet ;
1220import pl .wavesoftware .test .jpa .PetJPA ;
1321import pl .wavesoftware .test .mapper .MapperFacade ;
1624import java .util .ArrayList ;
1725import java .util .Arrays ;
1826import java .util .List ;
27+ import java .util .Optional ;
1928
2029import static org .assertj .core .api .Assertions .assertThat ;
30+ import static org .mockito .ArgumentMatchers .any ;
31+ import static org .mockito .ArgumentMatchers .anyLong ;
32+ import static org .mockito .Mockito .when ;
2133
2234/**
2335 * @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
@@ -33,14 +45,23 @@ public class MappingTest {
3345 @ Rule
3446 public MockitoRule mockitoRule = MockitoJUnit .rule ();
3547
48+ @ Rule
49+ public ExpectedException thrown = ExpectedException .none ();
50+
3651 @ Test
3752 public void testMapFromPetToJPA () {
3853 // given
3954 Injector injector = createInjector ((Module ) binder ->
4055 binder .bind (EntityManager .class ).toInstance (entityManager )
4156 );
4257 MapperFacade mapper = injector .getInstance (MapperFacade .class );
43- Pet alice = testRepository .createPetNamedAlice ();
58+ Execution execution = testRepository .forCase (Example .STANDARD );
59+ Database <Pet > entityDb = execution .createPetNamedAlice ();
60+ Database <PetJPA > database = execution .createJpaPetNamedAlice ();
61+ bindEntityManager (database );
62+ Pet alice = entityDb .getObject ();
63+ Optional .ofNullable (alice .getOwner ())
64+ .ifPresent (o -> o .setName ("John Wick" ));
4465
4566 // when
4667 PetJPA aliceJpa = mapper .map (alice );
@@ -49,6 +70,68 @@ public void testMapFromPetToJPA() {
4970 assertThat (alice ).isNotNull ();
5071 assertThat (aliceJpa ).isNotNull ();
5172 assertThat (mapper ).isNotNull ();
73+ assertThat (aliceJpa .getId ()).isEqualTo (TestRepository .ALICE_ID );
74+ assertThat (alice .getOwner ()).isNotNull ();
75+ assertThat (aliceJpa .getOwner ().getName ()).isEqualTo (TestRepository .OWNER_NAME );
76+ assertThat (aliceJpa .getOwner ().getSurname ()).isEqualTo ("Wick" );
77+ }
78+
79+ @ Test
80+ public void testMapFromJPAToPet () {
81+ // given
82+ Injector injector = createInjector ((Module ) binder ->
83+ binder .bind (EntityManager .class ).toInstance (entityManager )
84+ );
85+ MapperFacade mapper = injector .getInstance (MapperFacade .class );
86+ Database <PetJPA > database = testRepository
87+ .forCase (Example .STANDARD )
88+ .createJpaPetNamedAlice ();
89+ PetJPA aliceJpa = database .getObject ();
90+
91+ // when
92+ Pet alice = mapper .map (aliceJpa );
93+
94+ // then
95+ assertThat (alice ).isNotNull ();
96+ assertThat (aliceJpa ).isNotNull ();
97+ assertThat (mapper ).isNotNull ();
98+ assertThat (alice .getReference ()).isEqualTo (TestRepository .ALICE_ID );
99+ assertThat (alice .getOwner ()).isNotNull ();
100+ assertThat (alice .getOwner ().getReference ())
101+ .isEqualTo (TestRepository .OWNER_ID );
102+ assertThat (alice .getOwner ().getName ())
103+ .isEqualTo (TestRepository .OWNER_NAME + " " + TestRepository .OWNER_SURNAME );
104+ }
105+
106+ @ Test
107+ public void testMissingMappingForToy () {
108+ // given
109+ Injector injector = createInjector ((Module ) binder ->
110+ binder .bind (EntityManager .class ).toInstance (entityManager )
111+ );
112+ MapperFacade mapper = injector .getInstance (MapperFacade .class );
113+ Execution execution = testRepository .forCase (Example .WITH_TOY );
114+ Database <Pet > entityDb = execution .createPetNamedAlice ();
115+ Database <PetJPA > database = execution .createJpaPetNamedAlice ();
116+ bindEntityManager (database );
117+ Pet alice = entityDb .getObject ();
118+
119+ // then
120+ assertThat (alice ).isNotNull ();
121+ assertThat (mapper ).isNotNull ();
122+ thrown .expect (EidIllegalStateException .class );
123+ thrown .expectMessage ("20180425:135245" );
124+ thrown .expectMessage ("Mapping for source class pl.wavesoftware.test.entity.Toy " +
125+ "and target class pl.wavesoftware.test.jpa.ToyJPA is not configured!" );
126+
127+ // when
128+ mapper .map (alice );
129+ }
130+
131+ private void bindEntityManager (Database <?> database ) {
132+ Class <?> cls = any (Class .class );
133+ when (entityManager .find (cls , anyLong ()))
134+ .thenAnswer (new EntityManagerAnswer (database ));
52135 }
53136
54137 private Injector createInjector (Module ... modules ) {
@@ -57,4 +140,15 @@ private Injector createInjector(Module... modules) {
57140 allModules .addAll (Arrays .asList (modules ));
58141 return Guice .createInjector (allModules );
59142 }
143+
144+ @ RequiredArgsConstructor
145+ private static final class EntityManagerAnswer implements Answer <Object > {
146+ private final Database <?> database ;
147+ @ Override
148+ public Object answer (InvocationOnMock invocationOnMock ) {
149+ Class <?> cls = invocationOnMock .getArgument (0 );
150+ Object id = invocationOnMock .getArgument (1 );
151+ return database .find (cls , id );
152+ }
153+ }
60154}
0 commit comments