From 55ba184c851fe94bbff231fb9cc8193e0a633dc8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Sep 2025 13:52:04 +0000 Subject: [PATCH 1/3] Initial plan From 7774bde5d5cad6cc5fa50a2f6af9e61d923b7f0f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Sep 2025 14:07:23 +0000 Subject: [PATCH 2/3] Phase 1 complete: Comprehensive unit tests for turing-commons module Co-authored-by: alegauss <331174+alegauss@users.noreply.github.com> --- turing-commons/pom.xml | 25 ++ .../commons/file/TurFileAttributesTest.java | 209 +++++++++++ .../turing/commons/file/TurFileSizeTest.java | 172 +++++++++ .../turing/commons/sn/TurSNConfigTest.java | 80 ++++ .../bean/TurSNSiteSearchDocumentBeanTest.java | 186 ++++++++++ ...rSNSiteSearchDocumentMetadataBeanTest.java | 175 +++++++++ .../commons/utils/TurCommonsUtilsTest.java | 348 ++++++++++++++++++ 7 files changed, 1195 insertions(+) create mode 100644 turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileAttributesTest.java create mode 100644 turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileSizeTest.java create mode 100644 turing-commons/src/test/java/com/viglet/turing/commons/sn/TurSNConfigTest.java create mode 100644 turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBeanTest.java create mode 100644 turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentMetadataBeanTest.java create mode 100644 turing-commons/src/test/java/com/viglet/turing/commons/utils/TurCommonsUtilsTest.java diff --git a/turing-commons/pom.xml b/turing-commons/pom.xml index 965b0fc7886..60d278e21eb 100644 --- a/turing-commons/pom.xml +++ b/turing-commons/pom.xml @@ -96,6 +96,31 @@ 5.5.1 compile + + + org.junit.jupiter + junit-jupiter + 5.11.4 + test + + + org.mockito + mockito-core + 5.15.2 + test + + + org.mockito + mockito-junit-jupiter + 5.15.2 + test + + + org.assertj + assertj-core + 3.26.3 + test + turing-commons diff --git a/turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileAttributesTest.java b/turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileAttributesTest.java new file mode 100644 index 00000000000..60812cf0496 --- /dev/null +++ b/turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileAttributesTest.java @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2016-2024 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see . + */ + +package com.viglet.turing.commons.file; + +import org.junit.jupiter.api.Test; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurFileAttributes. + * + * @author Alexandre Oliveira + * @since 0.3.9 + */ +class TurFileAttributesTest { + + @Test + void testDefaultConstructor() { + TurFileAttributes attributes = new TurFileAttributes(); + + assertThat(attributes.getContent()).isNull(); + assertThat(attributes.getName()).isNull(); + assertThat(attributes.getTitle()).isNull(); + assertThat(attributes.getExtension()).isNull(); + assertThat(attributes.getSize()).isNotNull(); + assertThat(attributes.getLastModified()).isNotNull(); + assertThat(attributes.getMetadata()).isNotNull().isEmpty(); + } + + @Test + void testBuilderPattern() { + Date testDate = new Date(); + TurFileSize testSize = new TurFileSize(); + Map testMetadata = new HashMap<>(); + testMetadata.put("author", "Test Author"); + + TurFileAttributes attributes = TurFileAttributes.builder() + .content("Test content") + .name("test.txt") + .title("Test File") + .extension("txt") + .size(testSize) + .lastModified(testDate) + .metadata(testMetadata) + .build(); + + assertThat(attributes.getContent()).isEqualTo("Test content"); + assertThat(attributes.getName()).isEqualTo("test.txt"); + assertThat(attributes.getTitle()).isEqualTo("Test File"); + assertThat(attributes.getExtension()).isEqualTo("txt"); + assertThat(attributes.getSize()).isEqualTo(testSize); + assertThat(attributes.getLastModified()).isEqualTo(testDate); + assertThat(attributes.getMetadata()).containsEntry("author", "Test Author"); + } + + @Test + void testAllArgsConstructor() { + Date testDate = new Date(); + TurFileSize testSize = new TurFileSize(); + Map testMetadata = new HashMap<>(); + testMetadata.put("type", "document"); + + TurFileAttributes attributes = new TurFileAttributes( + "Test content", + "document.pdf", + "Test Document", + "pdf", + testSize, + testDate, + testMetadata + ); + + assertThat(attributes.getContent()).isEqualTo("Test content"); + assertThat(attributes.getName()).isEqualTo("document.pdf"); + assertThat(attributes.getTitle()).isEqualTo("Test Document"); + assertThat(attributes.getExtension()).isEqualTo("pdf"); + assertThat(attributes.getSize()).isEqualTo(testSize); + assertThat(attributes.getLastModified()).isEqualTo(testDate); + assertThat(attributes.getMetadata()).containsEntry("type", "document"); + } + + @Test + void testSettersAndGetters() { + TurFileAttributes attributes = new TurFileAttributes(); + + attributes.setContent("Modified content"); + attributes.setName("modified.txt"); + attributes.setTitle("Modified Title"); + attributes.setExtension("txt"); + + Date newDate = new Date(System.currentTimeMillis() + 1000); + attributes.setLastModified(newDate); + + TurFileSize newSize = new TurFileSize(); + attributes.setSize(newSize); + + Map newMetadata = new HashMap<>(); + newMetadata.put("updated", "true"); + attributes.setMetadata(newMetadata); + + assertThat(attributes.getContent()).isEqualTo("Modified content"); + assertThat(attributes.getName()).isEqualTo("modified.txt"); + assertThat(attributes.getTitle()).isEqualTo("Modified Title"); + assertThat(attributes.getExtension()).isEqualTo("txt"); + assertThat(attributes.getLastModified()).isEqualTo(newDate); + assertThat(attributes.getSize()).isEqualTo(newSize); + assertThat(attributes.getMetadata()).containsEntry("updated", "true"); + } + + @Test + void testToBuilder() { + TurFileAttributes original = TurFileAttributes.builder() + .content("Original content") + .name("original.txt") + .title("Original Title") + .extension("txt") + .build(); + + TurFileAttributes modified = original.toBuilder() + .content("Modified content") + .title("Modified Title") + .build(); + + // Original should be unchanged + assertThat(original.getContent()).isEqualTo("Original content"); + assertThat(original.getTitle()).isEqualTo("Original Title"); + assertThat(original.getName()).isEqualTo("original.txt"); + + // Modified should have new values + assertThat(modified.getContent()).isEqualTo("Modified content"); + assertThat(modified.getTitle()).isEqualTo("Modified Title"); + assertThat(modified.getName()).isEqualTo("original.txt"); // Unchanged from original + } + + @Test + void testToString() { + TurFileAttributes attributes = TurFileAttributes.builder() + .content("Test content") + .name("test.txt") + .title("Test File") + .extension("txt") + .build(); + + String toString = attributes.toString(); + + assertThat(toString).contains("content=Test content"); + assertThat(toString).contains("name=test.txt"); + assertThat(toString).contains("title=Test File"); + assertThat(toString).contains("extension=txt"); + } + + @Test + void testMetadataManipulation() { + TurFileAttributes attributes = new TurFileAttributes(); + + // Add metadata + attributes.getMetadata().put("format", "PDF"); + attributes.getMetadata().put("pages", "10"); + + assertThat(attributes.getMetadata()).hasSize(2); + assertThat(attributes.getMetadata()).containsEntry("format", "PDF"); + assertThat(attributes.getMetadata()).containsEntry("pages", "10"); + + // Update metadata + attributes.getMetadata().put("pages", "12"); + + assertThat(attributes.getMetadata()).hasSize(2); + assertThat(attributes.getMetadata()).containsEntry("pages", "12"); + + // Remove metadata + attributes.getMetadata().remove("format"); + + assertThat(attributes.getMetadata()).hasSize(1); + assertThat(attributes.getMetadata()).doesNotContainKey("format"); + assertThat(attributes.getMetadata()).containsEntry("pages", "12"); + } + + @Test + void testDefaultValues() { + TurFileAttributes attributes = TurFileAttributes.builder().build(); + + // Check that default values are properly set + assertThat(attributes.getSize()).isNotNull(); + assertThat(attributes.getLastModified()).isNotNull(); + assertThat(attributes.getMetadata()).isNotNull(); + assertThat(attributes.getMetadata()).isEmpty(); + + // Check that lastModified is recent + long timeDiff = System.currentTimeMillis() - attributes.getLastModified().getTime(); + assertThat(timeDiff).isLessThan(1000); // Should be created within last second + } +} \ No newline at end of file diff --git a/turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileSizeTest.java b/turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileSizeTest.java new file mode 100644 index 00000000000..c3b5aaf04cf --- /dev/null +++ b/turing-commons/src/test/java/com/viglet/turing/commons/file/TurFileSizeTest.java @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2016-2024 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see . + */ + +package com.viglet.turing.commons.file; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurFileSize. + * + * @author Alexandre Oliveira + * @since 0.3.9 + */ +class TurFileSizeTest { + + @Test + void testDefaultConstructor() { + TurFileSize fileSize = new TurFileSize(); + + assertThat(fileSize.getBytes()).isEqualTo(0.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(0.0f); + assertThat(fileSize.getMegaBytes()).isEqualTo(0.0f); + } + + @Test + void testConstructorWithBytes() { + float bytes = 1024.0f; + TurFileSize fileSize = new TurFileSize(bytes); + + assertThat(fileSize.getBytes()).isEqualTo(1024.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(1.0f); + assertThat(fileSize.getMegaBytes()).isEqualTo(0.0f); + } + + @Test + void testLargeFileSize() { + float bytes = 1048576.0f; // 1 MB + TurFileSize fileSize = new TurFileSize(bytes); + + assertThat(fileSize.getBytes()).isEqualTo(1048576.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(1024.0f); + assertThat(fileSize.getMegaBytes()).isEqualTo(1.0f); + } + + @Test + void testVeryLargeFileSize() { + float bytes = 2147483648.0f; // 2 GB + TurFileSize fileSize = new TurFileSize(bytes); + + assertThat(fileSize.getBytes()).isEqualTo(2147483648.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(2097152.0f); + assertThat(fileSize.getMegaBytes()).isEqualTo(2048.0f); + } + + @Test + void testFractionalBytes() { + float bytes = 1536.0f; // 1.5 KB + TurFileSize fileSize = new TurFileSize(bytes); + + assertThat(fileSize.getBytes()).isEqualTo(1536.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(1.5f); + // Very small megabyte values get rounded to 0.0 due to two decimal precision + assertThat(fileSize.getMegaBytes()).isEqualTo(0.0f); + } + + @Test + void testSmallFileSize() { + float bytes = 512.5f; + TurFileSize fileSize = new TurFileSize(bytes); + + assertThat(fileSize.getBytes()).isEqualTo(512.5f); + assertThat(fileSize.getKiloBytes()).isCloseTo(0.5f, org.assertj.core.data.Offset.offset(0.01f)); + // Very small megabyte values get rounded to 0.0 due to two decimal precision + assertThat(fileSize.getMegaBytes()).isEqualTo(0.0f); + } + + @Test + void testTwoDecimalPrecision() { + float bytes = 1000.0f / 3.0f; // Should result in 333.33... bytes + TurFileSize fileSize = new TurFileSize(bytes); + + // Verify that values are rounded to two decimal places + assertThat(fileSize.getBytes()).isEqualTo(333.33f); + assertThat(fileSize.getKiloBytes()).isCloseTo(0.33f, org.assertj.core.data.Offset.offset(0.01f)); + } + + @Test + void testBuilder() { + TurFileSize fileSize = TurFileSize.builder() + .bytes(2048.0f) + .kiloBytes(2.0f) + .megaBytes(0.002f) + .build(); + + assertThat(fileSize.getBytes()).isEqualTo(2048.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(2.0f); + assertThat(fileSize.getMegaBytes()).isEqualTo(0.002f); + } + + @Test + void testAllArgsConstructor() { + TurFileSize fileSize = new TurFileSize(1024.0f, 1.0f, 0.001f); + + assertThat(fileSize.getBytes()).isEqualTo(1024.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(1.0f); + assertThat(fileSize.getMegaBytes()).isEqualTo(0.001f); + } + + @Test + void testToString() { + TurFileSize fileSize = new TurFileSize(1024.0f); + String toString = fileSize.toString(); + + assertThat(toString).contains("bytes=1024.0"); + assertThat(toString).contains("kiloBytes=1.0"); + assertThat(toString).contains("megaBytes=0.0"); + } + + @Test + void testZeroBytes() { + TurFileSize fileSize = new TurFileSize(0.0f); + + assertThat(fileSize.getBytes()).isEqualTo(0.0f); + assertThat(fileSize.getKiloBytes()).isEqualTo(0.0f); + assertThat(fileSize.getMegaBytes()).isEqualTo(0.0f); + } + + @Test + void testRoundingBehavior() { + // Test rounding half up + float bytes = 1023.995f; // Should round up to 1024.0 + TurFileSize fileSize = new TurFileSize(bytes); + + // The exact value depends on float precision, but we can test the rounding behavior + assertThat(fileSize.getBytes()).isCloseTo(1024.0f, org.assertj.core.data.Offset.offset(0.01f)); + } + + @Test + void testCommonFileSizes() { + // Test some common file sizes + + // 1 KB + TurFileSize oneKB = new TurFileSize(1024.0f); + assertThat(oneKB.getKiloBytes()).isEqualTo(1.0f); + + // 500 KB + TurFileSize fiveHundredKB = new TurFileSize(512000.0f); + assertThat(fiveHundredKB.getKiloBytes()).isEqualTo(500.0f); + + // 1.5 MB + TurFileSize oneMBHalf = new TurFileSize(1572864.0f); + assertThat(oneMBHalf.getMegaBytes()).isEqualTo(1.5f); + + // 10 MB + TurFileSize tenMB = new TurFileSize(10485760.0f); + assertThat(tenMB.getMegaBytes()).isEqualTo(10.0f); + } +} \ No newline at end of file diff --git a/turing-commons/src/test/java/com/viglet/turing/commons/sn/TurSNConfigTest.java b/turing-commons/src/test/java/com/viglet/turing/commons/sn/TurSNConfigTest.java new file mode 100644 index 00000000000..40803a397b6 --- /dev/null +++ b/turing-commons/src/test/java/com/viglet/turing/commons/sn/TurSNConfigTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2016-2025 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see . + */ + +package com.viglet.turing.commons.sn; + +import org.junit.jupiter.api.Test; + +import java.io.Serializable; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNConfig. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurSNConfigTest { + + @Test + void testDefaultConstructor() { + TurSNConfig config = new TurSNConfig(); + + // Default boolean value should be false + assertThat(config.isHlEnabled()).isFalse(); + } + + @Test + void testSetterAndGetter() { + TurSNConfig config = new TurSNConfig(); + + config.setHlEnabled(true); + + assertThat(config.isHlEnabled()).isTrue(); + } + + @Test + void testSetterAndGetterFalse() { + TurSNConfig config = new TurSNConfig(); + + config.setHlEnabled(false); + + assertThat(config.isHlEnabled()).isFalse(); + } + + @Test + void testToggleHlEnabled() { + TurSNConfig config = new TurSNConfig(); + + // Start with default (false) + assertThat(config.isHlEnabled()).isFalse(); + + // Toggle to true + config.setHlEnabled(true); + assertThat(config.isHlEnabled()).isTrue(); + + // Toggle back to false + config.setHlEnabled(false); + assertThat(config.isHlEnabled()).isFalse(); + } + + @Test + void testImplementsSerializable() { + TurSNConfig config = new TurSNConfig(); + + assertThat(config).isInstanceOf(Serializable.class); + } +} \ No newline at end of file diff --git a/turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBeanTest.java b/turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBeanTest.java new file mode 100644 index 00000000000..10a948c24dd --- /dev/null +++ b/turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBeanTest.java @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2016-2021 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.commons.sn.bean; + +import org.junit.jupiter.api.Test; + +import java.io.Serializable; +import java.util.*; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNSiteSearchDocumentBean. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurSNSiteSearchDocumentBeanTest { + + @Test + void testBuilderPattern() { + List metadata = Arrays.asList( + TurSNSiteSearchDocumentMetadataBean.builder().build() + ); + Map fields = new HashMap<>(); + fields.put("title", "Test Document"); + fields.put("content", "Test content"); + + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder() + .source("test-source") + .elevate(true) + .metadata(metadata) + .fields(fields) + .build(); + + assertThat(document.getSource()).isEqualTo("test-source"); + assertThat(document.isElevate()).isTrue(); + assertThat(document.getMetadata()).isEqualTo(metadata); + assertThat(document.getFields()).isEqualTo(fields); + } + + @Test + void testSettersAndGetters() { + // Use builder since there's no default constructor + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder().build(); + + List metadata = new ArrayList<>(); + Map fields = new HashMap<>(); + fields.put("id", "123"); + fields.put("score", 0.95); + + document.setSource("web-source"); + document.setElevate(false); + document.setMetadata(metadata); + document.setFields(fields); + + assertThat(document.getSource()).isEqualTo("web-source"); + assertThat(document.isElevate()).isFalse(); + assertThat(document.getMetadata()).isEqualTo(metadata); + assertThat(document.getFields()).isEqualTo(fields); + } + + @Test + void testDefaultConstructor() { + // Use builder since there's no default constructor + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder().build(); + + assertThat(document.getSource()).isNull(); + assertThat(document.isElevate()).isFalse(); // default boolean value + assertThat(document.getMetadata()).isNull(); + assertThat(document.getFields()).isNull(); + } + + @Test + void testBuilderWithNullValues() { + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder() + .source(null) + .elevate(false) + .metadata(null) + .fields(null) + .build(); + + assertThat(document.getSource()).isNull(); + assertThat(document.isElevate()).isFalse(); + assertThat(document.getMetadata()).isNull(); + assertThat(document.getFields()).isNull(); + } + + @Test + void testImplementsSerializable() { + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder().build(); + + assertThat(document).isInstanceOf(Serializable.class); + } + + @Test + void testFieldsManipulation() { + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder().build(); + + Map fields = new HashMap<>(); + document.setFields(fields); + + // Add fields + document.getFields().put("title", "Document Title"); + document.getFields().put("author", "John Doe"); + document.getFields().put("date", new Date()); + + assertThat(document.getFields()).hasSize(3); + assertThat(document.getFields()).containsKey("title"); + assertThat(document.getFields()).containsKey("author"); + assertThat(document.getFields()).containsKey("date"); + assertThat(document.getFields().get("title")).isEqualTo("Document Title"); + } + + @Test + void testMetadataManipulation() { + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder().build(); + + List metadata = new ArrayList<>(); + document.setMetadata(metadata); + + // Add metadata + TurSNSiteSearchDocumentMetadataBean meta1 = TurSNSiteSearchDocumentMetadataBean.builder().build(); + TurSNSiteSearchDocumentMetadataBean meta2 = TurSNSiteSearchDocumentMetadataBean.builder().build(); + + document.getMetadata().add(meta1); + document.getMetadata().add(meta2); + + assertThat(document.getMetadata()).hasSize(2); + assertThat(document.getMetadata()).contains(meta1, meta2); + } + + @Test + void testElevateToggle() { + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder().build(); + + // Default should be false + assertThat(document.isElevate()).isFalse(); + + // Set to true + document.setElevate(true); + assertThat(document.isElevate()).isTrue(); + + // Set back to false + document.setElevate(false); + assertThat(document.isElevate()).isFalse(); + } + + @Test + void testComplexFieldTypes() { + TurSNSiteSearchDocumentBean document = TurSNSiteSearchDocumentBean.builder().build(); + + Map fields = new HashMap<>(); + + // Test various field types + fields.put("stringField", "text value"); + fields.put("intField", 42); + fields.put("doubleField", 3.14); + fields.put("booleanField", true); + fields.put("listField", Arrays.asList("item1", "item2", "item3")); + fields.put("mapField", Collections.singletonMap("nested", "value")); + + document.setFields(fields); + + assertThat(document.getFields().get("stringField")).isEqualTo("text value"); + assertThat(document.getFields().get("intField")).isEqualTo(42); + assertThat(document.getFields().get("doubleField")).isEqualTo(3.14); + assertThat(document.getFields().get("booleanField")).isEqualTo(true); + assertThat(document.getFields().get("listField")).isInstanceOf(List.class); + assertThat(document.getFields().get("mapField")).isInstanceOf(Map.class); + } +} \ No newline at end of file diff --git a/turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentMetadataBeanTest.java b/turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentMetadataBeanTest.java new file mode 100644 index 00000000000..7360ac15874 --- /dev/null +++ b/turing-commons/src/test/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentMetadataBeanTest.java @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2016-2021 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.commons.sn.bean; + +import org.junit.jupiter.api.Test; + +import java.io.Serializable; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNSiteSearchDocumentMetadataBean. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurSNSiteSearchDocumentMetadataBeanTest { + + @Test + void testDefaultConstructor() { + TurSNSiteSearchDocumentMetadataBean metadata = new TurSNSiteSearchDocumentMetadataBean(); + + assertThat(metadata.getHref()).isNull(); + assertThat(metadata.getText()).isNull(); + } + + @Test + void testAllArgsConstructor() { + String href = "https://example.com/document/123"; + String text = "Example Document Title"; + + TurSNSiteSearchDocumentMetadataBean metadata = + new TurSNSiteSearchDocumentMetadataBean(href, text); + + assertThat(metadata.getHref()).isEqualTo(href); + assertThat(metadata.getText()).isEqualTo(text); + } + + @Test + void testBuilderPattern() { + TurSNSiteSearchDocumentMetadataBean metadata = TurSNSiteSearchDocumentMetadataBean.builder() + .href("https://viglet.com") + .text("Viglet Turing") + .build(); + + assertThat(metadata.getHref()).isEqualTo("https://viglet.com"); + assertThat(metadata.getText()).isEqualTo("Viglet Turing"); + } + + @Test + void testBuilderWithNullValues() { + TurSNSiteSearchDocumentMetadataBean metadata = TurSNSiteSearchDocumentMetadataBean.builder() + .href(null) + .text(null) + .build(); + + assertThat(metadata.getHref()).isNull(); + assertThat(metadata.getText()).isNull(); + } + + @Test + void testSettersAndGetters() { + TurSNSiteSearchDocumentMetadataBean metadata = new TurSNSiteSearchDocumentMetadataBean(); + + metadata.setHref("https://example.org/page"); + metadata.setText("Sample Page"); + + assertThat(metadata.getHref()).isEqualTo("https://example.org/page"); + assertThat(metadata.getText()).isEqualTo("Sample Page"); + } + + @Test + void testToBuilder() { + TurSNSiteSearchDocumentMetadataBean original = TurSNSiteSearchDocumentMetadataBean.builder() + .href("https://original.com") + .text("Original Text") + .build(); + + TurSNSiteSearchDocumentMetadataBean modified = original.toBuilder() + .text("Modified Text") + .build(); + + // Original should remain unchanged + assertThat(original.getHref()).isEqualTo("https://original.com"); + assertThat(original.getText()).isEqualTo("Original Text"); + + // Modified should have updated text but same href + assertThat(modified.getHref()).isEqualTo("https://original.com"); + assertThat(modified.getText()).isEqualTo("Modified Text"); + } + + @Test + void testImplementsSerializable() { + TurSNSiteSearchDocumentMetadataBean metadata = new TurSNSiteSearchDocumentMetadataBean(); + + assertThat(metadata).isInstanceOf(Serializable.class); + } + + @Test + void testUpdateFields() { + TurSNSiteSearchDocumentMetadataBean metadata = new TurSNSiteSearchDocumentMetadataBean(); + + // Start with null values + assertThat(metadata.getHref()).isNull(); + assertThat(metadata.getText()).isNull(); + + // Set initial values + metadata.setHref("https://first.com"); + metadata.setText("First Text"); + + assertThat(metadata.getHref()).isEqualTo("https://first.com"); + assertThat(metadata.getText()).isEqualTo("First Text"); + + // Update values + metadata.setHref("https://second.com"); + metadata.setText("Second Text"); + + assertThat(metadata.getHref()).isEqualTo("https://second.com"); + assertThat(metadata.getText()).isEqualTo("Second Text"); + } + + @Test + void testEmptyStringValues() { + TurSNSiteSearchDocumentMetadataBean metadata = TurSNSiteSearchDocumentMetadataBean.builder() + .href("") + .text("") + .build(); + + assertThat(metadata.getHref()).isEmpty(); + assertThat(metadata.getText()).isEmpty(); + } + + @Test + void testLongUrlAndText() { + String longUrl = "https://example.com/very/long/path/to/document/with/many/segments/and/parameters?param1=value1¶m2=value2¶m3=value3"; + String longText = "This is a very long document title that might contain multiple sentences and special characters like émotions, números, and símbolos."; + + TurSNSiteSearchDocumentMetadataBean metadata = TurSNSiteSearchDocumentMetadataBean.builder() + .href(longUrl) + .text(longText) + .build(); + + assertThat(metadata.getHref()).isEqualTo(longUrl); + assertThat(metadata.getText()).isEqualTo(longText); + assertThat(metadata.getHref().length()).isGreaterThan(100); + assertThat(metadata.getText().length()).isGreaterThan(100); + } + + @Test + void testSpecialCharacters() { + String specialHref = "https://example.com/документ/文档/مستند"; + String specialText = "Document with émotions (1), números [2], and símbolos {3}"; + + TurSNSiteSearchDocumentMetadataBean metadata = new TurSNSiteSearchDocumentMetadataBean(); + metadata.setHref(specialHref); + metadata.setText(specialText); + + assertThat(metadata.getHref()).isEqualTo(specialHref); + assertThat(metadata.getText()).isEqualTo(specialText); + } +} \ No newline at end of file diff --git a/turing-commons/src/test/java/com/viglet/turing/commons/utils/TurCommonsUtilsTest.java b/turing-commons/src/test/java/com/viglet/turing/commons/utils/TurCommonsUtilsTest.java new file mode 100644 index 00000000000..55354401e33 --- /dev/null +++ b/turing-commons/src/test/java/com/viglet/turing/commons/utils/TurCommonsUtilsTest.java @@ -0,0 +1,348 @@ +/* + * Copyright (C) 2016-2024 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see . + */ + +package com.viglet.turing.commons.utils; + +import org.apache.commons.collections4.KeyValue; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Unit tests for TurCommonsUtils. + * + * @author Alexandre Oliveira + * @since 0.3.6 + */ +class TurCommonsUtilsTest { + + @Test + void testGetKeyValueFromColonValid() { + String input = "key:value"; + Optional> result = TurCommonsUtils.getKeyValueFromColon(input); + + assertThat(result).isPresent(); + assertThat(result.get().getKey()).isEqualTo("key"); + assertThat(result.get().getValue()).isEqualTo("value"); + } + + @Test + void testGetKeyValueFromColonWithMultipleColons() { + String input = "key:value:extra"; + Optional> result = TurCommonsUtils.getKeyValueFromColon(input); + + assertThat(result).isPresent(); + assertThat(result.get().getKey()).isEqualTo("key"); + assertThat(result.get().getValue()).isEqualTo("value:extra"); + } + + @Test + void testGetKeyValueFromColonNoColon() { + String input = "keyvalue"; + Optional> result = TurCommonsUtils.getKeyValueFromColon(input); + + assertThat(result).isEmpty(); + } + + @Test + void testGetKeyValueFromColonEmptyValue() { + String input = "key:"; + Optional> result = TurCommonsUtils.getKeyValueFromColon(input); + + // Based on the actual implementation, this returns empty when there's only one part after split + assertThat(result).isEmpty(); + } + + @Test + void testIsValidUrlValidUrl() throws MalformedURLException { + URL validUrl = new URL("https://www.example.com"); + boolean result = TurCommonsUtils.isValidUrl(validUrl); + + assertThat(result).isTrue(); + } + + @Test + void testIsValidUrlLocalUrl() throws MalformedURLException { + URL localUrl = new URL("http://localhost:8080/api"); + boolean result = TurCommonsUtils.isValidUrl(localUrl); + + assertThat(result).isTrue(); + } + + @Test + void testIsValidUrlFileUrl() throws MalformedURLException { + URL fileUrl = new URL("file:///tmp/test.txt"); + boolean result = TurCommonsUtils.isValidUrl(fileUrl); + + // Based on the actual behavior, file URLs are considered invalid by the validator + assertThat(result).isFalse(); + } + + @Test + void testHtml2Text() { + String html = "

This is a test with HTML tags.

"; + String result = TurCommonsUtils.html2Text(html); + + assertThat(result).isEqualTo("This is a test with HTML tags."); + } + + @Test + void testHtml2TextWithComplexHtml() { + String html = "

Title

Paragraph with link

"; + String result = TurCommonsUtils.html2Text(html); + + assertThat(result).isEqualTo("Title Paragraph with link"); + } + + @Test + void testText2DescriptionShortText() { + String text = "Short text"; + String result = TurCommonsUtils.text2Description(text, 100); + + assertThat(result).isEqualTo("Short text ..."); + } + + @Test + void testText2DescriptionLongText() { + String text = "This is a very long text that should be truncated at word boundaries " + + "to ensure readability and proper formatting when displayed."; + String result = TurCommonsUtils.text2Description(text, 50); + + assertThat(result).contains("..."); + assertThat(result.length()).isLessThanOrEqualTo(52); // 50 + " ..." + } + + @Test + void testText2DescriptionNullText() { + String result = TurCommonsUtils.text2Description(null, 50); + + assertThat(result).isEqualTo("null ..."); + } + + @Test + void testHtml2Description() { + String html = "

This is HTML content that should be converted to text and truncated.

"; + String result = TurCommonsUtils.html2Description(html, 30); + + assertThat(result).contains("This is HTML content"); + assertThat(result).contains("..."); + } + + @Test + void testAddOrReplaceParameterWithLocale() throws URISyntaxException { + URI uri = new URI("/test?param1=value1"); + Locale locale = Locale.ENGLISH; + URI result = TurCommonsUtils.addOrReplaceParameter(uri, "locale", locale, false); + + assertThat(result.toString()).contains("locale=en"); + } + + @Test + void testAddOrReplaceParameterNewParameter() throws URISyntaxException { + URI uri = new URI("/test?param1=value1"); + URI result = TurCommonsUtils.addOrReplaceParameter(uri, "newParam", "newValue", false); + + assertThat(result.toString()).contains("newParam=newValue"); + assertThat(result.toString()).contains("param1=value1"); + } + + @Test + void testAddOrReplaceParameterExistingParameter() throws URISyntaxException { + URI uri = new URI("/test?param1=oldValue¶m2=value2"); + URI result = TurCommonsUtils.addOrReplaceParameter(uri, "param1", "newValue", false); + + assertThat(result.toString()).contains("param1=newValue"); + assertThat(result.toString()).contains("param2=value2"); + assertThat(result.toString()).doesNotContain("param1=oldValue"); + } + + @Test + void testCleanTextContent() { + String dirtyText = " \t\n Text with multiple\r\n spaces \t "; + String result = TurCommonsUtils.cleanTextContent(dirtyText); + + assertThat(result).isEqualTo("Text with multiple spaces"); + } + + @Test + void testCleanTextContentEmpty() { + String result = TurCommonsUtils.cleanTextContent(""); + + assertThat(result).isEmpty(); + } + + @Test + void testCloneListOfTermsAsString() { + List input = Arrays.asList("term1", "term2", "term3"); + List result = TurCommonsUtils.cloneListOfTermsAsString(input); + + assertThat(result).hasSize(3); + assertThat(result).containsExactly("term1", "term2", "term3"); + } + + @Test + void testAddFilesToZip(@TempDir Path tempDir) throws IOException { + // Create source directory with test files + File sourceDir = tempDir.resolve("source").toFile(); + assertThat(sourceDir.mkdirs()).isTrue(); + + File testFile1 = new File(sourceDir, "test1.txt"); + Files.write(testFile1.toPath(), "Test content 1".getBytes()); + + File testFile2 = new File(sourceDir, "test2.txt"); + Files.write(testFile2.toPath(), "Test content 2".getBytes()); + + // Create destination zip + File destinationZip = tempDir.resolve("test.zip").toFile(); + + // Test the method + TurCommonsUtils.addFilesToZip(sourceDir, destinationZip); + + // Verify zip was created + assertThat(destinationZip).exists(); + assertThat(destinationZip.length()).isGreaterThan(0); + } + + @Test + void testUnZipIt(@TempDir Path tempDir) throws IOException { + // Create a test file structure + File sourceDir = tempDir.resolve("source").toFile(); + assertThat(sourceDir.mkdirs()).isTrue(); + + File testFile = new File(sourceDir, "test.txt"); + Files.write(testFile.toPath(), "Test content".getBytes()); + + // Create zip + File zipFile = tempDir.resolve("test.zip").toFile(); + TurCommonsUtils.addFilesToZip(sourceDir, zipFile); + + // Create extraction directory + File extractDir = tempDir.resolve("extract").toFile(); + assertThat(extractDir.mkdirs()).isTrue(); + + // Test unzipping + TurCommonsUtils.unZipIt(zipFile, extractDir); + + // Verify extracted files + File extractedFile = new File(extractDir, "test.txt"); + assertThat(extractedFile).exists(); + assertThat(Files.readString(extractedFile.toPath())).isEqualTo("Test content"); + } + + @Test + void testIsValidJsonValidObject() { + String validJson = "{\"key\": \"value\", \"number\": 42}"; + boolean result = TurCommonsUtils.isValidJson(validJson); + + assertThat(result).isTrue(); + } + + @Test + void testIsValidJsonValidArray() { + String validJsonArray = "[{\"key\": \"value1\"}, {\"key\": \"value2\"}]"; + boolean result = TurCommonsUtils.isValidJson(validJsonArray); + + assertThat(result).isTrue(); + } + + @Test + void testIsValidJsonInvalidJson() { + String invalidJson = "{invalid json structure"; + boolean result = TurCommonsUtils.isValidJson(invalidJson); + + assertThat(result).isFalse(); + } + + @Test + void testAsJsonStringValidObject() throws Exception { + TestObject testObject = new TestObject("test", 42); + String result = TurCommonsUtils.asJsonString(testObject); + + assertThat(result).contains("\"name\":\"test\""); + assertThat(result).contains("\"value\":42"); + } + + @Test + void testAsJsonStringNullObject() throws Exception { + // Test with null - should return "null" as JSON string + String result = TurCommonsUtils.asJsonString(null); + assertThat(result).isEqualTo("null"); + } + + @Test + void testGetStoreDir() { + File storeDir = TurCommonsUtils.getStoreDir(); + + assertThat(storeDir).isNotNull(); + assertThat(storeDir.getName()).isEqualTo("store"); + assertThat(storeDir).exists(); + } + + @Test + void testAddSubDirToStoreDir() { + String subDirName = "testSubDir"; + File subDir = TurCommonsUtils.addSubDirToStoreDir(subDirName); + + assertThat(subDir).isNotNull(); + assertThat(subDir.getName()).isEqualTo(subDirName); + assertThat(subDir).exists(); + + // Clean up + assertThat(subDir.delete()).isTrue(); + } + + @Test + void testGetTempDirectory() { + File tempDir = TurCommonsUtils.getTempDirectory(); + + assertThat(tempDir).isNotNull(); + assertThat(tempDir.getName()).isEqualTo("tmp"); + assertThat(tempDir).exists(); + } + + // Helper class for JSON testing + public static class TestObject { + private final String name; + private final int value; + + public TestObject(String name, int value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public int getValue() { + return value; + } + } +} \ No newline at end of file From fbaaf8c00e7d6b27320af47c8b1e75a34a579435 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Sep 2025 14:16:57 +0000 Subject: [PATCH 3/3] Phase 2 complete: Comprehensive unit tests for turing-java-sdk module Co-authored-by: alegauss <331174+alegauss@users.noreply.github.com> --- turing-java-sdk/pom.xml | 25 ++ .../turing/client/sn/TurMultiValueTest.java | 272 ++++++++++++++++ .../sn/TurSNClientBetweenDatesTest.java | 177 +++++++++++ .../turing/client/sn/TurSNDocumentTest.java | 251 +++++++++++++++ .../turing/client/sn/TurSNLocaleTest.java | 231 ++++++++++++++ .../turing/client/sn/TurSNQueryTest.java | 296 ++++++++++++++++++ .../turing/client/sn/TurSNSortFieldTest.java | 127 ++++++++ .../client/utils/TurClientUtilsTest.java | 146 +++++++++ 8 files changed, 1525 insertions(+) create mode 100644 turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurMultiValueTest.java create mode 100644 turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNClientBetweenDatesTest.java create mode 100644 turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNDocumentTest.java create mode 100644 turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNLocaleTest.java create mode 100644 turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNQueryTest.java create mode 100644 turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNSortFieldTest.java create mode 100644 turing-java-sdk/src/test/java/com/viglet/turing/client/utils/TurClientUtilsTest.java diff --git a/turing-java-sdk/pom.xml b/turing-java-sdk/pom.xml index 0d108d556d2..db600558e5a 100644 --- a/turing-java-sdk/pom.xml +++ b/turing-java-sdk/pom.xml @@ -35,6 +35,31 @@ 2.20.0 compile + + + org.junit.jupiter + junit-jupiter + 5.11.4 + test + + + org.mockito + mockito-core + 5.15.2 + test + + + org.mockito + mockito-junit-jupiter + 5.15.2 + test + + + org.assertj + assertj-core + 3.26.3 + test + turing-java-sdk diff --git a/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurMultiValueTest.java b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurMultiValueTest.java new file mode 100644 index 00000000000..2c60607ca58 --- /dev/null +++ b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurMultiValueTest.java @@ -0,0 +1,272 @@ +/* + * Copyright (C) 2016-2022 the original author or authors. + * + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you 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. + */ + +package com.viglet.turing.client.sn; + +import org.junit.jupiter.api.Test; + +import java.util.*; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurMultiValue. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurMultiValueTest { + + @Test + void testDefaultConstructor() { + TurMultiValue multiValue = new TurMultiValue(); + + assertThat(multiValue).isEmpty(); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testConstructorWithOverride() { + TurMultiValue multiValue = new TurMultiValue(true); + + assertThat(multiValue).isEmpty(); + assertThat(multiValue.isOverride()).isTrue(); + } + + @Test + void testConstructorWithCollection() { + List items = Arrays.asList("item1", "item2", "item3"); + TurMultiValue multiValue = new TurMultiValue(items); + + assertThat(multiValue).hasSize(3); + assertThat(multiValue).containsExactly("item1", "item2", "item3"); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testConstructorWithCollectionAndOverride() { + List items = Arrays.asList("item1", "item2"); + TurMultiValue multiValue = new TurMultiValue(items, true); + + assertThat(multiValue).hasSize(2); + assertThat(multiValue).containsExactly("item1", "item2"); + assertThat(multiValue.isOverride()).isTrue(); + } + + @Test + void testSingleItemString() { + TurMultiValue multiValue = TurMultiValue.singleItem("test"); + + assertThat(multiValue).hasSize(1); + assertThat(multiValue).containsExactly("test"); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testSingleItemStringWithOverride() { + TurMultiValue multiValue = TurMultiValue.singleItem("test", true); + + assertThat(multiValue).hasSize(1); + assertThat(multiValue).containsExactly("test"); + assertThat(multiValue.isOverride()).isTrue(); + } + + @Test + void testSingleItemBoolean() { + TurMultiValue multiValueTrue = TurMultiValue.singleItem(true); + TurMultiValue multiValueFalse = TurMultiValue.singleItem(false); + + assertThat(multiValueTrue).hasSize(1); + assertThat(multiValueTrue).containsExactly("true"); + assertThat(multiValueTrue.isOverride()).isFalse(); + + assertThat(multiValueFalse).hasSize(1); + assertThat(multiValueFalse).containsExactly("false"); + assertThat(multiValueFalse.isOverride()).isFalse(); + } + + @Test + void testSingleItemBooleanObject() { + TurMultiValue multiValue = TurMultiValue.singleItem(Boolean.TRUE, true); + + assertThat(multiValue).hasSize(1); + assertThat(multiValue).containsExactly("true"); + assertThat(multiValue.isOverride()).isTrue(); + } + + @Test + void testSingleItemDate() { + Date testDate = new Date(1609459200000L); // 2021-01-01 00:00:00 UTC + TurMultiValue multiValue = TurMultiValue.singleItem(testDate); + + assertThat(multiValue).hasSize(1); + assertThat(multiValue.get(0)).matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testSingleItemDateWithOverride() { + Date testDate = new Date(1609459200000L); // 2021-01-01 00:00:00 UTC + TurMultiValue multiValue = TurMultiValue.singleItem(testDate, true); + + assertThat(multiValue).hasSize(1); + assertThat(multiValue.get(0)).matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"); + assertThat(multiValue.isOverride()).isTrue(); + } + + @Test + void testSingleItemNullDate() { + Date nullDate = null; + TurMultiValue multiValue = TurMultiValue.singleItem(nullDate, false); + + assertThat(multiValue).isEmpty(); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testSingleItemGenericObject() { + Integer number = 42; + TurMultiValue multiValue = TurMultiValue.singleItem(number); + + assertThat(multiValue).hasSize(1); + assertThat(multiValue).containsExactly("42"); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testSingleItemNullObject() { + Object nullObject = null; + TurMultiValue multiValue = TurMultiValue.singleItem(nullObject); + + assertThat(multiValue).isNull(); + } + + @Test + void testFromList() { + List list = Arrays.asList("a", "b", "c", "d"); + TurMultiValue multiValue = TurMultiValue.fromList(list); + + assertThat(multiValue).hasSize(4); + assertThat(multiValue).containsExactly("a", "b", "c", "d"); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testFromEmptyList() { + List emptyList = new ArrayList<>(); + TurMultiValue multiValue = TurMultiValue.fromList(emptyList); + + assertThat(multiValue).isEmpty(); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testEmpty() { + TurMultiValue multiValue = TurMultiValue.empty(); + + assertThat(multiValue).isEmpty(); + assertThat(multiValue.isOverride()).isFalse(); + } + + @Test + void testEqualsAndHashCode() { + List items1 = Arrays.asList("item1", "item2"); + List items2 = Arrays.asList("item1", "item2"); + + TurMultiValue multiValue1 = new TurMultiValue(items1); + TurMultiValue multiValue2 = new TurMultiValue(items2); + + assertThat(multiValue1).isEqualTo(multiValue2); + assertThat(multiValue1.hashCode()).isEqualTo(multiValue2.hashCode()); + } + + @Test + void testNotEquals() { + TurMultiValue multiValue1 = TurMultiValue.singleItem("item1"); + TurMultiValue multiValue2 = TurMultiValue.singleItem("item2"); + + assertThat(multiValue1).isNotEqualTo(multiValue2); + } + + @Test + void testArrayListBehavior() { + TurMultiValue multiValue = new TurMultiValue(); + + // Test ArrayList functionality + multiValue.add("first"); + multiValue.add("second"); + multiValue.add(1, "middle"); + + assertThat(multiValue).hasSize(3); + assertThat(multiValue).containsExactly("first", "middle", "second"); + assertThat(multiValue.get(1)).isEqualTo("middle"); + + // Test removal + multiValue.remove("middle"); + assertThat(multiValue).containsExactly("first", "second"); + } + + @Test + void testDateFormatConstants() { + assertThat(TurMultiValue.DATE_FORMAT).isEqualTo("yyyy-MM-dd'T'HH:mm:ss'Z'"); + assertThat(TurMultiValue.UTC).isEqualTo("UTC"); + } + + @Test + void testDateFormattingConsistency() { + // Test that the same date always produces the same formatted string + Date testDate = new Date(1609459200000L); // 2021-01-01 00:00:00 UTC + + TurMultiValue multiValue1 = TurMultiValue.singleItem(testDate); + TurMultiValue multiValue2 = TurMultiValue.singleItem(testDate); + + assertThat(multiValue1.get(0)).isEqualTo(multiValue2.get(0)); + } + + @Test + void testCollectionWithNulls() { + List listWithNulls = Arrays.asList("item1", null, "item3"); + TurMultiValue multiValue = new TurMultiValue(listWithNulls); + + assertThat(multiValue).hasSize(3); + assertThat(multiValue).containsExactly("item1", null, "item3"); + } + + @Test + void testOverrideBehaviorPreservation() { + // Test that override flag is preserved through operations + TurMultiValue multiValue = new TurMultiValue(true); + multiValue.add("item1"); + multiValue.add("item2"); + + assertThat(multiValue.isOverride()).isTrue(); + assertThat(multiValue).hasSize(2); + } + + @Test + void testGenericTypeHandling() { + // Test various generic types + Double doubleValue = 3.14159; + TurMultiValue multiValueDouble = TurMultiValue.singleItem(doubleValue); + + Long longValue = 123456789L; + TurMultiValue multiValueLong = TurMultiValue.singleItem(longValue); + + assertThat(multiValueDouble).containsExactly("3.14159"); + assertThat(multiValueLong).containsExactly("123456789"); + } +} \ No newline at end of file diff --git a/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNClientBetweenDatesTest.java b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNClientBetweenDatesTest.java new file mode 100644 index 00000000000..92173f9bd89 --- /dev/null +++ b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNClientBetweenDatesTest.java @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2016-2021 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.client.sn; + +import org.junit.jupiter.api.Test; + +import java.util.Date; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNClientBetweenDates. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurSNClientBetweenDatesTest { + + @Test + void testConstructorWithParameters() { + Date startDate = new Date(System.currentTimeMillis() - 86400000L); // 1 day ago + Date endDate = new Date(); + + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("publishDate", startDate, endDate); + + assertThat(betweenDates.getField()).isEqualTo("publishDate"); + assertThat(betweenDates.getStartDate()).isEqualTo(startDate); + assertThat(betweenDates.getEndDate()).isEqualTo(endDate); + } + + @Test + void testFieldSetterAndGetter() { + Date now = new Date(); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("initialField", now, now); + + betweenDates.setField("modifiedDate"); + + assertThat(betweenDates.getField()).isEqualTo("modifiedDate"); + } + + @Test + void testStartDateSetterAndGetter() { + Date initialDate = new Date(0); + Date newStartDate = new Date(System.currentTimeMillis() - 86400000L); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("date", initialDate, initialDate); + + betweenDates.setStartDate(newStartDate); + + assertThat(betweenDates.getStartDate()).isEqualTo(newStartDate); + } + + @Test + void testEndDateSetterAndGetter() { + Date initialDate = new Date(0); + Date newEndDate = new Date(); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("date", initialDate, initialDate); + + betweenDates.setEndDate(newEndDate); + + assertThat(betweenDates.getEndDate()).isEqualTo(newEndDate); + } + + @Test + void testNullValues() { + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates(null, null, null); + + assertThat(betweenDates.getField()).isNull(); + assertThat(betweenDates.getStartDate()).isNull(); + assertThat(betweenDates.getEndDate()).isNull(); + } + + @Test + void testSetNullValues() { + Date now = new Date(); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("field", now, now); + + betweenDates.setField(null); + betweenDates.setStartDate(null); + betweenDates.setEndDate(null); + + assertThat(betweenDates.getField()).isNull(); + assertThat(betweenDates.getStartDate()).isNull(); + assertThat(betweenDates.getEndDate()).isNull(); + } + + @Test + void testEmptyFieldName() { + Date now = new Date(); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("", now, now); + + assertThat(betweenDates.getField()).isEmpty(); + } + + @Test + void testSameStartAndEndDates() { + Date sameDate = new Date(); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("createdAt", sameDate, sameDate); + + assertThat(betweenDates.getStartDate()).isEqualTo(betweenDates.getEndDate()); + } + + @Test + void testStartDateAfterEndDate() { + Date startDate = new Date(); + Date endDate = new Date(System.currentTimeMillis() - 86400000L); // 1 day ago + + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("date", startDate, endDate); + + assertThat(betweenDates.getStartDate()).isAfter(betweenDates.getEndDate()); + } + + @Test + void testFieldWithSpecialCharacters() { + Date now = new Date(); + String fieldWithSpecialChars = "field_with-special.chars"; + + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates(fieldWithSpecialChars, now, now); + + assertThat(betweenDates.getField()).isEqualTo(fieldWithSpecialChars); + } + + @Test + void testDateRangeUpdate() { + Date initialStart = new Date(0); + Date initialEnd = new Date(86400000L); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("date", initialStart, initialEnd); + + // Update to new range + Date newStart = new Date(System.currentTimeMillis() - 86400000L); + Date newEnd = new Date(); + + betweenDates.setStartDate(newStart); + betweenDates.setEndDate(newEnd); + + assertThat(betweenDates.getStartDate()).isEqualTo(newStart); + assertThat(betweenDates.getEndDate()).isEqualTo(newEnd); + } + + @Test + void testTypicalUsageScenario() { + // Simulate typical usage: last 30 days + Date thirtyDaysAgo = new Date(System.currentTimeMillis() - (30L * 24 * 60 * 60 * 1000)); + Date now = new Date(); + + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("publishDate", thirtyDaysAgo, now); + + assertThat(betweenDates.getField()).isEqualTo("publishDate"); + assertThat(betweenDates.getStartDate()).isBefore(betweenDates.getEndDate()); + assertThat(betweenDates.getStartDate()).isBefore(now); + assertThat(betweenDates.getEndDate()).isAfter(thirtyDaysAgo); + } + + @Test + void testVeryOldDates() { + Date epochDate = new Date(0); // 1970-01-01 + Date oldDate = new Date(946684800000L); // 2000-01-01 + + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("legacyDate", epochDate, oldDate); + + assertThat(betweenDates.getStartDate()).isEqualTo(epochDate); + assertThat(betweenDates.getEndDate()).isEqualTo(oldDate); + } +} \ No newline at end of file diff --git a/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNDocumentTest.java b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNDocumentTest.java new file mode 100644 index 00000000000..3396b5a1fc6 --- /dev/null +++ b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNDocumentTest.java @@ -0,0 +1,251 @@ +/* + * Copyright (C) 2016-2021 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.client.sn; + +import com.viglet.turing.commons.sn.bean.TurSNSiteSearchDocumentBean; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNDocument. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurSNDocumentTest { + + @Test + void testDefaultConstructor() { + TurSNDocument document = new TurSNDocument(); + + assertThat(document.getContent()).isNull(); + } + + @Test + void testSetAndGetContent() { + TurSNDocument document = new TurSNDocument(); + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder().build(); + + document.setContent(bean); + + assertThat(document.getContent()).isEqualTo(bean); + } + + @Test + void testGetFieldValueWithNullContent() { + TurSNDocument document = new TurSNDocument(); + + Object value = document.getFieldValue("title"); + + assertThat(value).isNull(); + } + + @Test + void testGetFieldValueWithNullFields() { + TurSNDocument document = new TurSNDocument(); + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(null) + .build(); + + document.setContent(bean); + + Object value = document.getFieldValue("title"); + + assertThat(value).isNull(); + } + + @Test + void testGetFieldValueWithEmptyFields() { + TurSNDocument document = new TurSNDocument(); + Map fields = new HashMap<>(); + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + + Object value = document.getFieldValue("title"); + + assertThat(value).isNull(); + } + + @Test + void testGetFieldValueWithExistingField() { + TurSNDocument document = new TurSNDocument(); + Map fields = new HashMap<>(); + fields.put("title", "Test Document Title"); + fields.put("content", "Test document content"); + fields.put("id", 123); + + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + + assertThat(document.getFieldValue("title")).isEqualTo("Test Document Title"); + assertThat(document.getFieldValue("content")).isEqualTo("Test document content"); + assertThat(document.getFieldValue("id")).isEqualTo(123); + } + + @Test + void testGetFieldValueWithNonExistentField() { + TurSNDocument document = new TurSNDocument(); + Map fields = new HashMap<>(); + fields.put("title", "Test Document Title"); + + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + + Object value = document.getFieldValue("nonexistent"); + + assertThat(value).isNull(); + } + + @Test + void testGetFieldValueWithNullValue() { + TurSNDocument document = new TurSNDocument(); + Map fields = new HashMap<>(); + fields.put("title", "Test Document Title"); + fields.put("description", null); + + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + + assertThat(document.getFieldValue("title")).isEqualTo("Test Document Title"); + assertThat(document.getFieldValue("description")).isNull(); + } + + @Test + void testGetFieldValueWithComplexTypes() { + TurSNDocument document = new TurSNDocument(); + Map fields = new HashMap<>(); + + // Add various data types + fields.put("stringField", "string value"); + fields.put("intField", 42); + fields.put("doubleField", 3.14159); + fields.put("booleanField", true); + fields.put("arrayField", new String[]{"item1", "item2", "item3"}); + + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + + assertThat(document.getFieldValue("stringField")).isEqualTo("string value"); + assertThat(document.getFieldValue("intField")).isEqualTo(42); + assertThat(document.getFieldValue("doubleField")).isEqualTo(3.14159); + assertThat(document.getFieldValue("booleanField")).isEqualTo(true); + assertThat(document.getFieldValue("arrayField")).isInstanceOf(String[].class); + } + + @Test + void testSetContentOverwrite() { + TurSNDocument document = new TurSNDocument(); + + // Set initial content + Map fields1 = new HashMap<>(); + fields1.put("title", "First Title"); + TurSNSiteSearchDocumentBean bean1 = TurSNSiteSearchDocumentBean.builder() + .fields(fields1) + .build(); + + document.setContent(bean1); + assertThat(document.getFieldValue("title")).isEqualTo("First Title"); + + // Overwrite with new content + Map fields2 = new HashMap<>(); + fields2.put("title", "Second Title"); + TurSNSiteSearchDocumentBean bean2 = TurSNSiteSearchDocumentBean.builder() + .fields(fields2) + .build(); + + document.setContent(bean2); + assertThat(document.getFieldValue("title")).isEqualTo("Second Title"); + assertThat(document.getContent()).isEqualTo(bean2); + } + + @Test + void testSetContentToNull() { + TurSNDocument document = new TurSNDocument(); + + // Set initial content + Map fields = new HashMap<>(); + fields.put("title", "Test Title"); + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + assertThat(document.getContent()).isNotNull(); + assertThat(document.getFieldValue("title")).isEqualTo("Test Title"); + + // Set content to null + document.setContent(null); + assertThat(document.getContent()).isNull(); + assertThat(document.getFieldValue("title")).isNull(); + } + + @Test + void testFieldCaseSensitivity() { + TurSNDocument document = new TurSNDocument(); + Map fields = new HashMap<>(); + fields.put("title", "Test Title"); + fields.put("Title", "Different Title"); + fields.put("TITLE", "Another Title"); + + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + + // Field names should be case-sensitive + assertThat(document.getFieldValue("title")).isEqualTo("Test Title"); + assertThat(document.getFieldValue("Title")).isEqualTo("Different Title"); + assertThat(document.getFieldValue("TITLE")).isEqualTo("Another Title"); + } + + @Test + void testEmptyStringFieldName() { + TurSNDocument document = new TurSNDocument(); + Map fields = new HashMap<>(); + fields.put("", "Empty key value"); + fields.put("normal", "Normal value"); + + TurSNSiteSearchDocumentBean bean = TurSNSiteSearchDocumentBean.builder() + .fields(fields) + .build(); + + document.setContent(bean); + + assertThat(document.getFieldValue("")).isEqualTo("Empty key value"); + assertThat(document.getFieldValue("normal")).isEqualTo("Normal value"); + } +} \ No newline at end of file diff --git a/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNLocaleTest.java b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNLocaleTest.java new file mode 100644 index 00000000000..95d188ac93d --- /dev/null +++ b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNLocaleTest.java @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2016-2021 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.client.sn; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNLocale. + * + * @author Alexandre Oliveira + * @since 0.3.5 + */ +class TurSNLocaleTest { + + @Test + void testDefaultConstructor() { + TurSNLocale locale = new TurSNLocale(); + + assertThat(locale.getLocale()).isNull(); + assertThat(locale.getLink()).isNull(); + } + + @Test + void testSettersAndGetters() { + TurSNLocale locale = new TurSNLocale(); + + locale.setLocale("en_US"); + locale.setLink("/search?locale=en_US"); + + assertThat(locale.getLocale()).isEqualTo("en_US"); + assertThat(locale.getLink()).isEqualTo("/search?locale=en_US"); + } + + @Test + void testToStringWithBothValues() { + TurSNLocale locale = new TurSNLocale(); + locale.setLocale("pt_BR"); + locale.setLink("/search?locale=pt_BR"); + + String result = locale.toString(); + + assertThat(result).isEqualTo("pt_BR: /search?locale=pt_BR"); + } + + @Test + void testToStringWithNullValues() { + TurSNLocale locale = new TurSNLocale(); + + String result = locale.toString(); + + assertThat(result).isEqualTo("null: null"); + } + + @Test + void testToStringWithNullLocale() { + TurSNLocale locale = new TurSNLocale(); + locale.setLink("/search"); + + String result = locale.toString(); + + assertThat(result).isEqualTo("null: /search"); + } + + @Test + void testToStringWithNullLink() { + TurSNLocale locale = new TurSNLocale(); + locale.setLocale("fr_FR"); + + String result = locale.toString(); + + assertThat(result).isEqualTo("fr_FR: null"); + } + + @Test + void testSetLocaleWithDifferentFormats() { + TurSNLocale locale = new TurSNLocale(); + + // Test with language only + locale.setLocale("en"); + assertThat(locale.getLocale()).isEqualTo("en"); + + // Test with language and country + locale.setLocale("en_US"); + assertThat(locale.getLocale()).isEqualTo("en_US"); + + // Test with language, country and variant + locale.setLocale("en_US_POSIX"); + assertThat(locale.getLocale()).isEqualTo("en_US_POSIX"); + } + + @Test + void testSetLinkWithDifferentFormats() { + TurSNLocale locale = new TurSNLocale(); + + // Test absolute URL + locale.setLink("https://example.com/search?locale=en"); + assertThat(locale.getLink()).isEqualTo("https://example.com/search?locale=en"); + + // Test relative URL + locale.setLink("/api/search"); + assertThat(locale.getLink()).isEqualTo("/api/search"); + + // Test with query parameters + locale.setLink("/search?q=test&locale=en_US&page=1"); + assertThat(locale.getLink()).isEqualTo("/search?q=test&locale=en_US&page=1"); + } + + @Test + void testEmptyStringValues() { + TurSNLocale locale = new TurSNLocale(); + + locale.setLocale(""); + locale.setLink(""); + + assertThat(locale.getLocale()).isEmpty(); + assertThat(locale.getLink()).isEmpty(); + assertThat(locale.toString()).isEqualTo(": "); + } + + @Test + void testUpdateValues() { + TurSNLocale locale = new TurSNLocale(); + + // Set initial values + locale.setLocale("en"); + locale.setLink("/search/en"); + + assertThat(locale.getLocale()).isEqualTo("en"); + assertThat(locale.getLink()).isEqualTo("/search/en"); + + // Update values + locale.setLocale("es"); + locale.setLink("/search/es"); + + assertThat(locale.getLocale()).isEqualTo("es"); + assertThat(locale.getLink()).isEqualTo("/search/es"); + } + + @Test + void testSpecialCharactersInLocale() { + TurSNLocale locale = new TurSNLocale(); + + locale.setLocale("zh_CN@calendar=chinese"); + assertThat(locale.getLocale()).isEqualTo("zh_CN@calendar=chinese"); + } + + @Test + void testSpecialCharactersInLink() { + TurSNLocale locale = new TurSNLocale(); + + locale.setLink("/search?q=café&locale=fr_FR"); + assertThat(locale.getLink()).isEqualTo("/search?q=café&locale=fr_FR"); + } + + @Test + void testToStringFormat() { + TurSNLocale locale = new TurSNLocale(); + locale.setLocale("de_DE"); + locale.setLink("https://example.de/suchen"); + + String result = locale.toString(); + + // Verify the exact format: "locale: link" + assertThat(result).matches("de_DE: https://example\\.de/suchen"); + assertThat(result).contains(": "); + assertThat(result).startsWith("de_DE"); + assertThat(result).endsWith("https://example.de/suchen"); + } + + @Test + void testLocaleNormalization() { + TurSNLocale locale = new TurSNLocale(); + + // Test various locale formats + String[] locales = { + "en", + "EN", + "en_US", + "en_us", + "EN_US", + "zh_Hans_CN" + }; + + for (String localeStr : locales) { + locale.setLocale(localeStr); + assertThat(locale.getLocale()).isEqualTo(localeStr); + } + } + + @Test + void testLinkWithFragment() { + TurSNLocale locale = new TurSNLocale(); + + locale.setLink("/search?locale=en_US#results"); + assertThat(locale.getLink()).isEqualTo("/search?locale=en_US#results"); + + String result = locale.toString(); + assertThat(result).contains("#results"); + } + + @Test + void testMultipleLocaleObjects() { + TurSNLocale locale1 = new TurSNLocale(); + locale1.setLocale("en_US"); + locale1.setLink("/en"); + + TurSNLocale locale2 = new TurSNLocale(); + locale2.setLocale("fr_FR"); + locale2.setLink("/fr"); + + assertThat(locale1.getLocale()).isNotEqualTo(locale2.getLocale()); + assertThat(locale1.getLink()).isNotEqualTo(locale2.getLink()); + assertThat(locale1.toString()).isNotEqualTo(locale2.toString()); + } +} \ No newline at end of file diff --git a/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNQueryTest.java b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNQueryTest.java new file mode 100644 index 00000000000..681d6472176 --- /dev/null +++ b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNQueryTest.java @@ -0,0 +1,296 @@ +/* + * Copyright (C) 2016-2021 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.client.sn; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNQuery. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurSNQueryTest { + + @Test + void testDefaultConstructor() { + TurSNQuery query = new TurSNQuery(); + + assertThat(query.getQuery()).isNull(); + assertThat(query.getRows()).isEqualTo(0); + assertThat(query.getGroupBy()).isNull(); + assertThat(query.getSortField()).isNull(); + assertThat(query.getBetweenDates()).isNull(); + assertThat(query.getFieldQueries()).isNull(); + assertThat(query.getTargetingRules()).isNull(); + assertThat(query.getPageNumber()).isEqualTo(0); + assertThat(query.isPopulateMetrics()).isFalse(); + } + + @Test + void testQuerySetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + + query.setQuery("test query"); + + assertThat(query.getQuery()).isEqualTo("test query"); + } + + @Test + void testRowsSetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + + query.setRows(50); + + assertThat(query.getRows()).isEqualTo(50); + } + + @Test + void testGroupBySetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + + query.setGroupBy("category"); + + assertThat(query.getGroupBy()).isEqualTo("category"); + } + + @Test + void testSortFieldSetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + TurSNSortField sortField = new TurSNSortField(); + + query.setSortField(sortField); + + assertThat(query.getSortField()).isEqualTo(sortField); + } + + @Test + void testSetSortFieldWithFieldAndOrder() { + TurSNQuery query = new TurSNQuery(); + + query.setSortField("title", TurSNQuery.ORDER.asc); + + assertThat(query.getSortField()).isNotNull(); + assertThat(query.getSortField().getField()).isEqualTo("title"); + assertThat(query.getSortField().getSort()).isEqualTo(TurSNQuery.ORDER.asc); + } + + @Test + void testSetSortFieldWithOrderOnly() { + TurSNQuery query = new TurSNQuery(); + + query.setSortField(TurSNQuery.ORDER.desc); + + assertThat(query.getSortField()).isNotNull(); + assertThat(query.getSortField().getField()).isNull(); + assertThat(query.getSortField().getSort()).isEqualTo(TurSNQuery.ORDER.desc); + } + + @Test + void testSetSortFieldOverwritesExisting() { + TurSNQuery query = new TurSNQuery(); + + // Set initial sort field + query.setSortField("date", TurSNQuery.ORDER.desc); + assertThat(query.getSortField().getField()).isEqualTo("date"); + assertThat(query.getSortField().getSort()).isEqualTo(TurSNQuery.ORDER.desc); + + // Overwrite with new field + query.setSortField("title", TurSNQuery.ORDER.asc); + assertThat(query.getSortField().getField()).isEqualTo("title"); + assertThat(query.getSortField().getSort()).isEqualTo(TurSNQuery.ORDER.asc); + } + + @Test + void testBetweenDatesSetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + Date now = new Date(); + TurSNClientBetweenDates betweenDates = new TurSNClientBetweenDates("testField", now, now); + + query.setBetweenDates(betweenDates); + + assertThat(query.getBetweenDates()).isEqualTo(betweenDates); + } + + @Test + void testSetBetweenDatesWithParameters() { + TurSNQuery query = new TurSNQuery(); + Date startDate = new Date(System.currentTimeMillis() - 86400000L); // 1 day ago + Date endDate = new Date(); + + query.setBetweenDates("publishDate", startDate, endDate); + + assertThat(query.getBetweenDates()).isNotNull(); + assertThat(query.getBetweenDates().getField()).isEqualTo("publishDate"); + assertThat(query.getBetweenDates().getStartDate()).isEqualTo(startDate); + assertThat(query.getBetweenDates().getEndDate()).isEqualTo(endDate); + } + + @Test + void testAddFilterQuerySingle() { + TurSNQuery query = new TurSNQuery(); + + query.addFilterQuery("category:news"); + + assertThat(query.getFieldQueries()).hasSize(1); + assertThat(query.getFieldQueries()).contains("category:news"); + } + + @Test + void testAddFilterQueryMultiple() { + TurSNQuery query = new TurSNQuery(); + + query.addFilterQuery("category:news", "status:published", "author:john"); + + assertThat(query.getFieldQueries()).hasSize(3); + assertThat(query.getFieldQueries()).containsExactly("category:news", "status:published", "author:john"); + } + + @Test + void testAddFilterQueryMultipleCalls() { + TurSNQuery query = new TurSNQuery(); + + query.addFilterQuery("category:news"); + query.addFilterQuery("status:published"); + + assertThat(query.getFieldQueries()).hasSize(2); + assertThat(query.getFieldQueries()).containsExactly("category:news", "status:published"); + } + + @Test + void testFieldQueriesSetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + List fieldQueries = Arrays.asList("type:article", "lang:en"); + + query.setFieldQueries(fieldQueries); + + assertThat(query.getFieldQueries()).isEqualTo(fieldQueries); + } + + @Test + void testAddTargetingRuleSingle() { + TurSNQuery query = new TurSNQuery(); + + query.addTargetingRule("location:US"); + + assertThat(query.getTargetingRules()).hasSize(1); + assertThat(query.getTargetingRules()).contains("location:US"); + } + + @Test + void testAddTargetingRuleMultiple() { + TurSNQuery query = new TurSNQuery(); + + query.addTargetingRule("location:US", "age:adult", "interest:tech"); + + assertThat(query.getTargetingRules()).hasSize(3); + assertThat(query.getTargetingRules()).containsExactly("location:US", "age:adult", "interest:tech"); + } + + @Test + void testTargetingRulesSetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + List targetingRules = Arrays.asList("segment:premium", "tier:gold"); + + query.setTargetingRules(targetingRules); + + assertThat(query.getTargetingRules()).isEqualTo(targetingRules); + } + + @Test + void testPageNumberSetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + + query.setPageNumber(5); + + assertThat(query.getPageNumber()).isEqualTo(5); + } + + @Test + void testPopulateMetricsSetterAndGetter() { + TurSNQuery query = new TurSNQuery(); + + query.setPopulateMetrics(true); + + assertThat(query.isPopulateMetrics()).isTrue(); + } + + @Test + void testCompleteQueryConfiguration() { + TurSNQuery query = new TurSNQuery(); + + // Configure all aspects of the query + query.setQuery("search term"); + query.setRows(20); + query.setGroupBy("category"); + query.setSortField("date", TurSNQuery.ORDER.desc); + query.setBetweenDates("publishDate", new Date(0), new Date()); + query.addFilterQuery("status:active", "type:article"); + query.addTargetingRule("location:US"); + query.setPageNumber(2); + query.setPopulateMetrics(true); + + // Verify all settings + assertThat(query.getQuery()).isEqualTo("search term"); + assertThat(query.getRows()).isEqualTo(20); + assertThat(query.getGroupBy()).isEqualTo("category"); + assertThat(query.getSortField().getField()).isEqualTo("date"); + assertThat(query.getSortField().getSort()).isEqualTo(TurSNQuery.ORDER.desc); + assertThat(query.getBetweenDates()).isNotNull(); + assertThat(query.getFieldQueries()).hasSize(2); + assertThat(query.getTargetingRules()).hasSize(1); + assertThat(query.getPageNumber()).isEqualTo(2); + assertThat(query.isPopulateMetrics()).isTrue(); + } + + @Test + void testOrderEnum() { + // Test enum values + assertThat(TurSNQuery.ORDER.asc).isNotNull(); + assertThat(TurSNQuery.ORDER.desc).isNotNull(); + + // Test enum string representations + assertThat(TurSNQuery.ORDER.asc.toString()).isEqualTo("asc"); + assertThat(TurSNQuery.ORDER.desc.toString()).isEqualTo("desc"); + } + + @Test + void testNullHandling() { + TurSNQuery query = new TurSNQuery(); + + // Test setting null values + query.setQuery(null); + query.setGroupBy(null); + query.setSortField((TurSNSortField) null); + query.setBetweenDates(null); + query.setFieldQueries(null); + query.setTargetingRules(null); + + assertThat(query.getQuery()).isNull(); + assertThat(query.getGroupBy()).isNull(); + assertThat(query.getSortField()).isNull(); + assertThat(query.getBetweenDates()).isNull(); + assertThat(query.getFieldQueries()).isNull(); + assertThat(query.getTargetingRules()).isNull(); + } +} \ No newline at end of file diff --git a/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNSortFieldTest.java b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNSortFieldTest.java new file mode 100644 index 00000000000..946d75e8b1a --- /dev/null +++ b/turing-java-sdk/src/test/java/com/viglet/turing/client/sn/TurSNSortFieldTest.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2016-2021 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.client.sn; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for TurSNSortField. + * + * @author Alexandre Oliveira + * @since 0.3.4 + */ +class TurSNSortFieldTest { + + @Test + void testDefaultConstructor() { + TurSNSortField sortField = new TurSNSortField(); + + assertThat(sortField.getField()).isNull(); + assertThat(sortField.getSort()).isNull(); + } + + @Test + void testFieldSetterAndGetter() { + TurSNSortField sortField = new TurSNSortField(); + + sortField.setField("title"); + + assertThat(sortField.getField()).isEqualTo("title"); + } + + @Test + void testSortSetterAndGetter() { + TurSNSortField sortField = new TurSNSortField(); + + sortField.setSort(TurSNQuery.ORDER.asc); + + assertThat(sortField.getSort()).isEqualTo(TurSNQuery.ORDER.asc); + } + + @Test + void testCompleteConfiguration() { + TurSNSortField sortField = new TurSNSortField(); + + sortField.setField("publishDate"); + sortField.setSort(TurSNQuery.ORDER.desc); + + assertThat(sortField.getField()).isEqualTo("publishDate"); + assertThat(sortField.getSort()).isEqualTo(TurSNQuery.ORDER.desc); + } + + @Test + void testNullValues() { + TurSNSortField sortField = new TurSNSortField(); + + sortField.setField(null); + sortField.setSort(null); + + assertThat(sortField.getField()).isNull(); + assertThat(sortField.getSort()).isNull(); + } + + @Test + void testFieldUpdate() { + TurSNSortField sortField = new TurSNSortField(); + + // Set initial value + sortField.setField("title"); + assertThat(sortField.getField()).isEqualTo("title"); + + // Update value + sortField.setField("date"); + assertThat(sortField.getField()).isEqualTo("date"); + } + + @Test + void testSortOrderUpdate() { + TurSNSortField sortField = new TurSNSortField(); + + // Set initial order + sortField.setSort(TurSNQuery.ORDER.asc); + assertThat(sortField.getSort()).isEqualTo(TurSNQuery.ORDER.asc); + + // Update order + sortField.setSort(TurSNQuery.ORDER.desc); + assertThat(sortField.getSort()).isEqualTo(TurSNQuery.ORDER.desc); + } + + @Test + void testEmptyStringField() { + TurSNSortField sortField = new TurSNSortField(); + + sortField.setField(""); + + assertThat(sortField.getField()).isEmpty(); + } + + @Test + void testSpecialCharactersInField() { + TurSNSortField sortField = new TurSNSortField(); + + sortField.setField("field_with_underscores"); + assertThat(sortField.getField()).isEqualTo("field_with_underscores"); + + sortField.setField("field-with-dashes"); + assertThat(sortField.getField()).isEqualTo("field-with-dashes"); + + sortField.setField("field.with.dots"); + assertThat(sortField.getField()).isEqualTo("field.with.dots"); + } +} \ No newline at end of file diff --git a/turing-java-sdk/src/test/java/com/viglet/turing/client/utils/TurClientUtilsTest.java b/turing-java-sdk/src/test/java/com/viglet/turing/client/utils/TurClientUtilsTest.java new file mode 100644 index 00000000000..94e895095ec --- /dev/null +++ b/turing-java-sdk/src/test/java/com/viglet/turing/client/utils/TurClientUtilsTest.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2016-2022 the original author or authors. + * + * 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. + */ + +package com.viglet.turing.client.utils; + +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Unit tests for TurClientUtils. + * + * @author Alexandre Oliveira + * @since 0.3.9 + */ +class TurClientUtilsTest { + + @Test + void testConstructorThrowsException() throws Exception { + // Use reflection to test private constructor + assertThrows(IllegalStateException.class, () -> { + try { + var constructor = TurClientUtils.class.getDeclaredConstructor(); + constructor.setAccessible(true); + constructor.newInstance(); + } catch (Exception e) { + if (e.getCause() instanceof IllegalStateException) { + throw (IllegalStateException) e.getCause(); + } + throw new RuntimeException(e); + } + }); + } + + @Test + void testAuthenticationWithValidApiKey() { + HttpPost httpPost = new HttpPost("http://example.com/api"); + String apiKey = "test-api-key-123"; + + TurClientUtils.authentication(httpPost, apiKey); + + assertThat(httpPost.getFirstHeader("Key")).isNotNull(); + assertThat(httpPost.getFirstHeader("Key").getValue()).isEqualTo(apiKey); + } + + @Test + void testAuthenticationWithNullApiKey() { + HttpPost httpPost = new HttpPost("http://example.com/api"); + + TurClientUtils.authentication(httpPost, null); + + // Should not add the Key header when apiKey is null + assertThat(httpPost.getFirstHeader("Key")).isNull(); + } + + @Test + void testAuthenticationWithEmptyApiKey() { + HttpPost httpPost = new HttpPost("http://example.com/api"); + String apiKey = ""; + + TurClientUtils.authentication(httpPost, apiKey); + + // Should add header even with empty string (non-null) + assertThat(httpPost.getFirstHeader("Key")).isNotNull(); + assertThat(httpPost.getFirstHeader("Key").getValue()).isEmpty(); + } + + @Test + void testAuthenticationOverwritesExistingHeader() { + HttpPost httpPost = new HttpPost("http://example.com/api"); + + // Set initial header + httpPost.setHeader("Key", "old-api-key"); + + // Overwrite with new key + String newApiKey = "new-api-key-456"; + TurClientUtils.authentication(httpPost, newApiKey); + + assertThat(httpPost.getFirstHeader("Key")).isNotNull(); + assertThat(httpPost.getFirstHeader("Key").getValue()).isEqualTo(newApiKey); + + // Should only have one Key header + assertThat(httpPost.getHeaders("Key")).hasSize(1); + } + + @Test + void testAuthenticationWithSpecialCharacters() { + HttpPost httpPost = new HttpPost("http://example.com/api"); + String specialApiKey = "api-key-with-special-chars!@#$%^&*()_+"; + + TurClientUtils.authentication(httpPost, specialApiKey); + + assertThat(httpPost.getFirstHeader("Key")).isNotNull(); + assertThat(httpPost.getFirstHeader("Key").getValue()).isEqualTo(specialApiKey); + } + + @Test + void testAuthenticationDoesNotAffectOtherHeaders() { + HttpPost httpPost = new HttpPost("http://example.com/api"); + + // Set other headers + httpPost.setHeader("Content-Type", "application/json"); + httpPost.setHeader("Accept", "application/json"); + + // Add authentication + String apiKey = "test-key"; + TurClientUtils.authentication(httpPost, apiKey); + + // Verify authentication header is added + assertThat(httpPost.getFirstHeader("Key")).isNotNull(); + assertThat(httpPost.getFirstHeader("Key").getValue()).isEqualTo(apiKey); + + // Verify other headers remain unchanged + assertThat(httpPost.getFirstHeader("Content-Type").getValue()).isEqualTo("application/json"); + assertThat(httpPost.getFirstHeader("Accept").getValue()).isEqualTo("application/json"); + } + + @Test + void testAuthenticationWithLongApiKey() { + HttpPost httpPost = new HttpPost("http://example.com/api"); + + // Create a very long API key + String longApiKey = "a".repeat(1000); + + TurClientUtils.authentication(httpPost, longApiKey); + + assertThat(httpPost.getFirstHeader("Key")).isNotNull(); + assertThat(httpPost.getFirstHeader("Key").getValue()).isEqualTo(longApiKey); + assertThat(httpPost.getFirstHeader("Key").getValue()).hasSize(1000); + } +} \ No newline at end of file