Skip to content

Commit

Permalink
feat(sdk): added support of validation for continuous range (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelianhan007 authored Nov 27, 2024
1 parent 4322751 commit ad7d188
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import com.consoleconnect.kraken.operator.test.AbstractIntegrationTest;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ spec:
valueMapping: {}
requiredMapping: true
- name: mapper.order.eline.add.bandwidth
title: "Bandwidth of the UNI"
title: "Bandwidth of the ELINE"
source: "@{{productOrderItem[0].product.productConfiguration.bandwidth}}"
target: "@{{speed}}"
sourceLocation: BODY
targetLocation: BODY
requiredMapping: true
- name: mapper.order.eline.add.name
title: "The name of the UNI"
title: "The name of the ELINE"
source: "@{{productOrderItem[0].product.productConfiguration.name}}"
target: "@{{name}}"
sourceLocation: BODY
targetLocation: BODY
requiredMapping: false
- name: mapper.order.eline.add.paymentType
title: "The payment type of the UNI"
title: "The payment type of the ELINE"
source: "@{{productOrderItem[0].product.productConfiguration.paymentType}}"
target: "@{{paymentType}}"
sourceLocation: BODY
Expand Down Expand Up @@ -220,6 +220,4 @@ spec:
target: "@{{productOrderItem[0].product.id}}"
targetLocation: BODY
customizedField: false
requiredMapping: false


requiredMapping: false
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ spec:
- name: mapper.order.uni.add.bandwidth
title: "Bandwidth of the UNI"
source: "@{{productOrderItem[0].product.productConfiguration.bandwidth}}"
sourceType: customized_enum
sourceType: continuousVar
sourceValues:
- 1000
- 10000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
@Getter
@AllArgsConstructor
public enum MappingTypeEnum {
ENUM("enum"),
CUSTOMIZED_ENUM("customized_enum");
ENUM("enum", "normal enumeration"),
DISCRETE_VAR("discreteVar", "discrete variable"),
CONTINUOUS_VAR("continuousVar", "continuous variable");
private final String kind;
private final String desc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -218,8 +219,14 @@ public void checkRequestConstraints(String targetKey, Map<String, Object> inputs
continue;
}
if (MappingTypeEnum.ENUM.getKind().equals(mapper.getSourceType())
|| MappingTypeEnum.CUSTOMIZED_ENUM.getKind().equals(mapper.getSourceType())) {
checkEnumValue(mapper.getSource(), mapper.getTarget(), inputs, mapper.getSourceValues());
|| MappingTypeEnum.DISCRETE_VAR.getKind().equals(mapper.getSourceType())
|| MappingTypeEnum.CONTINUOUS_VAR.getKind().equals(mapper.getSourceType())) {
checkEnumValue(
mapper.getSource(),
mapper.getTarget(),
inputs,
mapper.getSourceValues(),
mapper.getSourceType());
} else if (mapper.getTarget() != null && !mapper.getTarget().contains("@{{")) {
checkConstantValue(mapper.getSource(), mapper.getTarget(), inputs);
} else {
Expand All @@ -229,21 +236,26 @@ public void checkRequestConstraints(String targetKey, Map<String, Object> inputs
}
}

private void checkConstantValue(String expression, String value, Map<String, Object> inputs) {
if (String.valueOf(value).contains("{{")) {
private void checkConstantValue(
String expression, String expectedValue, Map<String, Object> inputs) {
if (String.valueOf(expectedValue).contains("{{")) {
return;
}
String evaluateValue = SpELEngine.evaluate(constructBody(expression), inputs, String.class);
if (!Objects.equals(evaluateValue, value)) {
if (!Objects.equals(evaluateValue, expectedValue)) {
throw KrakenException.unProcessableEntity(
String.format(
"can not process %s = %s, value should be %s",
expression.replace("@{{", "").replace("}}", ""), evaluateValue, value));
expression.replace("@{{", "").replace("}}", ""), evaluateValue, expectedValue));
}
}

private void checkEnumValue(
String source, String target, Map<String, Object> inputs, List<String> valueList) {
String source,
String target,
Map<String, Object> inputs,
List<String> valueList,
String sourceType) {
String constructedBody = constructBody(replaceStarToZero(source));
List<String> params = extractMapperParam(source);
if (CollectionUtils.isEmpty(params)) {
Expand All @@ -257,12 +269,28 @@ private void checkEnumValue(
String.format(
"can not process %s = %s, value should be %s", params.get(0), evaluateValue, target));
}
if (!valueList.contains(evaluateValue)) {
if ((MappingTypeEnum.DISCRETE_VAR.getKind().equals(sourceType)
|| (MappingTypeEnum.ENUM.getKind().equals(sourceType)))
&& !valueList.contains(evaluateValue)) {
throw KrakenException.unProcessableEntity(
String.format(
"can not process %s = %s, value should be in %s",
params.get(0), evaluateValue, valueList));
}
if ((MappingTypeEnum.CONTINUOUS_VAR.getKind().equals(sourceType))) {
List<Double> values = valueList.stream().map(Double::parseDouble).toList();
double min = Collections.min(values);
double max = Collections.max(values);
if (StringUtils.isBlank(evaluateValue)
|| !NumberUtils.isCreatable(evaluateValue)
|| Double.parseDouble(evaluateValue) < min
|| Double.parseDouble(evaluateValue) > max) {
throw KrakenException.unProcessableEntity(
String.format(
"can not process %s = %s, value should be in closed interval[%s, %s]",
params.get(0), evaluateValue, min, max));
}
}
}

private void checkMappingValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void givenTargetKeyNotDeployed_whenOnCheck_thenReturnException() {

@Test
@Order(3)
void givenTargetKeyNoRuls_whenOnCheck_thenReturnException() {
void givenTargetKeyNoRules_whenOnCheck_thenReturnException() {
Map<String, Object> inputs = new HashMap<>();
inputs.put("targetKey", "mef.sonata.api-target.order.eline.read");
inputs.put("mappingMatrixKey", "mef.sonata.api.matrix.address.validation");
Expand Down Expand Up @@ -138,6 +138,17 @@ void givenPayloadMissMappingParam_whenOnCheck_thenReturnError() {
validateOrderRequest("/mockData/productOrderRequestMissMappingParam.json", "productOffering");
}

@Test
@Order(4)
@SneakyThrows
void givenOutOfRangeBandwidth_whenChecking_thenThrowsExceptionMessageAsExpected() {
String requestJson = "/mockData/productOrderRequestOutOfRangeBandwidth.json";
String expected = "interval";
String targetKey = "mef.sonata.api-target.order.uni.add";
String mappingMatrixKey = "mef.sonata.api.matrix.order";
validateOrderRequest(requestJson, expected, targetKey, mappingMatrixKey);
}

@Test
@Order(4)
@SneakyThrows
Expand All @@ -147,11 +158,19 @@ void givenErrorMsgInMatrixItem_whenChecking_thenThrowsExceptionMessageAsExpected
}

private void validateOrderRequest(String request, String matchedMsg) throws IOException {
String targetKey = "mef.sonata.api-target.order.eline.add";
String mappingMatrixKey = "mef.sonata.api.matrix.order";
validateOrderRequest(request, matchedMsg, targetKey, mappingMatrixKey);
}

private void validateOrderRequest(
String request, String matchedMsg, String targetKey, String mappingMatrixKey)
throws IOException {
Map<String, Object> inputs = new HashMap<>();
inputs.put("query", Map.of("buyerId", "test-company"));
inputs.put("body", JsonToolkit.fromJson(readFileToString(request), Object.class));
inputs.put("targetKey", "mef.sonata.api-target.order.eline.add");
inputs.put("mappingMatrixKey", "mef.sonata.api.matrix.order");
inputs.put("targetKey", targetKey);
inputs.put("mappingMatrixKey", mappingMatrixKey);
KrakenException krakenException =
Assertions.assertThrowsExactly(
KrakenException.class, () -> mappingMatrixCheckerActionRunner.onCheck(inputs));
Expand All @@ -172,7 +191,7 @@ private void validateQuoteRequest(String request, String matchedMsg) throws IOEx
}

@Test
@Order(6)
@Order(5)
void givenCheckPath_whenCheckExpect_thenReturnException() {
MappingMatrixCheckerActionRunner.PathCheck pathCheck =
new MappingMatrixCheckerActionRunner.PathCheck(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ spec:
- id: create connection order
path: "/api/v2/company/{username}/connections/layer2"
method: put
serverKey: mef.sonata.api-target-spec.con1722215397621
mappers:
request:
- name: mapper.order.eline.add.buyerId
Expand Down Expand Up @@ -81,20 +82,25 @@ spec:
sourceLocation: BODY
targetLocation: BODY
- name: mapper.order.eline.add.bandwidth
title: "Bandwidth of the UNI"
title: "Bandwidth of the ELINE"
source: "@{{productOrderItem[0].product.productConfiguration.bandwidth}}"
sourceType: discreteVar
sourceValues:
- 1000
- 10000
target: "@{{speed}}"
sourceLocation: BODY
targetLocation: BODY
requiredMapping: true
- name: mapper.order.eline.add.name
title: "The name of the UNI"
title: "The name of the ELINE"
source: "@{{productOrderItem[0].product.productConfiguration.name}}"
target: "@{{name}}"
sourceLocation: BODY
targetLocation: BODY
requiredMapping: false
- name: mapper.order.eline.add.paymentType
title: "The payment type of the UNI"
title: "The payment type of the ELINE"
source: "@{{productOrderItem[0].product.productConfiguration.paymentType}}"
target: "@{{paymentType}}"
sourceLocation: BODY
Expand Down Expand Up @@ -125,7 +131,6 @@ spec:
target: "ACCESS_E_LINE"
sourceLocation: BODY
targetLocation: BODY
# --------------------------------------------------------------------------------------------------------------------
- name: mapper.order.eline.add.buyerCompanyId
title: "The Id of buyer Company"
source: "@{{productOrderItem[0].product.productConfiguration.buyerCompanyId}}"
Expand Down Expand Up @@ -194,6 +199,4 @@ spec:
sourceLocation: BODY
targetLocation: BODY
customizedField: false
requiredMapping: false
serverKey: mef.sonata.api-target-spec.con1722215397621

requiredMapping: false
Loading

0 comments on commit ad7d188

Please sign in to comment.