Skip to content

Commit

Permalink
Refactored CommonFactoryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Jun 17, 2024
1 parent 5185125 commit 0fde596
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 314 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ dependencies {

implementation "io.github.microutils:kotlin-logging:3.0.5"

testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation 'javax.annotation:javax.annotation-api:1.3.2'
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
testImplementation "org.mockito.kotlin:mockito-kotlin:5.2.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,7 @@ public <T> T getCustomConfiguration(Class<T> confClass) {
*
* @return Dictionary as {@link InputStream}
* @throws IllegalStateException if can not read dictionary or found more than one target
* @deprecated please use {@link #loadDictionary(String)}
*/
@Deprecated(since = "6", forRemoval = true)
public abstract InputStream loadSingleDictionary();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@
import java.util.Base64;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -145,12 +143,9 @@ public IConfigurationProvider getConfigurationProvider() {
return configurationProvider;
}

public static CommonFactory createFromProvider(IConfigurationProvider provider) {
FactorySettings settings = new FactorySettings();



return new CommonFactory(settings);
public static CommonFactory createFromProvider(@NotNull IConfigurationProvider configurationProvider,
@NotNull IDictionaryProvider dictionaryProvider) {
return new CommonFactory(configurationProvider, dictionaryProvider);
}

/**
Expand Down Expand Up @@ -593,10 +588,6 @@ private static ConfigurationManager createConfigurationManager(IConfigurationPro
return new ConfigurationManager(configurationProvider, paths);
}

private static Path defaultPathIfNull(Path customPath, Path basePath, String name) {
return customPath == null ? getConfigPath(basePath).resolve(name) : customPath;
}

private static <T> void putIfNotNull(@NotNull Map<T, Path> paths, @NotNull T key, @Nullable Path path) {
requireNonNull(paths, "'Paths' can't be null");
requireNonNull(key, "'Key' can't be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ interface IDictionaryProvider {
fun load(alias: String): InputStream
@Deprecated("Load dictionary by type is deprecated, please use load by alias")
fun load(type: DictionaryType): InputStream
@Deprecated("Load single dictionary is deprecated, please use load by alias")
fun load(): InputStream
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@ import java.io.IOException
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.exists
import java.util.Locale
import kotlin.io.path.isDirectory
import kotlin.io.path.isRegularFile
import kotlin.streams.asSequence

class DictionaryProvider @JvmOverloads constructor(
private val baseDir: Path,
baseDir: Path,
paths: Map<DictionaryKind, Path> = emptyMap()
): IDictionaryProvider {
init {
require(baseDir.exists()) { "Base dir '$baseDir' doesn't exist" }
require(baseDir.isDirectory()) { "Base dir '$baseDir' isn't a dictionary" }
}
) : IDictionaryProvider {

private val directoryPaths = DictionaryKind.createMapping(baseDir)
.plus(paths.mapValues { (_, value) -> value.toAbsolutePath() })
Expand All @@ -61,15 +57,31 @@ class DictionaryProvider @JvmOverloads constructor(

override fun aliases(): Set<String> {
try {
if(!dictionaryAliasPath.isDirectory()) {
if (!dictionaryAliasPath.isDirectory()) {
return emptySet()
}
return Files.walk(dictionaryAliasPath).asSequence()

val fileList = Files.walk(dictionaryAliasPath, 1).asSequence()
.filter(Path::isRegularFile)
.map(::toAlias)
.toSet()
.toList()
val aliasSet: MutableSet<String> = mutableSetOf()
val duplicates: MutableMap<String, MutableSet<String>> = mutableMapOf()
for (path in fileList) {
val alias = FilenameUtils.removeExtension(path.fileName.toString())
.lowercase(Locale.getDefault())
if (!aliasSet.add(alias)) {
duplicates.getOrPut(alias, ::mutableSetOf).add(path.fileName.toString())
}
}
check(duplicates.isEmpty()) {
"Dictionary directory contains files with the same name in different cases, files: $duplicates, path: $dictionaryAliasPath"
}
return aliasSet
} catch (e: IOException) {
throw IllegalStateException("Can not get dictionaries aliases from path: ${dictionaryAliasPath.toAbsolutePath()}", e)
throw IllegalStateException(
"Can not get dictionaries aliases from path: ${dictionaryAliasPath.toAbsolutePath()}",
e
)
}
}

Expand All @@ -88,7 +100,10 @@ class DictionaryProvider @JvmOverloads constructor(

return open(file)
} catch (e: IOException) {
throw IllegalStateException("Can not load dictionary by '$alias' alias from path: ${dictionaryAliasPath.toAbsolutePath()}", e)
throw IllegalStateException(
"Can not load dictionary by '$alias' alias from path: ${dictionaryAliasPath.toAbsolutePath()}",
e
)
}
}

Expand Down Expand Up @@ -117,19 +132,19 @@ class DictionaryProvider @JvmOverloads constructor(
val dirs = listOf(dictionaryAliasPath, dictionaryTypePath)
try {
var files: List<Path> = if (dictionaryAliasPath.isDirectory()) {
emptyList()
} else {
Files.walk(dictionaryAliasPath).asSequence()
Files.walk(dictionaryAliasPath, 1).asSequence()
.filter(Path::isRegularFile)
.toList()
} else {
emptyList()
}

if (files.isEmpty()) {
if (dictionaryTypePath.isDirectory()) {
files = Files.walk(dictionaryTypePath).asSequence()
files = Files.walk(dictionaryTypePath, 1).asSequence()
.filter(Path::isDirectory)
.flatMap { dir ->
Files.walk(dir).asSequence()
Files.walk(dir, 1).asSequence()
.filter(Path::isRegularFile)
}.toList()
}
Expand Down Expand Up @@ -163,33 +178,33 @@ class DictionaryProvider @JvmOverloads constructor(
}

private fun searchInOldDir(name: String): List<Path> {
if (!dictionaryOldPath.isDirectory()) {
return emptyList()
if (dictionaryOldPath.isDirectory()) {
return Files.walk(dictionaryOldPath, 1).asSequence()
.filter(Path::isRegularFile)
.filter { file -> file.fileName.toString().contains(name) }
.toList()
}
return Files.walk(dictionaryOldPath).asSequence()
.filter(Path::isRegularFile)
.filter { file -> file.fileName.toString().contains(name) }
.toList()
return emptyList()
}

private fun searchInTypeDir(type: DictionaryType): List<Path> {
val path = type.getDictionary(dictionaryTypePath)
if (!path.isDirectory()) {
return emptyList()
if (path.isDirectory()) {
return Files.walk(path, 1).asSequence()
.filter(Path::isRegularFile)
.toList()
}
return Files.walk(path).asSequence()
.filter(Path::isRegularFile)
.toList()
return emptyList()
}

private fun searchInAliasDir(alias: String): List<Path> {
if (!dictionaryAliasPath.isDirectory()) {
return emptyList()
if (dictionaryAliasPath.isDirectory()) {
return Files.walk(dictionaryAliasPath, 1).asSequence()
.filter(Path::isRegularFile)
.filter { file -> alias.equals(toAlias(file), true) }
.toList()
}
return Files.walk(dictionaryAliasPath).asSequence()
.filter(Path::isRegularFile)
.filter { file -> alias.equals(toAlias(file), true) }
.toList()
return emptyList()
}

companion object {
Expand All @@ -206,8 +221,6 @@ enum class DictionaryKind(

companion object {
fun createMapping(baseDir: Path): Map<DictionaryKind, Path> {
require(baseDir.exists()) { "Base dir '$baseDir' doesn't exist" }
require(baseDir.isDirectory()) { "Base dir '$baseDir' isn't a dictionary" }
return buildMap {
DictionaryKind.values().forEach {
put(it, baseDir.resolve(it.directoryName).toAbsolutePath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import kotlin.io.path.writeBytes
import kotlin.io.path.writeText
import kotlin.test.assertEquals

class TestDictionaryLoad {
@Suppress("DEPRECATION")
class DictionaryLoadTest {

@TempDir
lateinit var tempDir: Path
Expand Down Expand Up @@ -64,7 +65,7 @@ class TestDictionaryLoad {

@ParameterizedTest
@ValueSource(strings = ["MAIN", "main", "test-dictionary"])
fun `test read dictionary from type dictionary dir`(fileName: String, ) {
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()) })
Expand All @@ -87,7 +88,7 @@ class TestDictionaryLoad {

@ParameterizedTest
@ValueSource(strings = ["MAIN", "main", "MAIN.xml", "main.json"])
fun `test read dictionary from alias dictionary dir`(fileName: String, ) {
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()) })
Expand Down Expand Up @@ -120,7 +121,7 @@ class TestDictionaryLoad {

@ParameterizedTest
@ValueSource(strings = ["INCOMING", "incoming", "test-dictionary"])
fun `test read dictionary by type from type dictionary dir`(fileName: String, ) {
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 ->
Expand All @@ -147,7 +148,7 @@ class TestDictionaryLoad {

@ParameterizedTest
@ValueSource(strings = ["INCOMING", "incoming", "INCOMING.xml", "incoming.json"])
fun `test read dictionary by type from alias dictionary dir`(fileName: String, ) {
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 ->
Expand Down Expand Up @@ -193,7 +194,7 @@ class TestDictionaryLoad {

@ParameterizedTest
@ValueSource(strings = ["TEST-ALIAS", "test-alias"])
fun `test load dictionary by alias from alias dictionary dir`(fileName: String, ) {
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 ->
Expand Down Expand Up @@ -255,7 +256,7 @@ class TestDictionaryLoad {
cfgPath.resolve("prometheus.json").writeText("{\"enabled\":false}")
}

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

0 comments on commit 0fde596

Please sign in to comment.