-
Notifications
You must be signed in to change notification settings - Fork 14
Correcting Logic for archive/unarchive collection process #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fc2529c
2ef580d
3abe4e7
35a7405
6f8213c
7a7c7d7
389d225
7181af3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import edu.wgu.osmt.BaseDockerizedTest | |
| import edu.wgu.osmt.HasDatabaseReset | ||
| import edu.wgu.osmt.HasElasticsearchReset | ||
| import edu.wgu.osmt.SpringTest | ||
| import edu.wgu.osmt.api.model.ApiCollectionUpdate | ||
| import edu.wgu.osmt.config.AppConfig | ||
| import edu.wgu.osmt.db.PublishStatus | ||
| import edu.wgu.osmt.jobcode.JobCodeEsRepo | ||
|
|
@@ -13,10 +14,17 @@ import edu.wgu.osmt.richskill.RichSkillEsRepo | |
| import org.assertj.core.api.Assertions | ||
| import org.junit.jupiter.api.BeforeAll | ||
| import org.junit.jupiter.api.Test | ||
| import org.mockito.Mockito | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.security.core.Authentication | ||
| import org.springframework.security.core.GrantedAuthority | ||
| import org.springframework.security.core.context.SecurityContext | ||
| import org.springframework.security.core.context.SecurityContextHolder | ||
| import org.springframework.security.oauth2.core.user.OAuth2UserAuthority | ||
| import org.springframework.security.oauth2.jwt.Jwt | ||
| import org.springframework.test.util.ReflectionTestUtils | ||
| import org.springframework.transaction.annotation.Transactional | ||
| import java.time.LocalDateTime | ||
|
|
||
| @Transactional | ||
| internal class CollectionControllerTest @Autowired constructor( | ||
|
|
@@ -31,6 +39,8 @@ internal class CollectionControllerTest @Autowired constructor( | |
| @Autowired | ||
| lateinit var collectionController: CollectionController | ||
|
|
||
| var authentication: Authentication = Mockito.mock(Authentication::class.java) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Kotlin, we often use mockk. I can't give you a deep reason why, but there are times/places where Mockito is problematic. I'm not asking for a change here, just please be aware.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I know, I just replicated a code that I already had before knowing about mockk, and since Mockito was working for the needed here I just used it here. |
||
|
|
||
| private lateinit var mockData : MockData | ||
|
|
||
| val userString = "unittestuser" | ||
|
|
@@ -42,6 +52,17 @@ internal class CollectionControllerTest @Autowired constructor( | |
| fun setup() { | ||
| mockData = MockData() | ||
| ReflectionTestUtils.setField(appConfig, "roleAdmin", "ROLE_Osmt_Admin") | ||
| val securityContext: SecurityContext = Mockito.mock(SecurityContext::class.java) | ||
| SecurityContextHolder.setContext(securityContext) | ||
|
|
||
| val attributes: MutableMap<String, Any> = HashMap() | ||
| attributes["email"] = userEmail | ||
|
|
||
| val authority: GrantedAuthority = OAuth2UserAuthority("ROLE_Osmt_Admin", attributes) | ||
| val authorities: MutableSet<GrantedAuthority> = HashSet() | ||
| authorities.add(authority) | ||
| Mockito.`when`(securityContext.authentication).thenReturn(authentication) | ||
| Mockito.`when`(SecurityContextHolder.getContext().authentication.authorities).thenReturn(authorities) | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -70,4 +91,26 @@ internal class CollectionControllerTest @Autowired constructor( | |
| Assertions.assertThat(collectionRepository.findAll().toList()).hasSize(1) | ||
| Assertions.assertThat(result).isNotNull | ||
| } | ||
|
|
||
| @Test | ||
| fun `updateCollection() should return a collection to draft status when providing an Unarchived status and update the archived date`() { | ||
| // arrange | ||
| val jwt = Jwt.withTokenValue("foo").header("foo", "foo").claim("email", userEmail).build() | ||
| val update = ApiCollectionUpdate( | ||
| name = "newName", | ||
| description = "newDescription", | ||
| publishStatus = PublishStatus.Unarchived, | ||
| author = "newAuthor") | ||
| val collection = collectionRepository.create(name = "name", user = "user", email = "user@xmail.com", description = "description") | ||
| collection!!.status = PublishStatus.Archived | ||
| collection.archiveDate = LocalDateTime.now() | ||
|
|
||
| // act | ||
| val result = collectionController.updateCollection(collection.uuid, update, jwt) | ||
|
|
||
| // assert | ||
| Assertions.assertThat(result).isNotNull | ||
| Assertions.assertThat(result.status).isEqualTo(PublishStatus.Draft) | ||
| Assertions.assertThat(result.archiveDate).isNull() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package edu.wgu.osmt.collection | ||
|
|
||
| import edu.wgu.osmt.SpringTest | ||
| import edu.wgu.osmt.db.PublishStatus | ||
| import org.assertj.core.api.Assertions | ||
| import org.junit.jupiter.api.Test | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.transaction.annotation.Transactional | ||
| import java.time.LocalDateTime | ||
|
|
||
| @Transactional | ||
| internal class CollectionUpdateObjectTest @Autowired constructor( | ||
| val collectionRepository: CollectionRepository | ||
| ): SpringTest(){ | ||
|
|
||
| @Test | ||
| fun `applyStatusChange() should apply Archived status`() { | ||
| //Arrange | ||
| val collectionUpdateObject = CollectionUpdateObject(publishStatus = PublishStatus.Archived) | ||
| val dao: CollectionDao = | ||
| collectionRepository.create(name = "name", user = "user", email = "user@email.edu", description = "description")!! | ||
|
|
||
| //Act | ||
| collectionUpdateObject.applyStatusChange(dao) | ||
|
|
||
| //Assert | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Archived) | ||
| Assertions.assertThat(dao.archiveDate).isNotNull | ||
| } | ||
|
|
||
| @Test | ||
| fun `applyStatusChange() should apply Published status`() { | ||
| //Arrange | ||
| val collectionUpdateObject = CollectionUpdateObject(publishStatus = PublishStatus.Published) | ||
| val dao: CollectionDao = | ||
| collectionRepository.create(name = "name", user = "user", email = "user@email.edu", description = "description")!! | ||
|
|
||
| //Act | ||
| collectionUpdateObject.applyStatusChange(dao) | ||
|
|
||
| //Assert | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Published) | ||
| Assertions.assertThat(dao.publishDate).isNotNull | ||
| } | ||
|
|
||
| @Test | ||
| fun `applyStatusChange() should return to previous draft status if no Publish date is recorded`() { | ||
| //Arrange | ||
| val collectionUpdateObject = CollectionUpdateObject(publishStatus = PublishStatus.Unarchived) | ||
| val dao: CollectionDao = | ||
| collectionRepository.create(name = "name", user = "user", email = "user@email.edu", description = "description")!! | ||
| dao.status = PublishStatus.Archived | ||
| dao.publishDate = null | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Archived) | ||
|
|
||
| //Act | ||
| collectionUpdateObject.applyStatusChange(dao) | ||
|
|
||
| //Assert | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Draft) | ||
| } | ||
|
|
||
| @Test | ||
| fun `applyStatusChange() should return to previous published status if Publish date is recorded`() { | ||
| //Arrange | ||
| val collectionUpdateObject = CollectionUpdateObject(publishStatus = PublishStatus.Unarchived) | ||
| val dao: CollectionDao = | ||
| collectionRepository.create(name = "name", user = "user", email = "user@email.edu", description = "description")!! | ||
| dao.status = PublishStatus.Archived | ||
| dao.publishDate = LocalDateTime.now() | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Archived) | ||
|
|
||
| //Act | ||
| collectionUpdateObject.applyStatusChange(dao) | ||
|
|
||
| //Assert | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Published) | ||
| Assertions.assertThat(dao.publishDate).isNotNull | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| fun `applyStatusChange() should delete publish and archive date if draft status is applied and Apply draft status`() { | ||
| //Arrange | ||
| val collectionUpdateObject = CollectionUpdateObject(publishStatus = PublishStatus.Draft) | ||
| val dao: CollectionDao = | ||
| collectionRepository.create(name = "name", user = "user", email = "user@email.edu", description = "description")!! | ||
| dao.publishDate = LocalDateTime.now() | ||
| dao.archiveDate = LocalDateTime.now() | ||
|
|
||
| //Act | ||
| collectionUpdateObject.applyStatusChange(dao) | ||
|
|
||
| //Assert | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Draft) | ||
| Assertions.assertThat(dao.publishDate).isNull() | ||
| Assertions.assertThat(dao.archiveDate).isNull() | ||
| } | ||
|
|
||
| @Test | ||
| fun `applyStatusChange() should Apply workspace status`() { | ||
| //Arrange | ||
| val collectionUpdateObject = CollectionUpdateObject(publishStatus = PublishStatus.Workspace) | ||
| val dao: CollectionDao = | ||
| collectionRepository.create(name = "name", user = "user", email = "user@email.edu", description = "description")!! | ||
| dao.publishDate = LocalDateTime.now() | ||
| dao.archiveDate = LocalDateTime.now() | ||
|
|
||
| //Act | ||
| collectionUpdateObject.applyStatusChange(dao) | ||
|
|
||
| //Assert | ||
| Assertions.assertThat(dao.status).isEqualTo(PublishStatus.Workspace) | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this case ever happen? CollectionDao.status is not nullable, and the dao argument is also not nullable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I just supposed that this was like a default in java case statement, I have heard that it is a good practice to leave not skip the default even if nothing needs to happen there.