Skip to content

Commit

Permalink
Add tests for mapping entities with interface attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadkheder committed Feb 5, 2025
1 parent 6c85540 commit 2b7b2dc
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/test/java/com/remondis/remap/interfaceMapping/EntityA.java
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 src/test/java/com/remondis/remap/interfaceMapping/EntityADTO.java
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;
}
}
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");
}
}
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;
}
}
7 changes: 7 additions & 0 deletions src/test/java/com/remondis/remap/interfaceMapping/HasId.java
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;
}
}

0 comments on commit 2b7b2dc

Please sign in to comment.