-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/1009 cleanup dezerializer (#1217)
* introduce Dezerializer Helper * add tests for DezerializerHelper * use correct identifier for id * extract maps to constants * include conversion into method * add helper methods * use helper * add negative tests for DeserializerHelper.java * add deserializer package to allowed packages for businessservice calls * get id from path as backup * add null check in getpath * add new interface and reimplement serialize logic * refactor naming * implement review feedback
- Loading branch information
1 parent
736aa37
commit 534288a
Showing
15 changed files
with
525 additions
and
381 deletions.
There are no files selected for viewing
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
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
48 changes: 18 additions & 30 deletions
48
backend/src/main/java/ch/puzzle/okr/deserializer/CheckInDeserializer.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 |
---|---|---|
@@ -1,51 +1,39 @@ | ||
package ch.puzzle.okr.deserializer; | ||
|
||
import ch.puzzle.okr.dto.checkin.CheckInDto; | ||
import ch.puzzle.okr.dto.checkin.CheckInMetricDto; | ||
import ch.puzzle.okr.dto.checkin.CheckInOrdinalDto; | ||
import ch.puzzle.okr.models.keyresult.KeyResult; | ||
import ch.puzzle.okr.dto.checkin.*; | ||
import ch.puzzle.okr.service.business.KeyResultBusinessService; | ||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.springframework.http.HttpStatus; | ||
import com.fasterxml.jackson.databind.*; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.io.IOException; | ||
|
||
import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_METRIC; | ||
import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_ORDINAL; | ||
import static ch.puzzle.okr.Constants.*; | ||
|
||
@Component | ||
public class CheckInDeserializer extends JsonDeserializer<CheckInDto> { | ||
public class CheckInDeserializer extends JsonDeserializer<CheckInDto> implements MetricOrdinalDeserializer { | ||
|
||
private final DeserializerHelper deserializerHelper; | ||
private final KeyResultBusinessService keyResultBusinessService; | ||
|
||
public CheckInDeserializer(KeyResultBusinessService keyResultBusinessService) { | ||
public CheckInDeserializer(DeserializerHelper deserializerHelper, | ||
KeyResultBusinessService keyResultBusinessService) { | ||
this.deserializerHelper = deserializerHelper; | ||
this.keyResultBusinessService = keyResultBusinessService; | ||
} | ||
|
||
@Override | ||
public CheckInDto deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) | ||
throws IOException, JacksonException { | ||
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec(); | ||
ObjectNode root = mapper.readTree(jsonParser); | ||
if (root.has("keyResultId")) { | ||
KeyResult keyResultOfCheckIn = keyResultBusinessService.getEntityById(root.get("keyResultId").asLong()); | ||
if (KEY_RESULT_TYPE_METRIC.equals(keyResultOfCheckIn.getKeyResultType())) { | ||
return mapper.readValue(root.toString(), CheckInMetricDto.class); | ||
} else if (KEY_RESULT_TYPE_ORDINAL.equals(keyResultOfCheckIn.getKeyResultType())) { | ||
return mapper.readValue(root.toString(), CheckInOrdinalDto.class); | ||
} else { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "unsupported checkIn DTO to deserialize"); | ||
} | ||
} else { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, | ||
"missing keyResult ID to deserialize checkIn DTO"); | ||
throws IOException { | ||
return deserializerHelper.deserializeMetricOrdinal(jsonParser, CHECK_IN_MAP, this); | ||
} | ||
|
||
@Override | ||
public String getKeyResultType(JsonNode root) { | ||
JsonNode keyResultIdNode = root.get(CHECK_IN_KEY_RESULT_ID_ATTRIBUTE_NAME); | ||
if (keyResultIdNode == null) { | ||
return null; | ||
} | ||
return keyResultBusinessService.getEntityById(keyResultIdNode.asLong()).getKeyResultType(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/ch/puzzle/okr/deserializer/DeserializerHelper.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,33 @@ | ||
package ch.puzzle.okr.deserializer; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class DeserializerHelper { | ||
|
||
public <T> T deserializeMetricOrdinal(JsonParser jsonParser, Map<String, Class<? extends T>> validTypes, | ||
MetricOrdinalDeserializer deserializer) throws IOException { | ||
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec(); | ||
ObjectNode root = mapper.readTree(jsonParser); | ||
|
||
String keyResultType = deserializer.getKeyResultType(root); | ||
if (isKeyResultTypeInvalid(keyResultType, validTypes)) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "unsupported entity DTO to deserialize"); | ||
} | ||
|
||
Class<? extends T> targetClass = validTypes.get(keyResultType); | ||
return mapper.readValue(root.toString(), targetClass); | ||
} | ||
|
||
private <T> boolean isKeyResultTypeInvalid(String type, Map<String, Class<? extends T>> validTypes) { | ||
return type == null || !validTypes.containsKey(type); | ||
} | ||
} |
43 changes: 21 additions & 22 deletions
43
backend/src/main/java/ch/puzzle/okr/deserializer/KeyResultDeserializer.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 |
---|---|---|
@@ -1,35 +1,34 @@ | ||
package ch.puzzle.okr.deserializer; | ||
|
||
import ch.puzzle.okr.dto.keyresult.KeyResultDto; | ||
import ch.puzzle.okr.dto.keyresult.KeyResultMetricDto; | ||
import ch.puzzle.okr.dto.keyresult.KeyResultOrdinalDto; | ||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import com.fasterxml.jackson.databind.*; | ||
|
||
import java.io.IOException; | ||
|
||
import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_METRIC; | ||
import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_ORDINAL; | ||
import static ch.puzzle.okr.Constants.*; | ||
|
||
public class KeyResultDeserializer extends JsonDeserializer<KeyResultDto> implements MetricOrdinalDeserializer { | ||
|
||
private final DeserializerHelper deserializerHelper; | ||
|
||
public KeyResultDeserializer(DeserializerHelper deserializerHelper) { | ||
this.deserializerHelper = deserializerHelper; | ||
} | ||
|
||
public class KeyResultDeserializer extends JsonDeserializer<KeyResultDto> { | ||
@Override | ||
public KeyResultDto deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) | ||
throws IOException, JacksonException { | ||
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec(); | ||
ObjectNode root = mapper.readTree(jsonParser); | ||
String keyResultAttribute = "keyResultType"; | ||
if (root.has(keyResultAttribute) && root.get(keyResultAttribute).asText().equals(KEY_RESULT_TYPE_METRIC)) { | ||
return mapper.readValue(root.toString(), KeyResultMetricDto.class); | ||
} else if (root.has(keyResultAttribute) | ||
&& root.get(keyResultAttribute).asText().equals(KEY_RESULT_TYPE_ORDINAL)) { | ||
return mapper.readValue(root.toString(), KeyResultOrdinalDto.class); | ||
throws IOException { | ||
|
||
return deserializerHelper.deserializeMetricOrdinal(jsonParser, KEY_RESULT_MAP, this); | ||
} | ||
|
||
@Override | ||
public String getKeyResultType(JsonNode root) { | ||
JsonNode keyResultId = root.get(KEY_RESULT_TYPE_ATTRIBUTE_NAME); | ||
if (keyResultId == null) { | ||
return null; | ||
} | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "unsupported keyResult DTO to deserialize"); | ||
return keyResultId.asText(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/ch/puzzle/okr/deserializer/MetricOrdinalDeserializer.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,7 @@ | ||
package ch.puzzle.okr.deserializer; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public interface MetricOrdinalDeserializer { | ||
String getKeyResultType(JsonNode root); | ||
} |
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
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
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
Oops, something went wrong.