-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for mapping entities with interface attributes
- Loading branch information
1 parent
6c85540
commit 2b7b2dc
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/test/java/com/remondis/remap/interfaceMapping/EntityA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.remondis.remap.interfaceMapping; | ||
|
||
public class EntityA implements HasId { | ||
private String name; | ||
|
||
public EntityA() { | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/com/remondis/remap/interfaceMapping/EntityADTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.remondis.remap.interfaceMapping; | ||
|
||
public class EntityADTO { | ||
private String name; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/com/remondis/remap/interfaceMapping/EntityMappingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.remondis.remap.interfaceMapping; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.remondis.remap.AssertMapping; | ||
import com.remondis.remap.Mapper; | ||
import com.remondis.remap.Mapping; | ||
|
||
public class EntityMappingTest { | ||
|
||
private Mapper<EntityA, EntityADTO> entityMapperWithoutId; | ||
private Mapper<EntityA, EntityWithIdDTO> entityMapperWithId; | ||
|
||
@Before | ||
public void setup() { | ||
this.entityMapperWithoutId = Mapping.from(EntityA.class) | ||
.to(EntityADTO.class) | ||
.omitInSource(HasId::getId) | ||
.mapper(); | ||
|
||
this.entityMapperWithId = Mapping.from(EntityA.class) | ||
.to(EntityWithIdDTO.class) | ||
.mapper(); | ||
} | ||
|
||
@Test | ||
public void shouldMapEntityWithoutInterfaceAttribute() { | ||
EntityA entity = new EntityA(); | ||
entity.setName("Test Entity"); | ||
EntityADTO dto = entityMapperWithoutId.map(entity); | ||
|
||
assertEquals("Test Entity", dto.getName()); | ||
} | ||
|
||
@Test | ||
public void shouldMapEntityWithInterfaceAttribute() { | ||
EntityA entity = new EntityA(); | ||
entity.setName("Test Entity"); | ||
EntityWithIdDTO dto = entityMapperWithId.map(entity); | ||
|
||
assertEquals("Test Entity", dto.getName()); | ||
assertEquals(Long.valueOf(1), dto.getId()); | ||
} | ||
|
||
@Test | ||
public void shouldComplainAboutImplicitMapping() { | ||
Mapper<EntityA, EntityADTO> implicitMapper = Mapping.from(EntityA.class) | ||
.to(EntityADTO.class) | ||
.omitInSource(EntityA::getId) | ||
.mapper(); | ||
|
||
assertThatThrownBy(() -> AssertMapping.of(implicitMapper) | ||
.expectNoImplicitMappings() | ||
.ensure()).isInstanceOf(AssertionError.class) | ||
.hasMessageContaining("The mapper was expected to create no implicit mappings"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/com/remondis/remap/interfaceMapping/EntityWithIdDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.remondis.remap.interfaceMapping; | ||
|
||
public class EntityWithIdDTO { | ||
private Long id; | ||
private String name; | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.remondis.remap.interfaceMapping; | ||
|
||
public interface HasId { | ||
default Long getId() { | ||
return 1L; | ||
} | ||
} |