-
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.
Merge pull request #35 from vitekkor/34-fix-new-tests
#34: fix test
- Loading branch information
Showing
12 changed files
with
228 additions
and
15 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
94 changes: 94 additions & 0 deletions
94
blockchain/src/test/kotlin/com/vitekkor/blockchain/service/GenerationStrategyTest.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,94 @@ | ||
package com.vitekkor.blockchain.service | ||
|
||
import com.ninjasquad.springmockk.SpykBean | ||
import com.vitekkor.blockchain.configuration.properties.GenerationStrategy | ||
import com.vitekkor.blockchain.configuration.properties.GenerationStrategyProperties | ||
import com.vitekkor.blockchain.model.HttpOutgoingMessage | ||
import com.vitekkor.blockchain.util.generateData | ||
import io.mockk.every | ||
import io.mockk.mockkStatic | ||
import io.mockk.unmockkAll | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
import org.awaitility.Awaitility | ||
import org.awaitility.Durations | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.util.ReflectionTestUtils | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
import kotlin.test.assertTrue | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest | ||
internal class GenerationStrategyTest { | ||
@Autowired | ||
private lateinit var mockMvc: MockMvc | ||
|
||
@Autowired | ||
private lateinit var blockGeneratorService: BlockGeneratorService | ||
|
||
@SpykBean | ||
private lateinit var generationStrategyProperties: GenerationStrategyProperties | ||
|
||
@Test | ||
fun fibonacciStrategyTest() { | ||
every { generationStrategyProperties.generationStrategyName } returns GenerationStrategy.FIBONACCI | ||
mockkStatic(::generateData) | ||
every { generateData() } returns "Мимо тещиного дома я без шуток не хожу" | ||
ReflectionTestUtils.setField(blockGeneratorService, "lastNonce", 14000L) | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/start")) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
|
||
lateinit var result: HttpOutgoingMessage.BlockChainMessage | ||
|
||
Awaitility.await().atMost(Durations.ONE_MINUTE).untilAsserted { | ||
mockMvc.perform(MockMvcRequestBuilders.get("/blockChain")) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andDo { | ||
result = Json.decodeFromString(it.response.contentAsString) | ||
assertTrue(result.blocks.isNotEmpty()) | ||
} | ||
mockMvc.perform(MockMvcRequestBuilders.get("/stop")) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
assertTrue(result.blocks.size == 1) | ||
} | ||
} | ||
|
||
@Test | ||
fun randomStrategyTest() { | ||
every { generationStrategyProperties.generationStrategyName } returns GenerationStrategy.RANDOM | ||
mockkStatic(::generateData) | ||
every { generateData() } returns "Мимо тещиного дома я без шуток не хожу" | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/start")) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
|
||
lateinit var result: HttpOutgoingMessage.BlockChainMessage | ||
|
||
Awaitility.await().atMost(Durations.ONE_MINUTE).untilAsserted { | ||
mockMvc.perform(MockMvcRequestBuilders.get("/blockChain")) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
.andDo { | ||
result = Json.decodeFromString(it.response.contentAsString) | ||
assertTrue(result.blocks.isNotEmpty()) | ||
} | ||
mockMvc.perform(MockMvcRequestBuilders.get("/stop")) | ||
.andExpect(MockMvcResultMatchers.status().isOk) | ||
assertTrue(result.blocks.size == 1) | ||
} | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
@AfterAll | ||
fun unmock() { | ||
unmockkAll() | ||
} | ||
} | ||
} |
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
Oops, something went wrong.