Skip to content

Commit

Permalink
Added item definition to axiom query lang service
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Mar 15, 2024
1 parent ebcde3d commit d5199a3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import com.evolveum.axiom.lang.antlr.AxiomQueryError;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.ItemDefinition;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.xml.namespace.QName;
import java.util.List;
import java.util.Map;

public interface AxiomQueryLangService {

List<AxiomQueryError> validate(String query);
List<AxiomQueryError> validate(@Nullable ItemDefinition definition, String query);

Map<String, String> queryCompletion(String query);
Map<String, String> queryCompletion(@Nullable ItemDefinition definition, String query);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.evolveum.axiom.lang.antlr.AxiomQueryError;
import com.evolveum.axiom.lang.antlr.AxiomQuerySource;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.query.AxiomQueryLangService;

import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

Expand All @@ -18,15 +21,15 @@ public AxiomQueryLangServiceImpl(PrismContext prismContext) {
this.prismContext = prismContext;
}

public List<AxiomQueryError> validate(String query) {
public List<AxiomQueryError> validate(@Nullable ItemDefinition definition, String query) {
AxiomQueryValidationVisitor axiomQueryValidationVisitor = new AxiomQueryValidationVisitor(this.prismContext);
AxiomQuerySource axiomQuerySource = AxiomQuerySource.from(query);
axiomQuerySource.root().accept(axiomQueryValidationVisitor);
axiomQueryValidationVisitor.errorList.addAll(axiomQuerySource.getSyntaxError());
return axiomQueryValidationVisitor.errorList;
}

public Map<String, String> queryCompletion(String query) {
public Map<String, String> queryCompletion(@Nullable ItemDefinition definition, String query) {

if (query.isEmpty()) query = " ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void setupDebug() throws SchemaException, SAXException, IOException {
@Test
public void testQueryCompletionDot() {
String query = ". ";
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(query);
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(null, query);
Map<String, String> expected = new HashMap<>();
expected.put("isRoot", null);
expected.put("inOrg", null);
Expand All @@ -56,7 +56,7 @@ public void testQueryCompletionDot() {
public void testQueryCompletionTypesOfUserType() {
String query = ". type ";
TypeDefinition typeDefinition = schemaRegistry.findTypeDefinitionByType(new QName("UserType"));
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(query);
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(null, query);
List<String> objectTypes = schemaRegistry.getAllSubTypesByTypeDefinition(Collections.singletonList(typeDefinition)).stream().map(item -> item.getTypeName().getLocalPart()).sorted().toList();
Assert.assertEquals(suggestions.keySet().stream().sorted().toList(), objectTypes);
}
Expand All @@ -68,7 +68,7 @@ public void testQueryCompletionBasePathsOfUserType() {
PrismObjectDefinition<?> objectDefinition = schemaRegistry.findObjectDefinitionByCompileTimeClass((Class) typeDefinition.getCompileTimeClass());
List<String> itemPaths = new ArrayList<>(objectDefinition.getItemNames().stream().map(QName::getLocalPart).toList());
itemPaths.add(".");
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(query);
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(null, query);
Assert.assertEquals(suggestions.keySet().stream().sorted().toList(), itemPaths.stream().sorted().toList());
}

Expand Down Expand Up @@ -99,14 +99,14 @@ public void testQueryCompletionBaseFilterName() {
expected.add("startsWith");
expected.add("not");

Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(query);
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(null, query);
Assert.assertEquals(suggestions.keySet().stream().sorted().toList(), expected.stream().sorted().toList());
}

@Test
public void testQueryCompletionBaseSubFilter() {
String query = ". type UserType and givenName equal \"Jack\" ";
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(query);
Map<String, String> suggestions = axiomQueryLangServiceImpl.queryCompletion(null, query);
Assert.assertEquals(suggestions.keySet().stream().sorted().toList(), List.of("and", "or"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package com.evolveum.midpoint.prism.query.lang;

import static org.testng.AssertJUnit.assertTrue;

import static com.evolveum.midpoint.prism.PrismInternalTestUtil.DEFAULT_NAMESPACE_PREFIX;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import com.evolveum.axiom.lang.antlr.AxiomQueryError;
import com.evolveum.midpoint.prism.AbstractPrismTest;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismInternalTestUtil;
import com.evolveum.axiom.lang.antlr.AxiomQueryError;
import com.evolveum.midpoint.prism.impl.query.lang.AxiomQueryLangServiceImpl;
import com.evolveum.midpoint.prism.query.AxiomQueryLangService;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;

import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.evolveum.midpoint.prism.PrismInternalTestUtil.DEFAULT_NAMESPACE_PREFIX;
import static org.testng.AssertJUnit.assertTrue;

/**
* Created by Dominik.
*/
Expand All @@ -40,9 +41,9 @@ private boolean checkingAxiomQueryErrorList(List<AxiomQueryError> errorList, Lis
int errorCount = errorList.size();
int expectedCount = expectedList.size();

if (errorCount != expectedCount) return false;
if (errorCount != expectedCount) {return false;}

for(int i = 0; i < errorCount; ++i) {
for (int i = 0; i < errorCount; ++i) {
if (!errorList.get(i).equals(expectedList.get(i))) {
return false;
}
Expand All @@ -54,22 +55,22 @@ private boolean checkingAxiomQueryErrorList(List<AxiomQueryError> errorList, Lis
@Test
public void testValidateEasyQuery() {
String query = ". type UserType and givenName equal \"Jack\"";
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);
Assert.assertEquals(errorList, new ArrayList<>(), "verified query\n");
}

@Test
public void testInvalidateBadFilterName() {
String query = ". type UserType and givenName equal1 \"Jack\" and name = \"Jack\"";
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);

List<AxiomQueryError> expected = new ArrayList<>();
expected.add(
new AxiomQueryError(null,
null,
1, 30, 35,
"Filter equal1 is not supported for path givenName",
null)
new AxiomQueryError(null,
null,
1, 30, 35,
"Filter equal1 is not supported for path givenName",
null)
);

assertTrue("invalid filter name\n", checkingAxiomQueryErrorList(errorList, expected));
Expand All @@ -78,7 +79,7 @@ public void testInvalidateBadFilterName() {
@Test
public void testInvalidateReferencedByFilterName() {
String query = ". type UserType and givenName referencedBy \"Jack\"";
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);

List<AxiomQueryError> expected = new ArrayList<>();
expected.add(
Expand All @@ -95,7 +96,7 @@ public void testInvalidateReferencedByFilterName() {
@Test
public void testInvalidateMatchesFilterName() {
String query = ". type UserType and givenName matches \"Jack\"";
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);

List<AxiomQueryError> expected = new ArrayList<>();
expected.add(
Expand All @@ -112,7 +113,7 @@ public void testInvalidateMatchesFilterName() {
@Test
public void testInvalidateFilterNameAlias() {
String query = ". type UserType and activation = \"disabled\"";
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);

List<AxiomQueryError> expected = new ArrayList<>();
expected.add(
Expand All @@ -129,7 +130,7 @@ public void testInvalidateFilterNameAlias() {
@Test
public void testInvalidateItemPath() {
String query = ". type UserType and givenName1 equal \"Jack\"";
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);

List<AxiomQueryError> expected = new ArrayList<>();
expected.add(
Expand All @@ -146,7 +147,7 @@ public void testInvalidateItemPath() {
@Test
public void testInvalidateObjectType() {
String query = ". type UserType1 and givenName1 equal \"Jack\"";
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);

List<AxiomQueryError> expected = new ArrayList<>();
expected.add(
Expand Down Expand Up @@ -182,7 +183,7 @@ public void testInvalidateComplexitiesQuery() {
+ " and y = 1.0\n"
+ ")";

List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(query);
List<AxiomQueryError> errorList = this.axiomQueryLangService.validate(null, query);

List<AxiomQueryError> expected = new ArrayList<>();
expected.add(
Expand Down

0 comments on commit d5199a3

Please sign in to comment.