-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/test/kotlin/generator/RandomCharacterGeneratorImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
} |