Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelianhan007 committed Dec 3, 2024
1 parent def7b58 commit 9d3c761
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.consoleconnect.kraken.operator.gateway.service;

import com.consoleconnect.kraken.operator.core.dto.StateValueMappingDto;
import com.consoleconnect.kraken.operator.core.model.UnifiedAsset;
import com.consoleconnect.kraken.operator.core.model.facet.ComponentAPITargetFacets;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import com.consoleconnect.kraken.operator.core.toolkit.JsonToolkit;
import com.consoleconnect.kraken.operator.gateway.CustomConfig;
import com.consoleconnect.kraken.operator.gateway.helper.AssetConfigReader;
import com.consoleconnect.kraken.operator.test.MockIntegrationTest;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.HashMap;
import java.util.Map;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ContextConfiguration;

@Slf4j
@MockIntegrationTest
@ContextConfiguration(classes = CustomConfig.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class RenderRequestServiceTest extends AssetConfigReader {

@Autowired private UnifiedAssetService unifiedAssetService;

@SpyBean RenderRequestService renderRequestService;

@SneakyThrows
@Test
void givenArrayItems_whenHandleBody_thenReturnOK() {
Map<String, Object> map =
JsonToolkit.fromJson(
readFileToString("mockData/quoteArrayRequest.json"),
new TypeReference<Map<String, Object>>() {});
Map<String, Object> inputs = new HashMap<>();
inputs.put("body", map);
StateValueMappingDto stateValueMappingDto = new StateValueMappingDto();
UnifiedAsset targetAsset =
getTarget(
"deployment-config/components/api-targets/api-target.quote.uni.add.sync.yaml",
"deployment-config/components/api-targets-mappers/api-target-mapper.quote.uni.add.sync.yaml");
ComponentAPITargetFacets facets =
UnifiedAsset.getFacets(targetAsset, ComponentAPITargetFacets.class);
renderRequestService.handleBody(facets, stateValueMappingDto, inputs);
String transformedRequest =
com.consoleconnect.kraken.operator.core.toolkit.StringUtils.compact(
facets.getEndpoints().get(0).getRequestBody());
String expectedResult = readCompactedFile("mockData/expected-quote-array-request.json");
Assertions.assertEquals(expectedResult, transformedRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"ports" : [ {
"durationUnit" : "${body.quoteItem[0].requestedQuoteItemTerm.duration.units}",
"dcf" : "${body.quoteItem[0].product.place[0].id}",
"speed" : "${body.quoteItem[0].product.productConfiguration.bandwidth}"
}, {
"durationUnit" : "${body.quoteItem[1].requestedQuoteItemTerm.duration.units}",
"dcf" : "${body.quoteItem[1].product.place[0].id}",
"speed" : "${body.quoteItem[1].product.productConfiguration.bandwidth}"
}, {
"durationUnit" : "${body.quoteItem[2].requestedQuoteItemTerm.duration.units}",
"dcf" : "${body.quoteItem[2].product.place[0].id}",
"speed" : "${body.quoteItem[2].product.productConfiguration.bandwidth}"
} ]
}

0 comments on commit 9d3c761

Please sign in to comment.