Created
May 1, 2019 13:29
-
-
Save tbelmega/007b116ee7f55d80cb7a261b24a25fa2 to your computer and use it in GitHub Desktop.
ProductE2EContainerlessTest
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
class ProductE2EContainerlessTest { | |
private lateinit var flyway: Flyway | |
private lateinit var entityManager: EntityManager | |
private lateinit var repository: ProductRepository | |
private val authService = mock(AuthService::class.java) | |
private val auditLoggerClient = mock(AuditLoggerClient::class.java) | |
private lateinit var client: MockMvc | |
@Before | |
fun migrate() { | |
flyway = Flyway.configure() | |
.dataSource("jdbc:h2:mem:test/pcshop_test_db;DB_CLOSE_DELAY=-1", null, null) | |
.load() | |
flyway.migrate() | |
val emFactory = Persistence.createEntityManagerFactory("TestDS") | |
entityManager = emFactory.createEntityManager() | |
repository = JpaRepositoryFactory(entityManager).getRepository(ProductRepository::class.java) | |
val controller = ProductController(repository, auditLoggerClient, authService) | |
this.client = MockMvcBuilders.standaloneSetup(controller).build() | |
} | |
@After | |
fun clean() { | |
flyway.clean() | |
} | |
@Test | |
fun `create new product - success - return 201 CREATED`() { | |
//given | |
given(authService.isProductAdmin()).willReturn(true) | |
val createProductRequest = CreateProductRequest("ROG Strix Z390-F Gaming Mainboard", "ASUS") | |
// when | |
client.perform( | |
post("/api/v1/products") | |
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE) | |
.content(createProductRequest.toJson())) | |
//then | |
.andExpect(status().`is`(201)) | |
//then | |
assertThat(repository.findByProductName(createProductRequest.productName)).hasSize(1) | |
verify(auditLoggerClient).logProductCreated(createProductRequest) | |
} | |
} | |
fun Any.toJson(): String { | |
return ObjectMapper().writeValueAsString(this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment