Skip to content

Commit

Permalink
Refactored load dictionary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Jun 14, 2024
1 parent e7029ac commit 9078577
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 Exactpro (Exactpro Systems Limited)
* Copyright 2020-2024 Exactpro (Exactpro Systems Limited)
* 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
Expand Down Expand Up @@ -61,6 +61,7 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -465,10 +466,23 @@ public Set<String> getDictionaryAliases() {
}

try (Stream<Path> files = Files.list(dictionaryFolder)) {
return files
List<Path> fileList = files
.filter(Files::isRegularFile)
.map(dictionary -> FilenameUtils.removeExtension(dictionary.getFileName().toString()))
.collect(Collectors.toSet());
.collect(Collectors.toList());

Set<String> aliasSet = new HashSet<>();
Map<String, Set<String>> duplicates = new HashMap<>();
for (Path path : fileList) {
String alias = FilenameUtils.removeExtension(path.getFileName().toString()).toLowerCase();
if (!aliasSet.add(alias)) {
duplicates.computeIfAbsent(alias, (ignore) -> new HashSet<>())
.add(path.getFileName().toString());
}
}
if (!duplicates.isEmpty()) {
throw new IllegalStateException("Dictionary directory contains files with the same name in different cases, files: " + duplicates + ", path: " + dictionaryFolder.toAbsolutePath());
}
return aliasSet;
}
} catch (IOException e) {
throw new IllegalStateException("Can not get dictionaries aliases from path: " + dictionaryFolder.toAbsolutePath(), e);
Expand Down
246 changes: 205 additions & 41 deletions src/test/kotlin/com/exactpro/th2/common/schema/TestDictionaryLoad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,85 +17,249 @@ package com.exactpro.th2.common.schema

import com.exactpro.th2.common.schema.dictionary.DictionaryType
import com.exactpro.th2.common.schema.factory.CommonFactory
import com.exactpro.th2.common.schema.factory.FactorySettings
import com.exactpro.th2.common.schema.util.ArchiveUtils
import org.apache.commons.lang3.RandomStringUtils
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.nio.file.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.createDirectories
import kotlin.io.path.writeBytes
import kotlin.io.path.writeText
import kotlin.test.assertEquals

class TestDictionaryLoad {

@Test
fun `test file load dictionary`() {
val factory = CommonFactory.createFromArguments("-c", "src/test/resources/test_load_dictionaries")
@TempDir
lateinit var tempDir: Path

@BeforeEach
fun beforeEach() {
writePrometheus(tempDir)
}

factory.readDictionary().use {
assert(String(it.readAllBytes()) == "test file")
//--//--//--readDictionary()--//--//--//

@Test
fun `test read dictionary from old dictionary dir`() {
val content = writeDictionary(tempDir.resolve(Path.of("MAIN")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(content, commonFactory.readDictionary().use { String(it.readAllBytes()) })
}
}

@Test
fun `test folder load dictionary`() {
val factory = CommonFactory.createFromArguments("-c", "src/test/resources/test_load_dictionaries")
fun `test read dictionary from old dictionary dir - file name mismatch`() {
writeDictionary(tempDir.resolve(Path.of("main")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.readDictionary()
}
}
}

@ParameterizedTest
@ValueSource(strings = ["MAIN", "main", "test-dictionary"])
fun `test read dictionary from type dictionary dir`(fileName: String, ) {
val content = writeDictionary(tempDir.resolve(Path.of("dictionary", "main", fileName)))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(content, commonFactory.readDictionary().use { String(it.readAllBytes()) })
}
}

@ParameterizedTest
@ValueSource(strings = ["MAIN", "main", "test-dictionary"])
fun `test read dictionary from type dictionary dir - dictionary name mismatch`(
fileName: String,

) {
writeDictionary(tempDir.resolve(Path.of("dictionary", "MAIN", fileName)))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.readDictionary()
}
}
}

factory.readDictionary(DictionaryType.LEVEL1).use {
assert(String(it.readAllBytes()) == "test file")
@ParameterizedTest
@ValueSource(strings = ["MAIN", "main", "MAIN.xml", "main.json"])
fun `test read dictionary from alias dictionary dir`(fileName: String, ) {
val content = writeDictionary(tempDir.resolve(Path.of("dictionaries", fileName)))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(content, commonFactory.readDictionary().use { String(it.readAllBytes()) })
}
}

//--//--//--readDictionary(<type>)--//--//--//

@Test
fun `test folder load dictionaries by alias`() {
val factory = CommonFactory.createFromArguments("-c", "src/test/resources/test_load_dictionaries")
fun `test read dictionary by type from old dictionary dir`() {
val content = writeDictionary(tempDir.resolve(Path.of("INCOMING")))
writeDictionary(tempDir.resolve(Path.of("MAIN")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(
content,
commonFactory.readDictionary(DictionaryType.INCOMING).use { String(it.readAllBytes()) })
}
}

Assertions.assertDoesNotThrow {
factory.loadDictionary("test_alias_2").use {
assert(String(it.readAllBytes()) == "test file")
@Test
fun `test read dictionary by type from old dictionary dir - file name mismatch`() {
writeDictionary(tempDir.resolve(Path.of("incoming")))
writeDictionary(tempDir.resolve(Path.of("MAIN")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.readDictionary(DictionaryType.INCOMING)
}
}
}

@Test
fun `test folder load all dictionary aliases`() {
val factory = CommonFactory.createFromArguments("-c", "src/test/resources/test_load_dictionaries")
val expectedNames = listOf("main", "test_alias_1", "test_alias_2", "test_alias_3", "test_alias_4")
val names = factory.dictionaryAliases
Assertions.assertEquals(5, names.size)
Assertions.assertTrue(names.containsAll(expectedNames))
@ParameterizedTest
@ValueSource(strings = ["INCOMING", "incoming", "test-dictionary"])
fun `test read dictionary by type from type dictionary dir`(fileName: String, ) {
val content = writeDictionary(tempDir.resolve(Path.of("dictionary", "incoming", fileName)))
writeDictionary(tempDir.resolve(Path.of("dictionary", "main", "MAIN")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(
content,
commonFactory.readDictionary(DictionaryType.INCOMING).use { String(it.readAllBytes()) })
}
}

@ParameterizedTest
@ValueSource(strings = ["INCOMING", "incoming", "test-dictionary"])
fun `test read dictionary by type from type dictionary dir - dictionary name mismatch`(
fileName: String,

) {
writeDictionary(tempDir.resolve(Path.of("dictionary", "INCOMING", fileName)))
writeDictionary(tempDir.resolve(Path.of("dictionary", "main", "MAIN")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.readDictionary(DictionaryType.INCOMING)
}
}
}

@ParameterizedTest
@ValueSource(strings = ["INCOMING", "incoming", "INCOMING.xml", "incoming.json"])
fun `test read dictionary by type from alias dictionary dir`(fileName: String, ) {
val content = writeDictionary(tempDir.resolve(Path.of("dictionaries", fileName)))
writeDictionary(tempDir.resolve(Path.of("dictionaries", "MAIN")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(
content,
commonFactory.readDictionary(DictionaryType.INCOMING).use { String(it.readAllBytes()) })
}
}

//--//--//--loadSingleDictionary()--//--//--//

@Test
fun `test folder load single dictionary from folder with several`() {
val factory = CommonFactory.createFromArguments("-c", "src/test/resources/test_load_dictionaries")
fun `test load single dictionary from alias dictionary dir`() {
val content = writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(content, commonFactory.loadSingleDictionary().use { String(it.readAllBytes()) })
}
}

Assertions.assertThrows(IllegalStateException::class.java) {
factory.loadSingleDictionary()
@Test
fun `test load single dictionary from alias dictionary dir - several dictionaries`() {
writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary-1")))
writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary-2")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.loadSingleDictionary()
}
}
}

@Test
fun `test folder load single dictionary`() {
val customSettings = FactorySettings().apply {
prometheus = Path.of("src/test/resources/test_load_dictionaries/prometheus.json")
dictionaryAliasesDir = Path.of("src/test/resources/test_load_dictionaries/single_dictionary")
fun `test load single dictionary from alias dictionary dir - several dictionaries with name in different case`() {
writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary")))
writeDictionary(tempDir.resolve(Path.of("dictionaries", "TEST-DICTIONARY")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.loadSingleDictionary()
}
}
}

//--//--//--loadDictionary(<alias>)--//--//--//

@ParameterizedTest
@ValueSource(strings = ["TEST-ALIAS", "test-alias"])
fun `test load dictionary by alias from alias dictionary dir`(fileName: String, ) {
val content = writeDictionary(tempDir.resolve(Path.of("dictionaries", fileName)))
writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(content, commonFactory.loadDictionary("test-alias").use { String(it.readAllBytes()) })
}
}

@ParameterizedTest
@ValueSource(strings = ["TEST-DICTIONARY", "test-dictionary"])
fun `test load dictionary by alias from alias dictionary dir - several dictionaries with name in different case`(
alias: String,

) {
writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary")))
writeDictionary(tempDir.resolve(Path.of("dictionaries", "TEST-DICTIONARY")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.loadDictionary(alias)
}
}
val customFactory = CommonFactory(customSettings)
}

//--//--//--loadAliases--//--//--//

customFactory.loadSingleDictionary().use {
assert(String(it.readAllBytes()) == "test file")
@Test
fun `test dictionary aliases from alias dictionary dir`() {
val alias1 = "test-dictionary-1"
val alias2 = "TEST-DICTIONARY-2"
val file1 = "$alias1.xml"
val file2 = "$alias2.json"
writeDictionary(tempDir.resolve(Path.of("dictionaries", file1)))
writeDictionary(tempDir.resolve(Path.of("dictionaries", file2)))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(setOf(alias1, alias2.lowercase()), commonFactory.dictionaryAliases)
}
}

@Test
fun `test folder load single dictionary by type as alias`() {
val customSettings = FactorySettings().apply {
prometheus = Path.of("src/test/resources/test_load_dictionaries/prometheus.json")
dictionaryTypesDir = Path.of("..")
dictionaryAliasesDir = Path.of("src/test/resources/test_load_dictionaries/dictionaries")
fun `test dictionary aliases from alias dictionary dir - empty directory`() {
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
assertEquals(emptySet(), commonFactory.dictionaryAliases)
}
val customFactory = CommonFactory(customSettings)
}

customFactory.readDictionary().use {
assert(String(it.readAllBytes()) == "test file")
@Test
fun `test dictionary aliases from alias dictionary dir - several dictionaries with name in different case`() {
writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary")))
writeDictionary(tempDir.resolve(Path.of("dictionaries", "TEST-DICTIONARY")))
CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory ->
Assertions.assertThrows(IllegalStateException::class.java) {
commonFactory.dictionaryAliases
}
}
}

//--//--//--Others--//--//--//

private fun writePrometheus(cfgPath: Path) {
cfgPath.resolve("prometheus.json").writeText("{\"enabled\":false}")
}

private fun writeDictionary(path: Path): String? {
val content = RandomStringUtils.randomAlphanumeric(10)
path.parent.createDirectories()
path.writeBytes(ArchiveUtils.getGzipBase64StringEncoder().encode(content))
return content
}

}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/resources/test_load_dictionaries/prometheus.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 9078577

Please sign in to comment.