diff --git a/FlySpring/edgechain-app/src/main/resources/schema.sql b/FlySpring/edgechain-app/src/main/resources/schema.sql new file mode 100644 index 000000000..a4fed6928 --- /dev/null +++ b/FlySpring/edgechain-app/src/main/resources/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE history_context ( + id VARCHAR(255) NOT NULL PRIMARY KEY, + response VARCHAR(1024), + created_at TIMESTAMP +); \ No newline at end of file diff --git a/FlySpring/edgechain-app/src/test/java/com/edgechain/chunker/ChunkerTest.java b/FlySpring/edgechain-app/src/test/java/com/edgechain/chunker/ChunkerTest.java index e6edef4d7..08f6df3e0 100644 --- a/FlySpring/edgechain-app/src/test/java/com/edgechain/chunker/ChunkerTest.java +++ b/FlySpring/edgechain-app/src/test/java/com/edgechain/chunker/ChunkerTest.java @@ -1,10 +1,9 @@ package com.edgechain.chunker; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.Timeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; @@ -12,6 +11,11 @@ import com.edgechain.lib.chunk.Chunker; import com.edgechain.lib.chunk.enums.LangType; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.*; + @SpringBootTest public class ChunkerTest { @@ -82,4 +86,61 @@ public void chunker_BySentenceWithEmptyInput_ReturnEmptyArray(TestInfo testInfo) String[] expected = {"This is a test."}; assertArrayEquals(expected, result); } + + @Test + @DisplayName("Test By Very Small ChunkSize ") + void chunker_ByVerySmallChunkSize_ReturnedExpectedValue() { + String input = "This is Testing"; + Chunker chunker = new Chunker(input); + + String[] result = chunker.byChunkSize(1); + + String[] expected = {"T", "h", "i", "s", "i", "s", "T", "e", "s", "t", "i", "n", "g"}; + assertNotEquals(expected, result); + } + + @Test + @DisplayName("Test By ChunkSize - Input Contains Whitespace") + void chunker_ByChunkSize_InputWhiteSpaceCharacter_ReturnedExpectedValue() { + String input = "\n\t\t"; + Chunker chunker = new Chunker(input); + int chunkSize = 5; + + String[] result = chunker.byChunkSize(chunkSize); + + String[] expected = {""}; + assertArrayEquals(expected, result); + } + + @Test + @DisplayName("Test By Sentence - Contains Only Spaces") + void chunker_BySentence_InputContainsOnlySpaces_ReturnedExpectedValue() { + String input = " "; + Chunker chunker = new Chunker(input); + + String[] result = chunker.bySentence(LangType.EN); + logger.info(Arrays.toString(result)); + String[] expected = {}; + assertArrayEquals(expected, result); + assertEquals(expected.length, result.length); + } + + @Test + @DisplayName("Performance Test With Large String") + @Timeout(value = 5, unit = TimeUnit.SECONDS) + void chunker_Performance_LargeInputString_ReturnedExpectedValue() { + String input = "E".repeat(10000); + Chunker chunker = new Chunker(input); + int chunkSize = 5; + + long startTime = System.currentTimeMillis(); + String[] result = chunker.byChunkSize(chunkSize); + long endTime = System.currentTimeMillis(); + long totalExecutionTime = endTime - startTime; + logger.info(String.valueOf(totalExecutionTime)); + + long maxExecutionTime = 5000; // Execution time in mills + assertEquals(2000, result.length); + assertTrue(totalExecutionTime <= maxExecutionTime); + } } diff --git a/FlySpring/edgechain-app/src/test/java/com/edgechain/codeInterpreter/CodeInterpreterTest.java b/FlySpring/edgechain-app/src/test/java/com/edgechain/codeInterpreter/CodeInterpreterTest.java index e4d0527aa..ff6d22dde 100644 --- a/FlySpring/edgechain-app/src/test/java/com/edgechain/codeInterpreter/CodeInterpreterTest.java +++ b/FlySpring/edgechain-app/src/test/java/com/edgechain/codeInterpreter/CodeInterpreterTest.java @@ -1,9 +1,7 @@ package com.edgechain.codeInterpreter; import static org.junit.Assert.assertFalse; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.io.ByteArrayInputStream; import java.io.InputStream; @@ -65,4 +63,14 @@ public void test_empty_example_extraction() throws Exception { assertNotNull(extractedValue); assertFalse(extractedValue.contains(prompt)); } + + @Test + @DisplayName("Test for empty input") + void test_emptyInput_ReturnedExpectedValue() { + String inputJsonnet = ""; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + + assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream)); + } } diff --git a/FlySpring/edgechain-app/src/test/java/com/edgechain/jsonnet/JsonnetLoaderTest.java b/FlySpring/edgechain-app/src/test/java/com/edgechain/jsonnet/JsonnetLoaderTest.java index 5dcdcbfc8..12e6b77fa 100644 --- a/FlySpring/edgechain-app/src/test/java/com/edgechain/jsonnet/JsonnetLoaderTest.java +++ b/FlySpring/edgechain-app/src/test/java/com/edgechain/jsonnet/JsonnetLoaderTest.java @@ -1,9 +1,5 @@ package com.edgechain.jsonnet; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.io.ByteArrayInputStream; import java.io.InputStream; @@ -20,6 +16,9 @@ import com.edgechain.lib.jsonnet.JsonnetLoader; import com.edgechain.lib.jsonnet.impl.FileJsonnetLoader; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; + @SpringBootTest public class JsonnetLoaderTest { @@ -106,4 +105,36 @@ public void test_external_variable_xtrasonnet() throws Exception { assertNotNull(externalVar); assertEquals(externalVar, "5"); } + + @Test + void jsonLoader_LoadJsonnet_WithInvalidJsonnet_ThrowsException() { + String inputJsonnet = "This is a test sentence."; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream)); + } + + @Test + void jsonLoader_LoadJsonnet_WithEmptyJsonnet_ThrowsExpcetion() { + String inputJsonnet = "{}"; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + jsonnetLoader.load(inputStream); + assertThrows(Exception.class, () -> jsonnetLoader.get("jsonnet")); + } + + @Test + void jsonLoader_LoadJsonnetWithArrayOfObjects_ReturnExpectedValue(TestInfo testInfo) { + String inputJsonnet = "{ \"objects\": [{ \"key\": \"value1\" }, { \"key\": \"value2\" }] }"; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + + jsonnetLoader.load(inputStream); + JSONArray objects = jsonnetLoader.getArray("objects"); + + assertNotNull(objects); + assertEquals(2, objects.length()); + assertEquals("value1", objects.getJSONObject(0).getString("key")); + assertEquals("value2", objects.getJSONObject(1).getString("key")); + } } diff --git a/FlySpring/edgechain-app/src/test/java/com/edgechain/lib/context/client/repositories/PostgreSQLHistoryContextRepositoryTest.java b/FlySpring/edgechain-app/src/test/java/com/edgechain/lib/context/client/repositories/PostgreSQLHistoryContextRepositoryTest.java new file mode 100644 index 000000000..88801627b --- /dev/null +++ b/FlySpring/edgechain-app/src/test/java/com/edgechain/lib/context/client/repositories/PostgreSQLHistoryContextRepositoryTest.java @@ -0,0 +1,85 @@ +package com.edgechain.lib.context.client.repositories; + +import com.edgechain.lib.context.domain.HistoryContext; +import com.edgechain.testutil.PostgresTestContainer; +import org.junit.jupiter.api.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.jdbc.Sql; + +import java.time.LocalDateTime; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@Sql(scripts = {"classpath:schema.sql"}) +class PostgreSQLHistoryContextRepositoryTest { + + Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired private PostgreSQLHistoryContextRepository repository; + + private static final PostgresTestContainer instance = + new PostgresTestContainer(PostgresTestContainer.PostgresImage.VECTOR); + + @BeforeAll + static void setupAll() { + instance.start(); + } + + @AfterAll + static void tearAll() { + instance.stop(); + } + + @BeforeEach + void setUp() { + repository.deleteAll(); + } + + @DynamicPropertySource + static void setProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", instance::getJdbcUrl); + registry.add("spring.datasource.username", instance::getUsername); + registry.add("spring.datasource.password", instance::getPassword); + } + + @Test + void test_Save_And_Retrieve_History_Context() { + HistoryContext historyContext = getHistoryContext(); + repository.save(historyContext); + + Optional result = repository.findById("1"); + logger.info("history context {}", result); + + assertTrue(result.isPresent()); + } + + @Test + void test_Delete_History_Context() { + HistoryContext historyContext = getHistoryContext(); + repository.save(historyContext); + + repository.deleteById("1"); + Optional result = repository.findById("1"); + + assertTrue(result.isEmpty()); + } + + @Test + void test_Find_By_Non_Exist_Context() { + Optional result = repository.findById("10"); + assertTrue(result.isEmpty()); + } + + private HistoryContext getHistoryContext() { + return new HistoryContext("1", "testing history context", LocalDateTime.now()); + } +} diff --git a/FlySpring/edgechain-app/src/test/java/com/edgechain/reactChain/ReactChainTest.java b/FlySpring/edgechain-app/src/test/java/com/edgechain/reactChain/ReactChainTest.java index fc8de64ab..9d6ec81be 100644 --- a/FlySpring/edgechain-app/src/test/java/com/edgechain/reactChain/ReactChainTest.java +++ b/FlySpring/edgechain-app/src/test/java/com/edgechain/reactChain/ReactChainTest.java @@ -1,6 +1,7 @@ package com.edgechain.reactChain; import com.edgechain.lib.jsonnet.JsonnetLoader; +import com.edgechain.lib.jsonnet.exceptions.JsonnetLoaderException; import com.edgechain.lib.jsonnet.impl.FileJsonnetLoader; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -9,8 +10,8 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; @SpringBootTest public class ReactChainTest { @@ -76,4 +77,48 @@ local callFunction(funcName) = assertNotNull(searchFunction); assertEquals(searchFunction, "udf.fn"); } + + @Test + @DisplayName("Test extractAction with invalid input") + void test_extractAction_WithInvalidJsonnet() throws Exception { + String inputJsonnet = "This is invalid jsonnet."; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream)); + } + + @Test + @DisplayName("Test extractAction with empty input") + void test_extractAction_withEmptyJsonnet() throws Exception { + String inputJsonnet = ""; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + assertThrows(Exception.class, () -> jsonnetLoader.get("action")); + assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream)); + } + + @Test + @DisplayName("Test extractThought - invalid input") + void test_extractThought_WithInvalidInput() { + String inputJsonnet = "This is not a valid jsonnet pattern"; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + assertThrows(Exception.class, () -> jsonnetLoader.load(inputStream)); + } + + @Test + @DisplayName("Test Mapper - Missing function") + public void test_mapper_MissingFunction_ReturnedExpectedResult() { + String inputJsonnet = + """ + local config = { + "edgechains.config": { + "mapper": {}, + }, + }; + """; + InputStream inputStream = new ByteArrayInputStream(inputJsonnet.getBytes()); + JsonnetLoader jsonnetLoader = new FileJsonnetLoader(); + assertThrows(JsonnetLoaderException.class, () -> jsonnetLoader.load(inputStream)); + } }