Skip to content

56 jsonaccesessor #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.7'
services:

no-code-qa-platform:
image: 'amaralus/qa-platform:0.0.31'
image: 'amaralus/qa-platform:0.0.32'
container_name: qa-platform
volumes:
- ./data/qa-platform:/rocksdb
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "no-code-qa-platform",
"version": "0.0.31",
"version": "0.0.32",
"license": "Apache-2.0",
"dependencies": {
"react": "^16.5.2",
Expand Down
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>apps.amaralus</groupId>
<artifactId>no-code-qa-platform</artifactId>
<version>0.0.31</version>
<version>0.0.32</version>
<packaging>jar</packaging>

<licenses>
Expand Down Expand Up @@ -92,7 +92,11 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${spring-docs.version}</version>
</dependency>

<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.rocksdb</groupId>
<artifactId>rocksdbjni</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected void afterCreate(M model) {
var dataset = datasetService.create(
new Dataset(createDatasetName(model.getId()), null, true, new Backlink<>(modelClass, model.getId())));
model.setDataset(dataset.getId());
repository.save(model);
}

private String createDatasetName(I id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public class Placeholder {

public static final Pattern BRACES_PATTERN = Pattern.compile("\\{\\{(.*?)}}");
public static final Pattern LOCATION_PATTERN = Pattern.compile("([a-zA-Z0-9-_]+)");
public static final Pattern VARIABLE_PATTERN = Pattern.compile("([a-zA-Z0-9-_.\\[\\]]+)");
public static final Pattern LOCATION_VARIABLE_PATTERN =
Pattern.compile(LOCATION_PATTERN + ":" + LOCATION_PATTERN);
public static final Pattern FULL_PATH_PATTERN =
Pattern.compile(LOCATION_PATTERN + "#(\\d+):" + LOCATION_PATTERN);
Pattern.compile(LOCATION_PATTERN + "#(\\d+):" + VARIABLE_PATTERN);

private final String path;
private final String location;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package apps.amaralus.qa.platform.placeholder.accessor;

public interface DataAccessor {
Object read(Object value, String path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package apps.amaralus.qa.platform.placeholder.accessor;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

@Service
public class DataAccessorProvider {

private final List<DataAccessor> dataAccessors;

public DataAccessorProvider(List<DataAccessor> dataAccessors) {
this.dataAccessors = dataAccessors;
}

public Object getVariableByPath(String path, Object variable) {
if (path == null) return variable;

return dataAccessors.stream()
.map(dataAccessor -> Optional.ofNullable(dataAccessor.read(variable, path)))
.findAny()
.flatMap(Function.identity())
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package apps.amaralus.qa.platform.placeholder.accessor;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Getter
@Component
public class JsonAccessor implements DataAccessor {

@Override
public Object read(Object json, String path) {
String jsonPath = "$." + path;

try {
return JsonPath.read(json.toString(), jsonPath);
} catch (PathNotFoundException e) {
log.info(e.getMessage());
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import apps.amaralus.qa.platform.dataset.model.DatasetModel;
import apps.amaralus.qa.platform.placeholder.DefaultPlaceholderType;
import apps.amaralus.qa.platform.placeholder.Placeholder;
import apps.amaralus.qa.platform.placeholder.accessor.DataAccessorProvider;
import apps.amaralus.qa.platform.placeholder.generate.GeneratedPlaceholderType;
import apps.amaralus.qa.platform.placeholder.generate.PlaceholderGeneratorsProvider;
import lombok.EqualsAndHashCode;
Expand All @@ -19,6 +20,7 @@ public class PlaceholderResolver {

private final ResolvingContext resolvingContext;
private final PlaceholderGeneratorsProvider generatorsProvider;
private final DataAccessorProvider dataAccessorProvider;

public Object resolve(Placeholder placeholder) {
return new RecursiveStackResolver().resolve(placeholder);
Expand All @@ -43,7 +45,9 @@ Object resolve(Placeholder placeholder) {
else
stack.push(datasetVariable);

var value = dataset.map(model -> model.getVariable(datasetVariable.variable)).orElse(null);
var value = dataset.map(model -> model.getVariable(datasetVariable.variable))
.map(v -> dataAccessorProvider.getVariableByPath(datasetVariable.pathToNestedVariable, v))
.orElse(null);

if (value instanceof String text)
value = resolve(text);
Expand Down Expand Up @@ -92,9 +96,16 @@ Optional<DatasetModel> findDataset(Placeholder placeholder, DatasetVariable data
public static class DatasetVariable {
Long datasetId;
String variable;
String pathToNestedVariable;

public DatasetVariable(String variable) {
this.variable = variable;
if (variable.contains(".")) {
this.variable = variable.substring(0, variable.indexOf("."));
this.pathToNestedVariable = variable.substring(variable.indexOf(".") + 1);
} else {
this.variable = variable;
this.pathToNestedVariable = null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package apps.amaralus.qa.platform.placeholder.resolve.config;

import apps.amaralus.qa.platform.placeholder.accessor.DataAccessorProvider;
import apps.amaralus.qa.platform.placeholder.generate.PlaceholderGeneratorsProvider;
import apps.amaralus.qa.platform.placeholder.resolve.PlaceholderResolver;
import apps.amaralus.qa.platform.placeholder.resolve.ResolvingContext;
Expand All @@ -11,7 +12,8 @@ public class PlaceholderResolvingConfiguration {

@Bean
public PlaceholderResolver defaultPlaceholderResolver(ResolvingContext resolvingContext,
PlaceholderGeneratorsProvider placeholderGeneratorsProvider) {
return new PlaceholderResolver(resolvingContext, placeholderGeneratorsProvider);
PlaceholderGeneratorsProvider placeholderGeneratorsProvider,
DataAccessorProvider dataAccessorProvider) {
return new PlaceholderResolver(resolvingContext, placeholderGeneratorsProvider, dataAccessorProvider);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package apps.amaralus.qa.platform.runtime.schedule;

import apps.amaralus.qa.platform.placeholder.accessor.DataAccessorProvider;
import apps.amaralus.qa.platform.placeholder.generate.PlaceholderGeneratorsProvider;
import apps.amaralus.qa.platform.placeholder.resolve.PlaceholderResolver;
import apps.amaralus.qa.platform.project.context.ProjectContext;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class TestPlanFactory {
private final ExecutionScheduler parallelExecutionScheduler;
private final PlaceholderResolvingContextFactory resolvingContextFactory;
private final PlaceholderGeneratorsProvider placeholderGeneratorsProvider;
private final DataAccessorProvider dataAccessorProvider;
private final TestCaseService testCaseService;
private final TestCaseFactory testCaseFactory;
private final ProjectContext projectContext;
Expand Down Expand Up @@ -92,7 +94,7 @@ private void initializeTestContext(ExecutableTestPlan testPlan, PlaceholderResol
testPlan.getTestInfo(),
testCase.getTestInfo(),
testStep.getTestInfo(),
new PlaceholderResolver(resolvingContext, placeholderGeneratorsProvider),
new PlaceholderResolver(resolvingContext, placeholderGeneratorsProvider, dataAccessorProvider),
contextFactory.getTestCaseDataset()
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package apps.amaralus.qa.platform.placeholder;

import apps.amaralus.qa.platform.placeholder.accessor.JsonAccessor;
import org.junit.jupiter.api.Test;

public class JsonAccessorTest {

@Test
void jsonPathTest() {
JsonAccessor jsonAccessor = new JsonAccessor();

String json = """
{
"some1" : "Hi",
"some2" : [
{
"hello" : "world"
},
{
"hello" : "world1"
}
],
"some3" : ["1", "2", "3"]
}
""";

System.out.println(jsonAccessor.read(json, "some1"));
System.out.println(jsonAccessor.read(json, "some2"));
System.out.println(jsonAccessor.read(json, "some2[0]"));
System.out.println(jsonAccessor.read(json, "some2[0].hello"));
System.out.println(jsonAccessor.read(json, "some3"));
System.out.println(jsonAccessor.read(json, "some3[0]"));
System.out.println(jsonAccessor.read(json, "some4"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import apps.amaralus.qa.platform.dataset.DatasetService;
import apps.amaralus.qa.platform.dataset.alias.AliasService;
import apps.amaralus.qa.platform.dataset.model.api.Dataset;
import apps.amaralus.qa.platform.folder.FolderService;
import apps.amaralus.qa.platform.folder.model.Folder;
import apps.amaralus.qa.platform.placeholder.Placeholder;
import apps.amaralus.qa.platform.placeholder.resolve.PlaceholderResolver;
import apps.amaralus.qa.platform.project.ProjectService;
Expand Down Expand Up @@ -71,6 +73,7 @@ class FunctionalManualTest {
TestReportService testReportService;

Project project;
Dataset dataset;

@BeforeEach
void before() {
Expand Down Expand Up @@ -119,6 +122,92 @@ void placeholders() {
assertTrue(true);
}

@Test
void jsonBasePlaceholders() {
projectContext.setProjectId("myProject");
Folder folder = folderService.findById(project.getRootFolder()).get();

datasetService.setVariable(folder.getDataset(), "var", """
{
"some1" : "Hi",
"some2" : [
{
"hello" : "world"
},
{
"hello" : "world1"
}
],
"some3" : ["1", "2", "3"]
}
""");
log.info("project {}", project);

// act
var resolve = placeholderResolver
.resolve(Placeholder.parse("{{folder#14:var.some1}}"));
log.info("resolved: {}", resolve);

projectService.delete("myProject");
assertTrue(true);
}

@Test
void jsonComplexArrayPlaceholders() {
projectContext.setProjectId("myProject");
datasetService.setVariable(folderService.findById(project.getRootFolder()).get().getDataset(), "var", """
{
"some1" : "Hi",
"some2" : [
{
"hello" : "world"
},
{
"hello" : "world1"
}
],
"some3" : ["1", "2", "3"]
}
""");
log.info("project {}", project);

// act
var resolve = placeholderResolver
.resolve(Placeholder.parse("{{folder#17:var.some2[1].hello}}"));
log.info("resolved: {}", resolve);

projectService.delete("myProject");
assertTrue(true);
}

@Test
void jsonBaseArrayPlaceholders() {
projectContext.setProjectId("myProject");
datasetService.setVariable(folderService.findById(project.getRootFolder()).get().getDataset(), "var", """
{
"some1" : "Hi",
"some2" : [
{
"hello" : "world"
},
{
"hello" : "world1"
}
],
"some3" : ["1", "2", "3"]
}
""");
log.info("project {}", project);

// act
var resolve = placeholderResolver
.resolve(Placeholder.parse("{{folder#18:var.some3}}"));
log.info("resolved: {}", resolve);

projectService.delete("myProject");
assertTrue(true);
}

@Test
void runtimePlaceholders() throws InterruptedException {
projectContext.setProjectId("myProject");
Expand Down
Loading