Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion api/src/main/kotlin/edu/wgu/osmt/collection/Collection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ data class CollectionUpdateObject(

override fun applyToDao(dao: CollectionDao): Unit{
dao.updateDate = LocalDateTime.now(ZoneOffset.UTC)
applyPublishStatus(dao)
applyStatusChange(dao)
name?.let { dao.name = it }

description?.let {
Expand All @@ -76,6 +76,38 @@ data class CollectionUpdateObject(
applySkills()
}

fun applyStatusChange(dao: CollectionDao) {
when (publishStatus) {
PublishStatus.Archived -> {
dao.archiveDate = LocalDateTime.now(ZoneOffset.UTC)
dao.status = PublishStatus.Archived
}
PublishStatus.Published -> {
dao.publishDate = LocalDateTime.now(ZoneOffset.UTC)
dao.status = PublishStatus.Published
dao.archiveDate = null
}
PublishStatus.Unarchived -> {
if (dao.publishDate != null) {
dao.status = PublishStatus.Published

} else {
dao.status = PublishStatus.Draft
}
dao.archiveDate = null
}
PublishStatus.Draft -> {
dao.status = PublishStatus.Draft
dao.archiveDate = null
dao.publishDate = null
}
PublishStatus.Workspace -> {
dao.status = PublishStatus.Workspace
}
else -> {}
Copy link
Contributor

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.

Copy link
Contributor Author

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.

}
}

fun applySkills(): CollectionUpdateObject{
skills?.let {
it.add?.forEach { skill ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class CollectionRepositoryImpl @Autowired constructor(
}

updateObject.copy(id = newCollection.id.value).applyToDao(newCollection)
if(PublishStatus.Workspace == newCollection.status) {
if(PublishStatus.Workspace == updateObject.publishStatus) {
newCollection.workspaceOwner = email
}

Expand Down
8 changes: 0 additions & 8 deletions api/src/main/kotlin/edu/wgu/osmt/db/UpdateObject.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package edu.wgu.osmt.db

import edu.wgu.osmt.collection.CollectionDao
import org.apache.commons.lang3.StringUtils
import org.jetbrains.exposed.dao.LongEntity
import java.time.LocalDateTime
import java.time.ZoneOffset
Expand Down Expand Up @@ -29,11 +27,5 @@ interface HasPublishStatus<T: MutablePublishStatusDetails> {
}
else -> {} // draft is non-op
}
if(dao is CollectionDao && publishStatus != null) {
dao.status = publishStatus as PublishStatus
if (dao.status != PublishStatus.Workspace) {
dao.workspaceOwner = StringUtils.EMPTY
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -31,6 +39,8 @@ internal class CollectionControllerTest @Autowired constructor(
@Autowired
lateinit var collectionController: CollectionController

var authentication: Authentication = Mockito.mock(Authentication::class.java)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

}