-
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
4 changed files
with
124 additions
and
135 deletions.
There are no files selected for viewing
69 changes: 0 additions & 69 deletions
69
src/test/java/com/lucasia/ginquiry/controller/GinControllerTest.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
src/test/java/com/lucasia/ginquiry/controller/GinControllerTest.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,64 @@ | ||
package com.lucasia.ginquiry.controller; | ||
|
||
import com.lucasia.ginquiry.dao.BoozeRepository | ||
import com.lucasia.ginquiry.dao.BrandRepository | ||
import com.lucasia.ginquiry.domain.Booze | ||
import com.lucasia.ginquiry.domain.Brand | ||
import com.lucasia.ginquiry.domain.DomainFactory | ||
import com.lucasia.ginquiry.service.BoozeService | ||
import lombok.extern.log4j.Log4j2 | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.security.test.context.support.WithMockUser | ||
import org.springframework.web.util.NestedServletException | ||
import java.util.* | ||
|
||
|
||
@WebMvcTest(GinCrudController::class) // run without the server | ||
@Log4j2 | ||
class GinControllerTest() : AbstractCrudControllerTest<Booze>(DomainFactory.BoozeDomainFactory(Brand(UUID.randomUUID().toString())), GinCrudController.GIN_PATH) { | ||
|
||
@MockBean | ||
private val brandRepository : BrandRepository? = null | ||
|
||
@MockBean | ||
private val boozeRepository : BoozeRepository? = null | ||
|
||
@MockBean | ||
private val boozeService : BoozeService? = null | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
Assertions.assertNotNull(boozeService); | ||
} | ||
|
||
|
||
@Test | ||
@WithMockUser(GUEST_USER) | ||
override fun testNewSucceeds() { | ||
val brand = Brand(UUID.randomUUID().toString()) | ||
|
||
Mockito.`when`(brandRepository?.save(brand)).thenReturn(brand) | ||
|
||
testAddNewSucceeds(Booze(brand, UUID.randomUUID().toString(), UUID.randomUUID().toString())); | ||
} | ||
|
||
@Test | ||
@WithMockUser(GUEST_USER) | ||
fun testNewBoozeWithMissingBrandFails() { | ||
Mockito.`when`(brandRepository?.save(null)).thenThrow(NullPointerException()); | ||
|
||
// TODO: change to NPE or a named Exception | ||
val ex: Exception = Assertions.assertThrows(NestedServletException::class.java) { saveEntity(Booze()) } | ||
|
||
Assertions.assertTrue(ex.message!!.contains("brand is marked non-null but is null")) | ||
} | ||
|
||
override fun getRepository(): BoozeRepository? { | ||
return boozeRepository; | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
src/test/java/com/lucasia/ginquiry/controller/UserControllerTest.java
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
src/test/java/com/lucasia/ginquiry/controller/UserControllerTest.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,60 @@ | ||
package com.lucasia.ginquiry.controller; | ||
|
||
import org.hamcrest.Matchers | ||
import org.hamcrest.text.IsEmptyString | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
import org.springframework.security.test.context.support.WithMockUser | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.ResultActions | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import java.util.* | ||
|
||
|
||
@WebMvcTest(UserController::class) // run without the server | ||
class UserControllerTest { | ||
|
||
@Autowired | ||
private val mockMvc: MockMvc? = null | ||
|
||
private val principalPath: String = UserController.PRINCIPAL_PATH | ||
|
||
@Test | ||
fun testPrincipalSuccessWhenNotLoggedIn() { | ||
val resultActions: ResultActions = mockMvc!!.perform(get(principalPath)) | ||
|
||
resultActions.andDo( | ||
print()) | ||
.andExpect(status().isOk) | ||
.andExpect(content().string(IsEmptyString.emptyString())) | ||
} | ||
|
||
@Test | ||
@WithMockUser("guest") | ||
fun testPrincipalSuccessAndUsernameAvailableWhenLoggedIn() { | ||
val resultActions: ResultActions = mockMvc!!.perform(get(principalPath)) | ||
|
||
resultActions.andDo( | ||
print()) | ||
.andExpect(status().isOk) | ||
.andExpect(content().string(Matchers.containsString("guest"))) | ||
} | ||
|
||
|
||
@Test | ||
fun testOtherEndpointsReturnUnauthorizedWhenNotLoggedIn() { | ||
|
||
val resultActions: ResultActions = mockMvc!!.perform(get("/" + UUID.randomUUID())) | ||
|
||
resultActions.andDo( | ||
print()) | ||
.andExpect(status().is4xxClientError) | ||
.andExpect(status().isUnauthorized) | ||
.andExpect(content().string(Matchers.containsString(""))) | ||
} | ||
|
||
} |