Skip to content

Commit

Permalink
[Gepardec/mega#727] DRYed code
Browse files Browse the repository at this point in the history
  • Loading branch information
sgartner03 committed Jan 16, 2024
1 parent d6477cb commit d739978
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 41 deletions.
13 changes: 3 additions & 10 deletions src/main/java/com/gepardec/mega/zep/mapper/AbsenceTimeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ public static AbsenceTime map(FehlzeitType fehlzeitType) {
return null;
}

LocalDate startDate = parseDate(fehlzeitType.getStartdatum());
LocalDate endDate = fehlzeitType.getEnddatum() == null ?
null : parseDate(fehlzeitType.getEnddatum());
Map<String, String> attributes = fehlzeitType.getAttributes() == null ?
null : convertAttributesToMap(fehlzeitType.getAttributes());
LocalDate startDate = MapperUtil.convertStringToDate(fehlzeitType.getStartdatum());
LocalDate endDate = MapperUtil.convertStringToDate(fehlzeitType.getEnddatum());
Map<String, String> attributes = MapperUtil.convertAttributesToMap(fehlzeitType.getAttributes());

return AbsenceTime.builder()
.id(fehlzeitType.getId())
Expand All @@ -64,9 +62,4 @@ public static AbsenceTime map(FehlzeitType fehlzeitType) {
.build();
}

public static Map<String, String> convertAttributesToMap(AttributesType attributes) {
return attributes.getAttribute().stream()
.filter(Objects::nonNull)
.collect(Collectors.toMap(AttributeType::getName, AttributeType::getValue));
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/gepardec/mega/zep/mapper/MapperUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.gepardec.mega.zep.mapper;

import java.time.LocalDate;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.gepardec.mega.domain.utils.DateUtils;
import de.provantis.zep.AttributesType;
import de.provantis.zep.AttributeType;


public class MapperUtil {
public static Map<String, String> convertAttributesToMap(AttributesType attributes) {
if (attributes == null)
return null;
return attributes.getAttribute().stream()
.filter(Objects::nonNull)
.collect(Collectors.toMap(de.provantis.zep.AttributeType::getName, AttributeType::getValue));
}

public static LocalDate convertStringToDate(String dateString) {
if (dateString == null)
return null;

return DateUtils.parseDate(dateString);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public static ProjectTime map(ProjektzeitType projektzeitType) {
if (projektzeitType == null)
return null;

LocalDate date = projektzeitType.getDatum() == null ?
null : parseDate(projektzeitType.getDatum());
LocalDate date = MapperUtil.convertStringToDate(projektzeitType.getDatum());

Map<String, String> attributes = projektzeitType.getAttributes() == null ?
null : convertAttributesToMap(projektzeitType.getAttributes());
Map<String, String> attributes = MapperUtil.convertAttributesToMap(projektzeitType.getAttributes());


return ProjectTime.builder()
Expand Down Expand Up @@ -68,9 +66,4 @@ public static ProjectTime map(ProjektzeitType projektzeitType) {
.build();
}

public static Map<String, String> convertAttributesToMap(AttributesType attributes) {
return attributes.getAttribute().stream()
.filter(Objects::nonNull)
.collect(Collectors.toMap(AttributeType::getName, AttributeType::getValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,6 @@ void withFullSettings_thenReturnsAbsenceTimeObject() {
assertThat(at.getId()).isEqualTo(fzt.getId());
}

@Test
void withStartDateString_thenReturnsLocalDate() {
FehlzeitType fzt = new FehlzeitType();
fzt.setUserId("4");
fzt.setStartdatum("2002-11-23");
fzt.setFehlgrund("KA");
AbsenceTime at = AbsenceTimeMapper.map(fzt);
String absenceTimeDateString = at.getFromDate().toString();
assertThat(fzt.getStartdatum()).isEqualTo(absenceTimeDateString);
}

@Test
void whenEmptyList_thenReturnsEmptyList() {
assertThat(AbsenceTimeMapper.mapList(List.of())).isEmpty();
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/com/gepardec/mega/zep/mapper/MapperUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.gepardec.mega.zep.mapper;

import de.provantis.zep.AttributeType;
import de.provantis.zep.AttributesType;
import com.gepardec.mega.domain.model.ProjectTime;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class MapperUtilTest {
@Test
void DateString_convertsToLocalDate() {
String dateString = "2002-11-23";
LocalDate date = MapperUtil.convertStringToDate(dateString);
assertThat(date.toString()).isEqualTo(dateString);
}

@Test
void whenAttributes_thenReturnsAttributesMap() {
AttributeType attribute = new de.provantis.zep.AttributeType();
attribute.setName("gilde");
attribute.setValue("cia");
AttributeType attribute2 = new de.provantis.zep.AttributeType();
attribute2.setName("office");
attribute2.setValue("wien");

List<AttributeType> attributesList = new ArrayList<>();
attributesList.add(attribute);
attributesList.add(attribute2);

AttributesType attributes = new de.provantis.zep.AttributesType();
attributes.setAttribute(attributesList);

Map<String, String> attributesMap = MapperUtil.convertAttributesToMap(attributes);

assertThat(attributesMap.get(attribute.getName())).isEqualTo(attribute.getValue());
assertThat(attributesMap.get(attribute2.getName())).isEqualTo(attribute2.getValue());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,6 @@ void withFullSettings_thenReturnsAbsenceTimeObject() {
assertThat(pt.getModified()).isEqualTo(pzt.getModified());
}

@Test
void withStartDateString_thenReturnsLocalDate() {
de.provantis.zep.FehlzeitType fzt = new de.provantis.zep.FehlzeitType();
fzt.setUserId("4");
fzt.setStartdatum("2002-11-23");
fzt.setFehlgrund("KA");
AbsenceTime at = AbsenceTimeMapper.map(fzt);
String absenceTimeDateString = at.getFromDate().toString();
assertThat(fzt.getStartdatum()).isEqualTo(absenceTimeDateString);
}

@Test
void whenEmptyList_thenReturnsEmptyList() {
assertThat(ProjectTimeMapper.mapList(List.of())).isEmpty();
Expand Down

0 comments on commit d739978

Please sign in to comment.