-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-2081: Fix "resolve dependent variable" logic of Batch reques…
…t resolver
- Loading branch information
Showing
2 changed files
with
127 additions
and
23 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
107 changes: 107 additions & 0 deletions
107
fineract-core/src/test/java/org/apache/fineract/batch/service/ResolutionHelperTest.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,107 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.fineract.batch.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.jayway.jsonpath.ReadContext; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.fineract.batch.domain.BatchRequest; | ||
import org.apache.fineract.batch.domain.BatchResponse; | ||
import org.apache.fineract.batch.exception.BatchReferenceInvalidException; | ||
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ResolutionHelperTest { | ||
|
||
private final FromJsonHelper fromJsonHelper = new FromJsonHelper(); | ||
private final ResolutionHelper resolutionHelper = new ResolutionHelper(fromJsonHelper); | ||
|
||
@Test | ||
void testBuildNodesTreeWithValidRequests() { | ||
List<BatchRequest> requests = new ArrayList<>(); | ||
BatchRequest firstRequest = new BatchRequest(); | ||
firstRequest.setRequestId(1L); | ||
firstRequest.setBody("{\"key1\": \"value1\", \"key2\": 12, \"key3\": null, \"key4\": false}"); | ||
firstRequest.setRelativeUrl("/resource/id"); | ||
requests.add(firstRequest); | ||
BatchRequest secondRequest = new BatchRequest(); | ||
secondRequest.setRequestId(2L); | ||
secondRequest.setReference(1L); | ||
secondRequest.setRelativeUrl("/resource/id"); | ||
secondRequest.setBody("{\"key1\": { \"subKey\": false }, \"key2\": [1,2,3]}"); | ||
requests.add(secondRequest); | ||
BatchRequest thirdRequest = new BatchRequest(); | ||
thirdRequest.setRequestId(3L); | ||
thirdRequest.setReference(2L); | ||
requests.add(thirdRequest); | ||
List<ResolutionHelper.BatchRequestNode> nodes = resolutionHelper.buildNodesTree(requests); | ||
assertEquals(1, nodes.size()); | ||
assertEquals(1, nodes.get(0).getRequest().getRequestId()); | ||
assertEquals("{\"key1\": \"value1\", \"key2\": 12, \"key3\": null, \"key4\": false}", nodes.get(0).getRequest().getBody()); | ||
assertEquals(1, nodes.get(0).getChildNodes().size()); | ||
assertEquals(2, nodes.get(0).getChildNodes().get(0).getRequest().getRequestId()); | ||
assertEquals("{\"key1\": { \"subKey\": false }, \"key2\": [1,2,3]}", nodes.get(0).getChildNodes().get(0).getRequest().getBody()); | ||
} | ||
|
||
@Test | ||
void testBuildNodesTreeWithInvalidReference() { | ||
List<BatchRequest> requests = new ArrayList<>(); | ||
BatchRequest invalidRequest = new BatchRequest(); | ||
invalidRequest.setRequestId(2L); | ||
// Not existing reference | ||
invalidRequest.setReference(1L); | ||
requests.add(invalidRequest); | ||
assertThrows(BatchReferenceInvalidException.class, () -> resolutionHelper.buildNodesTree(requests)); | ||
} | ||
|
||
@Test | ||
void testResolveRequestWithValidDependenciesResolveParameterFromResponse() { | ||
BatchRequest batchRequest = new BatchRequest(); | ||
batchRequest.setBody("{\"key1\": \"$.value1\"}"); | ||
batchRequest.setRelativeUrl("/resource/$.id?key=value"); | ||
BatchResponse parentResponse = new BatchResponse(); | ||
parentResponse.setBody("{\"value1\": \"resolvedValue\", \"id\": \"123\"}"); | ||
ReadContext readContext = mock(ReadContext.class); | ||
when(readContext.read("$.value1")).thenReturn("resolvedValue"); | ||
when(readContext.read("$.id")).thenReturn("123"); | ||
BatchRequest resolvedRequest = resolutionHelper.resolveRequest(batchRequest, parentResponse); | ||
assertNotNull(resolvedRequest); | ||
assertEquals("{\"key1\":\"resolvedValue\"}", resolvedRequest.getBody()); | ||
assertEquals("/resource/123?key=value", resolvedRequest.getRelativeUrl()); | ||
} | ||
|
||
@Test | ||
void testResolveRequestWithNoDependencies() { | ||
BatchRequest batchRequest = new BatchRequest(); | ||
batchRequest.setBody("{\"key1\": \"value1\",\"key2\": { \"subKey\": false }, \"key3\": [1,2,3], \"key4\": null}"); | ||
batchRequest.setRelativeUrl("/resource/id"); | ||
BatchResponse parentResponse = new BatchResponse(); | ||
parentResponse.setBody("{\"value2\": \"not used\"}"); | ||
BatchRequest resolvedRequest = resolutionHelper.resolveRequest(batchRequest, parentResponse); | ||
assertNotNull(resolvedRequest); | ||
assertEquals("{\"key1\":\"value1\",\"key2\":{\"subKey\":false},\"key3\":[1,2,3],\"key4\":null}", resolvedRequest.getBody()); | ||
assertEquals("/resource/id", resolvedRequest.getRelativeUrl()); | ||
} | ||
} |