Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djvelimir committed Jul 15, 2023
1 parent 42a1acb commit fd47f3f
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,20 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<version>5.6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>1.12.4</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
2 changes: 1 addition & 1 deletion src/main/kotlin/processor/ArgumentProcessorImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ArgumentProcessorImpl(
private val terminal: Terminal
) : ArgumentProcessor {
override fun process(args: Array<String>) {
if (!argumentValidator.validate(args!!)) {
if (!argumentValidator.validate(args)) {
val usage = "Usage:" + System.getProperty("line.separator") +
"java -jar ./demo-kotlin-cli.jar generate password"
terminal.show(usage)
Expand Down
25 changes: 25 additions & 0 deletions src/test/kotlin/display/TerminalImplTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package display

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.io.ByteArrayOutputStream
import java.io.PrintStream

class TerminalImplTest {
private val sut: Terminal = TerminalImpl()

@Test
fun show() {
val message = "Test Message"
val expected = message + System.getProperty("line.separator")

val standardOut = System.out
val outputStreamCaptor = ByteArrayOutputStream()

System.setOut(PrintStream(outputStreamCaptor))
sut.show(message)
System.setOut(standardOut)

assertEquals(expected, outputStreamCaptor.toString())
}
}
66 changes: 66 additions & 0 deletions src/test/kotlin/generator/PasswordGeneratorImplTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package generator

import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
import shuffler.StringShuffler

class PasswordGeneratorImplTest {
private val randomCharacterGenerator: RandomCharacterGenerator = mockk<RandomCharacterGenerator>(relaxed = true)
private val stringShuffler: StringShuffler = mockk<StringShuffler>(relaxed = true)
private val sut: PasswordGenerator = PasswordGeneratorImpl(randomCharacterGenerator, stringShuffler)

@Test
fun generateShouldCallGenerateUppercaseCharacter() {
sut.generate()

verify(exactly = 1) {
randomCharacterGenerator.generateUppercaseCharacter()
}
}

@Test
fun generateShouldCallGenerateLowercaseCharacter() {
sut.generate()

verify(exactly = 1) {
randomCharacterGenerator.generateLowercaseCharacter()
}
}

@Test
fun generateShouldCallGenerateDigitCharacter() {
sut.generate()

verify(exactly = 1) {
randomCharacterGenerator.generateDigitCharacter()
}
}

@Test
fun generateShouldCallGenerateSpecialCharacter() {
sut.generate()

verify(exactly = 1) {
randomCharacterGenerator.generateSpecialCharacter()
}
}

@Test
fun generateShouldCallGenerateAllowedCharacter12Times() {
sut.generate()

verify(exactly = 12) {
randomCharacterGenerator.generateAllowedCharacter()
}
}

@Test
fun generateShouldCallShuffle() {
sut.generate()

verify(exactly = 1) {
stringShuffler.shuffle(any())
}
}
}
42 changes: 42 additions & 0 deletions src/test/kotlin/generator/RandomCharacterGeneratorImplTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package generator

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class RandomCharacterGeneratorImplTest {
private val uppercaseCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
private val lowercaseCharacters = "abcdefghijklmnopqrstuvwxyz"
private val digitCharacters = "0123456789"
private val specialCharacters = "~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?"
private val allowedCharacters = uppercaseCharacters
.plus(lowercaseCharacters)
.plus(digitCharacters)
.plus(specialCharacters)

private val sut: RandomCharacterGenerator = RandomCharacterGeneratorImpl()

@Test
fun generateUppercaseCharacter() {
assertTrue(uppercaseCharacters.contains(sut.generateUppercaseCharacter()))
}

@Test
fun generateLowercaseCharacter() {
assertTrue(lowercaseCharacters.contains(sut.generateLowercaseCharacter()))
}

@Test
fun generateDigitCharacter() {
assertTrue(digitCharacters.contains(sut.generateDigitCharacter()))
}

@Test
fun generateSpecialCharacter() {
assertTrue(specialCharacters.contains(sut.generateSpecialCharacter()))
}

@Test
fun generateAllowedCharacter() {
assertTrue(allowedCharacters.contains(sut.generateAllowedCharacter()))
}
}
48 changes: 48 additions & 0 deletions src/test/kotlin/processor/ArgumentProcessorImplTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package processor

import display.Terminal
import generator.PasswordGenerator
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
import validator.ArgumentValidator

class ArgumentProcessorImplTest {
private val argumentValidator: ArgumentValidator = mockk<ArgumentValidator>()
private val passwordGenerator: PasswordGenerator = mockk<PasswordGenerator>()
private val terminal: Terminal = mockk<Terminal>(relaxed = true)
private val sut: ArgumentProcessor = ArgumentProcessorImpl(argumentValidator, passwordGenerator, terminal)

@Test
fun processGeneratePassword() {
val args = arrayOf("generate", "password")
every { argumentValidator.validate(args) } returns true

val sample = "A8!(,wV5YuI[Vr^>"
every { passwordGenerator.generate() } returns sample

sut.process(args)

verify(exactly = 1) {
argumentValidator.validate(args)
passwordGenerator.generate()
terminal.show(sample)
}
}

@Test
fun processInvalidArguments() {
val usage = "Usage:" + System.getProperty("line.separator") +
"java -jar ./demo-kotlin-cli.jar generate password"
val args = arrayOf<String>()
every { argumentValidator.validate(args) } returns false

sut.process(args)

verify(exactly = 1) {
argumentValidator.validate(args)
terminal.show(usage)
}
}
}
21 changes: 21 additions & 0 deletions src/test/kotlin/shuffler/StringShufflerImplTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package shuffler

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class StringShufflerImplTest {
private val sut: StringShuffler = StringShufflerImpl()

@Test
fun shuffle() {
val sample = "A8!(,wV5YuI[Vr^>"
val actual: String = sut.shuffle(sample)

assertNotEquals(sample, actual)
assertEquals(sample.length, actual.length)

for (item in sample.toCharArray()) {
assertTrue(actual.contains(item.toString()))
}
}
}
19 changes: 19 additions & 0 deletions src/test/kotlin/validator/ArgumentValidatorImplTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package validator

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class ArgumentValidatorImplTest {
private val sut: ArgumentValidator = ArgumentValidatorImpl()

@Test
fun validateCheckForGeneratePassword() {
assertTrue(sut.validate(arrayOf("generate", "password")))
}

@Test
fun validateCheckForInvalidArguments() {
assertFalse(sut.validate(emptyArray()))
}
}

0 comments on commit fd47f3f

Please sign in to comment.