Skip to content

Commit

Permalink
- Test Kit - Enabling test data loading from file (#235)
Browse files Browse the repository at this point in the history
* - Test Kit - Enabling test data loading from file

* - Test Kit - Enabling test data loading from file

* - Formatting

* - Formatting

* - Refactoring - removing double code

* - Refactoring - replace loop with stream

* - Fixing typo in changelog

* - Replacing custom "file:" constant with ResourceUtils.FILE_URL_PREFIX

* - Code formatting fixed

* - File prefix changed to resource-file:

* - Formatting fixed

* adding commons-io dependency

* refactoring commons-exec dependency version

---------

Co-authored-by: Tucakovic, Vladimir <[email protected]>
Co-authored-by: Leto Bukarica <[email protected]>
  • Loading branch information
3 people authored Sep 19, 2023
1 parent c5cd9a4 commit f2ce10e
Show file tree
Hide file tree
Showing 7 changed files with 474 additions and 10 deletions.
5 changes: 5 additions & 0 deletions changelogs/feature/load test data from files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"author": "vladiIkor",
"pullrequestId": "235",
"message": "Set value of 'body' in test-kit-definition as reference to a file on the classpath"
}
412 changes: 412 additions & 0 deletions docs-snapshot/test-kit.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
<mapstruct.version>1.5.3.Final</mapstruct.version>
<commons-text.version>1.10.0</commons-text.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons-io.version>2.13.0</commons-io.version>
<commons-exec.version>1.3</commons-exec.version>
<asm.version>9.4</asm.version> <!-- Spring is using 9.1, Cxf is using 9.4, when Spring upgrades to 9.4, then explicit dependency can be removed -->
<cxf.version>4.0.2</cxf.version> <!-- keep in sync with camel-dependencies cxf-version property -->
<snakeyaml.version>2.0</snakeyaml.version>
Expand Down Expand Up @@ -220,6 +222,16 @@
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>${commons-exec.version}</version>
</dependency>

<!--maven dependencies-->
<dependency>
Expand Down
5 changes: 4 additions & 1 deletion sip-test-kit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package de.ikor.sip.foundation.testkit.configurationproperties.models;

import static de.ikor.sip.foundation.core.util.SIPExchangeHelper.filterNonSerializableHeaders;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import lombok.SneakyThrows;
import org.apache.camel.Exchange;
import org.apache.camel.support.MessageHelper;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;

/** Class that holds a single message used in test cases */
@Data
public class MessageProperties {
private static final String RESOURCE_FILE_PREFIX = "resource-file:";
private String body;
private Map<String, Object> headers = new HashMap<>();

Expand All @@ -26,4 +32,15 @@ public static MessageProperties mapToMessageProperties(Exchange exchange) {
messageProperties.setHeaders(filterNonSerializableHeaders(exchange));
return messageProperties;
}

@SneakyThrows
public String getBody() {
if (isNotBlank(body) && body.startsWith(RESOURCE_FILE_PREFIX)) {
String bodyLocation = body.substring(RESOURCE_FILE_PREFIX.length());
body =
FileUtils.readFileToString(
new ClassPathResource(bodyLocation).getFile(), StandardCharsets.UTF_8);
}
return body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import de.ikor.sip.foundation.testkit.workflow.thenphase.result.ValidationResult;
import de.ikor.sip.foundation.testkit.workflow.thenphase.validator.ExchangeValidator;
import de.ikor.sip.foundation.testkit.workflow.thenphase.validator.TestCaseValidator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -99,14 +98,10 @@ private boolean evaluateValidationResults(List<ValidationResult> validationResul

private List<ValidationResult> runValidators(
Exchange executionResult, Exchange expectedResponse) {
List<ValidationResult> validationResults = new ArrayList<>();
for (ExchangeValidator validator : this.exchangeValidators) {
if (validator.isApplicable(executionResult, expectedResponse)) {
validationResults.add(validator.execute(executionResult, expectedResponse));
}
}

return validationResults;
return this.exchangeValidators.stream()
.filter(validator -> validator.isApplicable(executionResult, expectedResponse))
.map(validator -> validator.execute(executionResult, expectedResponse))
.toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.ikor.sip.foundation.testkit.workflow;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import de.ikor.sip.foundation.testkit.configurationproperties.models.MessageProperties;
import java.io.FileNotFoundException;
import org.junit.jupiter.api.Test;

class MessagePropertiesTest {
MessageProperties subject = new MessageProperties();

@Test
void when_BodyReferencesFile_thenBodyContentIsReadFromFile() {
subject.setBody("resource-file:body.json");
FileNotFoundException fileNotFoundException =
assertThrows(FileNotFoundException.class, () -> subject.getBody());
assertThat(fileNotFoundException.getMessage()).contains("body.json");
}
}

0 comments on commit f2ce10e

Please sign in to comment.