From 5dab80d6ba4ceac232c83e98527af4825a05273e Mon Sep 17 00:00:00 2001 From: BrunoRosendo Date: Tue, 1 Aug 2023 00:39:51 +0100 Subject: [PATCH] Refactored account test mocks --- .../controller/AccountControllerTest.kt | 77 ++++++++----------- .../controller/GenerationControllerTest.kt | 8 +- .../ni/website/backend/model/AccountTest.kt | 2 +- .../payloadschemas/model/PayloadActivity.kt | 7 +- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt index e168f57f..203be488 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AccountControllerTest.kt @@ -1,11 +1,10 @@ package pt.up.fe.ni.website.backend.controller +import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName import com.fasterxml.jackson.databind.ObjectMapper -import java.util.Calendar -import java.util.Date -import java.util.UUID -import org.junit.jupiter.api.AfterEach +import java.util.* +import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeEach @@ -29,7 +28,6 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import pt.up.fe.ni.website.backend.config.upload.UploadConfigProperties import pt.up.fe.ni.website.backend.model.Account import pt.up.fe.ni.website.backend.model.CustomWebsite -import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants import pt.up.fe.ni.website.backend.repository.AccountRepository import pt.up.fe.ni.website.backend.utils.TestUtils import pt.up.fe.ni.website.backend.utils.ValidationTester @@ -169,9 +167,17 @@ class AccountControllerTest @Autowired constructor( @NestedTest @DisplayName("POST /accounts/new") inner class CreateAccount { - @AfterEach - fun clearAccounts() { - repository.deleteAll() + private val uuid: UUID = UUID.randomUUID() + private val mockedSettings = Mockito.mockStatic(UUID::class.java) + + @BeforeAll + fun setupMocks() { + Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) + } + + @AfterAll + fun cleanup() { + mockedSettings.close() } @Test @@ -238,10 +244,6 @@ class AccountControllerTest @Autowired constructor( @Test fun `should create the account with valid image`() { - val uuid: UUID = UUID.randomUUID() - val mockedSettings = Mockito.mockStatic(UUID::class.java) - Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) - val expectedPhotoPath = "${uploadConfigProperties.staticServe}/profile/${testAccount.email}-$uuid.jpeg" mockMvc.multipartBuilder("/accounts/new") @@ -268,16 +270,10 @@ class AccountControllerTest @Autowired constructor( // "This endpoint operation creates a new account.", // documentRequestPayload = true // ) - - mockedSettings.close() } @Test fun `should fail to create account with invalid filename extension`() { - val uuid: UUID = UUID.randomUUID() - val mockedSettings = Mockito.mockStatic(UUID::class.java) - Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) - mockMvc.multipartBuilder("/accounts/new") .addPart("account", testAccount.toJson()) .addFile(filename = "photo.pdf") @@ -290,16 +286,10 @@ class AccountControllerTest @Autowired constructor( jsonPath("$.errors[0].param").value("createAccount.photo") ) .andDocumentErrorResponse(documentation, hasRequestPayload = true) - - mockedSettings.close() } @Test fun `should fail to create account with invalid filename media type`() { - val uuid: UUID = UUID.randomUUID() - val mockedSettings = Mockito.mockStatic(UUID::class.java) - Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) - mockMvc.multipartBuilder("/accounts/new") .addPart("account", testAccount.toJson()) .addFile(contentType = MediaType.APPLICATION_PDF_VALUE) @@ -311,8 +301,6 @@ class AccountControllerTest @Autowired constructor( jsonPath("$.errors[0].message").value("invalid image type (png, jpg or jpeg)"), jsonPath("$.errors[0].param").value("createAccount.photo") ) - - mockedSettings.close() } @Test @@ -714,12 +702,25 @@ class AccountControllerTest @Autowired constructor( ) ) + private val uuid: UUID = UUID.randomUUID() + private val mockedSettings = Mockito.mockStatic(UUID::class.java) + @BeforeEach fun addAccounts() { repository.save(testAccount) repository.save(newAccount) } + @BeforeAll + fun setupMocks() { + Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) + } + + @AfterAll + fun cleanup() { + mockedSettings.close() + } + private val parameters = listOf(parameterWithName("id").description("ID of the account to update")) private val newName = "Test Account 2" @@ -766,7 +767,7 @@ class AccountControllerTest @Autowired constructor( // .andDocument( // documentationNoPassword, // "Update accounts", -// "Update a previously created account, with the exception of the password, using its ID (no image).", +// "Update a previously created account, except the password, using its ID (no image).", // urlParameters = parameters, // documentRequestPayload = true // ) @@ -783,10 +784,6 @@ class AccountControllerTest @Autowired constructor( @Test fun `should update the account with valid image`() { - val uuid: UUID = UUID.randomUUID() - val mockedSettings = Mockito.mockStatic(UUID::class.java) - Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) - val expectedPhotoPath = "${uploadConfigProperties.staticServe}/profile/$newEmail-$uuid.jpeg" mockMvc.multipartBuilder("/accounts/${testAccount.id}") @@ -811,7 +808,7 @@ class AccountControllerTest @Autowired constructor( // .andDocument( // documentationNoPassword, // "Update accounts", -// "Update a previously created account, with the exception of the password, using its ID.", +// "Update a previously created account, except the password, using its ID.", // urlParameters = parameters, // documentRequestPayload = true // ) @@ -825,20 +822,14 @@ class AccountControllerTest @Autowired constructor( Assertions.assertEquals(newLinkedin, updatedAccount.linkedin) Assertions.assertEquals(newWebsites[0].url, updatedAccount.websites[0].url) Assertions.assertEquals(newWebsites[0].iconPath, updatedAccount.websites[0].iconPath) - - mockedSettings.close() } @Test fun `should fail to update account with invalid filename extension`() { - val uuid: UUID = UUID.randomUUID() - val mockedSettings = Mockito.mockStatic(UUID::class.java) - Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) - mockMvc.multipartBuilder("/accounts/${testAccount.id}") .asPutMethod() .addPart("account", data) - .addFile(filename = "photo.pdf", contentType = MediaType.APPLICATION_PDF_VALUE) + .addFile(filename = "photo.pdf") .perform() .andExpectAll( status().isBadRequest, @@ -848,16 +839,10 @@ class AccountControllerTest @Autowired constructor( jsonPath("$.errors[0].param").value("updateAccountById.photo") ) .andDocumentErrorResponse(documentation, hasRequestPayload = true) - - mockedSettings.close() } @Test fun `should fail to update account with invalid filename media type`() { - val uuid: UUID = UUID.randomUUID() - val mockedSettings = Mockito.mockStatic(UUID::class.java) - Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) - mockMvc.multipartBuilder("/accounts/${testAccount.id}") .asPutMethod() .addPart("account", data) @@ -871,8 +856,6 @@ class AccountControllerTest @Autowired constructor( jsonPath("$.errors[0].param").value("updateAccountById.photo") ) .andDocumentErrorResponse(documentation, hasRequestPayload = true) - - mockedSettings.close() } @Test diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/GenerationControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/GenerationControllerTest.kt index 843aeb07..df2b7c3f 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/GenerationControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/GenerationControllerTest.kt @@ -1055,7 +1055,13 @@ class GenerationControllerTest @Autowired constructor( listOf(testAccount), listOf( buildTestPerActivityRole( - Project("NIJobs", "cool project", image = "cool-image.png", targetAudience = "students", github = "https://github.com/NIAEFEUP/nijobs-be") + Project( + "NIJobs", + "cool project", + image = "cool-image.png", + targetAudience = "students", + github = "https://github.com/NIAEFEUP/nijobs-be" + ) ) ) ), diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/model/AccountTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/model/AccountTest.kt index 57abffed..dd2967d7 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/model/AccountTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/model/AccountTest.kt @@ -33,7 +33,7 @@ class AccountTest { "NI Website", "NI's website is where everything about NI is shown to the public", image = "cool-image.jpg", - targetAudience = "Everyone", + targetAudience = "Everyone" ) diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/utils/documentation/payloadschemas/model/PayloadActivity.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/utils/documentation/payloadschemas/model/PayloadActivity.kt index 2a60c3db..5ef11c31 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/utils/documentation/payloadschemas/model/PayloadActivity.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/utils/documentation/payloadschemas/model/PayloadActivity.kt @@ -38,9 +38,14 @@ class PayloadActivity { "Information about the target audience", JsonFieldType.STRING ), + DocumentedJSONField( + "slogan", + "Slogan of the activity", + JsonFieldType.STRING + ), DocumentedJSONField( "github", - "Handle/link to the project's GitHub repository", + "Handle/link to the activity's GitHub repository", JsonFieldType.STRING ), DocumentedJSONField(