From: nisha.gangore Date: Fri, 21 Mar 2025 09:51:24 +0000 (+0530) Subject: [AAI] Improve test coverage for A&AI component aai-traversal X-Git-Tag: 1.16.0~3 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=b1e15b732534f1c933e3ecc444cdbffaf80fcf68;p=aai%2Ftraversal.git [AAI] Improve test coverage for A&AI component aai-traversal - to Improve test coverage for A&AI component aai-traversal <=80% Issue-ID: AAI-4106 Change-Id: Ife8bf517909a088dc032369e4d852de8a7261192 Signed-off-by: nisha.gangore --- diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/dsl/DslQueryBuilderTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/DslQueryBuilderTest.java index a2271516..a2858ac4 100644 --- a/aai-traversal/src/test/java/org/onap/aai/rest/dsl/DslQueryBuilderTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/DslQueryBuilderTest.java @@ -20,34 +20,89 @@ package org.onap.aai.rest.dsl; -import static org.junit.Assert.assertSame; - +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.introspection.Loader; +import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; -import org.junit.Before; -import org.junit.Test; -import org.onap.aai.edges.EdgeIngestor; +class DslQueryBuilderTest { -public class DslQueryBuilderTest { DslQueryBuilder dslQueryBuilder; - @Before - public void setUp() { + @BeforeEach + void setUp() { EdgeIngestor edgeIngestor = new EdgeIngestor(new HashSet<>()); dslQueryBuilder = new DslQueryBuilder(edgeIngestor, null); } @Test - public void testQuery() { + void testQuery() { StringBuilder query = new StringBuilder(); dslQueryBuilder.setQuery(query); assertSame(query, dslQueryBuilder.getQuery()); } @Test - public void testQueryException() { + void testQueryException() { StringBuilder queryException = new StringBuilder(); dslQueryBuilder.setQueryException(queryException); assertSame(queryException, dslQueryBuilder.getQueryException()); } + + @Test + void testEndInstance() { + DslQueryBuilder result = dslQueryBuilder.endInstance(); + assertSame(result, dslQueryBuilder); + } + + @Test + void testTrimSingleQuotes() throws Exception { + EdgeIngestor edgeIngestor = new EdgeIngestor(new HashSet<>()); + DslQueryBuilder dslQueryBuilder = new DslQueryBuilder(edgeIngestor, null); + + Method trimSingleQuotesMethod = DslQueryBuilder.class.getDeclaredMethod("trimSingleQuotes", String.class); + trimSingleQuotesMethod.setAccessible(true); + + assertEquals("value", trimSingleQuotesMethod.invoke(dslQueryBuilder, "'value'")); + assertEquals("value", trimSingleQuotesMethod.invoke(dslQueryBuilder, "value")); + assertEquals("value", trimSingleQuotesMethod.invoke(dslQueryBuilder, "value")); + assertEquals("", trimSingleQuotesMethod.invoke(dslQueryBuilder, "''")); + assertEquals("", trimSingleQuotesMethod.invoke(dslQueryBuilder, "")); + } + + @Test + void testTrimSingleQuotesEdgeCases() throws Exception { + EdgeIngestor edgeIngestor = new EdgeIngestor(new HashSet<>()); + DslQueryBuilder dslQueryBuilder = new DslQueryBuilder(edgeIngestor, null); + + Method trimSingleQuotesMethod = DslQueryBuilder.class.getDeclaredMethod("trimSingleQuotes", String.class); + trimSingleQuotesMethod.setAccessible(true); + + Object result = trimSingleQuotesMethod.invoke(dslQueryBuilder, (Object) null); + assertNull(result); + + result = trimSingleQuotesMethod.invoke(dslQueryBuilder, ""); + assertEquals("", result); + } + + @Test + void testFilterPropertyStartWithTrueBoolean() { + EdgeIngestor edgeIngestorMock = mock(EdgeIngestor.class); + Loader loaderMock = mock(Loader.class); + + DslQueryBuilder dslQueryBuilder = new DslQueryBuilder(edgeIngestorMock, loaderMock); + + List values = new ArrayList<>(); + values.add("true"); + + StringBuilder query = new StringBuilder(); + dslQueryBuilder.filterPropertyStart(false, values); + assertFalse(query.toString().contains(".getVerticesByBooleanProperty(")); + } } diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslQueryValidatorTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslQueryValidatorTest.java new file mode 100644 index 00000000..920f9e74 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslQueryValidatorTest.java @@ -0,0 +1,106 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.rest.dsl.validation; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Properties; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class DslQueryValidatorTest { + + private DslQueryValidator validator; + + @BeforeEach + void setUp() { + loadProperties(); + validator = new DslQueryValidator(new DslQueryValidator.Builder()); + } + + @Test + void testValidate_LoopValidationPassed() { + List edges = List.of("edge1", "edge2", "edge1", "edge2"); + DslValidatorRule rule = new DslValidatorRule.Builder() + .query("query") + .loop("loop", edges) + .build(); + + boolean result = validator.validate(rule); + + assertTrue(result); + } + + @Test + void testValidate_LoopValidationPassedAll() { + List edges = List.of("edge1", "edge2", "edge1", "edge2"); + DslValidatorRule rule = new DslValidatorRule.Builder() + .query("query") + .loop("all", edges) + .build(); + + boolean result = validator.validate(rule); + + assertTrue(result); + } + + + private void loadProperties() { + Properties properties = new Properties(); + try (InputStream input = new FileInputStream("src/test/resources/aaiconfig.properties")) { + properties.load(input); + + String maxNodeCount = properties.getProperty("aai.dsl.max.nodecount", "100"); + System.setProperty("aai.dsl.max.nodecount", maxNodeCount); + System.out.println("Loaded max node count: " + maxNodeCount); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + void testValidate_NodeCountValidationFailed() { + int maxNodeCount = 100; + + DslValidatorRule rule = new DslValidatorRule.Builder() + .query("query") + .nodeCount("nodeCount", maxNodeCount + 1) + .build(); + + boolean result = validator.validate(rule); + + assertFalse(result); + } + + @Test + void testValidate_NoValidation() { + DslValidatorRule rule = new DslValidatorRule.Builder() + .query("query") + .build(); + + boolean result = validator.validate(rule); + + assertTrue(result); + } +} diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslSchemaValidatorTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslSchemaValidatorTest.java new file mode 100644 index 00000000..2cd7fc5e --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslSchemaValidatorTest.java @@ -0,0 +1,64 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.rest.dsl.validation; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class DslSchemaValidatorTest { + + private DslSchemaValidator.Builder builder; + + @BeforeEach + void setUp() { + builder = new DslSchemaValidator.Builder(); + } + + @Test + void testCreateDslSchemaValidatorWithBuilder() { + builder.schema(); + DslSchemaValidator validator = (DslSchemaValidator) builder.create(); + assertTrue(validator instanceof DslSchemaValidator); + } + + @Test + void testValidateReturnsTrue() { + builder.schema(); + DslSchemaValidator validator = (DslSchemaValidator) builder.create(); + DslValidatorRule rule = new DslValidatorRule.Builder().build(); + boolean result = validator.validate(rule); + assertTrue(result); + } + + @Test + void testDslSchemaValidatorIsNotNull() { + builder.schema(); + DslSchemaValidator validator = (DslSchemaValidator) builder.create(); + assertNotNull(validator); + } + + @Test + void testBuilderSetsCorrectFlag() { + builder.schema(); + assertTrue(builder.isSchemaValidation); + } +} diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslValidatorRuleTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslValidatorRuleTest.java new file mode 100644 index 00000000..991eb96a --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslValidatorRuleTest.java @@ -0,0 +1,131 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.rest.dsl.validation; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.List; + +public class DslValidatorRuleTest { + + @Test + public void testBuilder_DefaultInitialization() { + DslValidatorRule rule = new DslValidatorRule.Builder().build(); + assertEquals("", rule.getQuery()); + assertFalse(rule.isValidateLoop()); + assertFalse(rule.isValidateNodeCount()); + assertEquals(0, rule.getNodeCount()); + assertTrue(rule.getEdges().isEmpty()); + } + + @Test + public void testBuilder_LoopValidationEnabled() { + List edges = List.of("edge1", "edge2"); + DslValidatorRule rule = new DslValidatorRule.Builder() + .loop("loop", edges) + .build(); + assertTrue(rule.isValidateLoop()); + assertEquals(edges, rule.getEdges()); + } + + @Test + public void testBuilder_NodeCountValidationEnabled() { + DslValidatorRule rule = new DslValidatorRule.Builder() + .nodeCount("nodeCount", 5) + .build(); + assertTrue(rule.isValidateNodeCount()); + assertEquals(5, rule.getNodeCount()); + } + + @Test + public void testBuilder_QuerySetCorrectly() { + String testQuery = "SELECT * FROM Nodes"; + DslValidatorRule rule = new DslValidatorRule.Builder() + .query(testQuery) + .build(); + assertEquals(testQuery, rule.getQuery()); + } + + @Test + public void testBuilder_InvalidNodeCountValidation() { + DslValidatorRule rule = new DslValidatorRule.Builder() + .nodeCount("none", 5) + .build(); + assertFalse(rule.isValidateNodeCount()); + } + + @Test + public void testBuilder_InvalidLoopValidation() { + DslValidatorRule rule = new DslValidatorRule.Builder() + .loop("none", List.of("edge1", "edge2")) + .build(); + assertFalse(rule.isValidateLoop()); + } + + @Test + public void testBuilder_CombiningAllValidations() { + List edges = List.of("edge1", "edge2"); + DslValidatorRule rule = new DslValidatorRule.Builder() + .loop("loop", edges) + .nodeCount("nodeCount", 5) + .build(); + assertTrue(rule.isValidateLoop()); + assertTrue(rule.isValidateNodeCount()); + assertEquals(edges, rule.getEdges()); + assertEquals(5, rule.getNodeCount()); + } + + @Test + public void testSetQuery() { + DslValidatorRule rule = new DslValidatorRule.Builder().build(); + String testQuery = "SELECT * FROM Nodes"; + rule.setQuery(testQuery); + assertEquals(testQuery, rule.getQuery()); + } + + @Test + public void testSetValidateLoop() { + DslValidatorRule rule = new DslValidatorRule.Builder().build(); + rule.setValidateLoop(true); + assertTrue(rule.isValidateLoop()); + } + + @Test + public void testSetValidateNodeCount() { + DslValidatorRule rule = new DslValidatorRule.Builder().build(); + rule.setValidateNodeCount(true); + assertTrue(rule.isValidateNodeCount()); + } + + @Test + public void testSetNodeCount() { + DslValidatorRule rule = new DslValidatorRule.Builder().build(); + rule.setNodeCount(10); + assertEquals(10, rule.getNodeCount()); + } + + @Test + public void testSetEdges() { + DslValidatorRule rule = new DslValidatorRule.Builder().build(); + List edges = List.of("edge1", "edge2", "edge3"); + rule.setEdges(edges); + assertEquals(edges, rule.getEdges()); + } +} diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslValidatorTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslValidatorTest.java new file mode 100644 index 00000000..184ea342 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/rest/dsl/validation/DslValidatorTest.java @@ -0,0 +1,60 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.rest.dsl.validation; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DslValidatorTest { + + private DslValidator.Builder builder; + + @BeforeEach + void setUp() { + builder = new DslValidator.Builder(); + } + + @Test + void testCreateDslSchemaValidatorWhenSchemaIsTrue() { + builder.schema(); + DslValidator validator = builder.create(); + assertTrue(validator instanceof DslSchemaValidator); + } + + @Test + void testCreateDslQueryValidatorWhenSchemaIsFalse() { + DslValidator validator = builder.create(); + assertTrue(validator instanceof DslQueryValidator); + } + + @Test + void testErrorMessageInitialization() { + DslValidator validator = builder.create(); + assertEquals("", validator.getErrorMessage()); + } + + @Test + void testSchemaMethodSetsFlag() { + builder.schema(); + assertTrue(builder.isSchemaValidation); + } +} diff --git a/aai-traversal/src/test/resources/aaiconfig.properties b/aai-traversal/src/test/resources/aaiconfig.properties new file mode 100644 index 00000000..585a49f1 --- /dev/null +++ b/aai-traversal/src/test/resources/aaiconfig.properties @@ -0,0 +1,2 @@ +# src/test/resources/aaiconfig.properties +aai.dsl.max.nodecount=100