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
14 changes: 8 additions & 6 deletions api/src/main/kotlin/edu/wgu/osmt/api/model/ApiSortEnum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package edu.wgu.osmt.api.model

import edu.wgu.osmt.config.CATEGORY_ASC
import edu.wgu.osmt.config.CATEGORY_DESC
import edu.wgu.osmt.config.CATEGORY_SORT_INSENSITIVE
import edu.wgu.osmt.config.NAME_ASC
import edu.wgu.osmt.config.NAME_DESC
import edu.wgu.osmt.config.NAME_SORT_INSENSITIVE
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.data.domain.Sort
Expand Down Expand Up @@ -44,19 +46,19 @@ interface SortOrderCompanion<T> where T: SortOrder{
enum class SkillSortEnum(override val apiValue: String) : SortOrder {
CategoryAsc(CATEGORY_ASC) {
override val sort = Sort.by(
Sort.Order.asc("category.sort_insensitive"),
Sort.Order.asc("name.sort_insensitive"))
Sort.Order.asc(CATEGORY_SORT_INSENSITIVE),
Sort.Order.asc(NAME_SORT_INSENSITIVE))
},
CategoryDesc(CATEGORY_DESC) {
override val sort = Sort.by(
Sort.Order.desc("category.sort_insensitive"),
Sort.Order.asc("name.sort_insensitive"))
Sort.Order.desc(CATEGORY_SORT_INSENSITIVE),
Sort.Order.asc(NAME_SORT_INSENSITIVE))
},
NameAsc(NAME_ASC) {
override val sort = Sort.by(nameKeyword).ascending()
override val sort = Sort.by(NAME_SORT_INSENSITIVE).ascending()
},
NameDesc(NAME_DESC) {
override val sort = Sort.by(nameKeyword).descending()
override val sort = Sort.by(NAME_SORT_INSENSITIVE).descending()
};

companion object : SortOrderCompanion<SkillSortEnum> {
Expand Down
5 changes: 4 additions & 1 deletion api/src/main/kotlin/edu/wgu/osmt/config/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ const val NAME_ASC = "skill.asc"
const val NAME_DESC = "skill.desc"



// ElasticSearch Index Names
const val INDEX_RICHSKILL_DOC = "richskill_v1"
const val INDEX_COLLECTION_DOC = "collection_v1"
const val INDEX_JOBCODE_DOC = "jobcode_v1"
const val INDEX_KEYWORD_DOC = "keyword"

// ElasticSearch Sort Criteria
const val NAME_SORT_INSENSITIVE = "name.sort_insensitive"
const val CATEGORY_SORT_INSENSITIVE = "category.sort_insensitive"

3 changes: 2 additions & 1 deletion api/src/main/kotlin/edu/wgu/osmt/richskill/RichSkillDoc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ data class RichSkillDoc(
otherFields = [
InnerField(suffix = "", type = Search_As_You_Type),
InnerField(suffix = "raw", analyzer = "whitespace_exact", type = Text),
InnerField(suffix = "keyword", type = Keyword)
InnerField(suffix = "keyword", type = Keyword),
InnerField(suffix = "sort_insensitive", type = Keyword, normalizer = "lowercase_normalizer")
]
)
@get:JsonProperty("skillStatement")
Expand Down
152 changes: 152 additions & 0 deletions api/src/test/kotlin/edu/wgu/osmt/richskill/RichSkillSortOrderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package edu.wgu.osmt.richskill

import edu.wgu.osmt.BaseDockerizedTest
import edu.wgu.osmt.HasDatabaseReset
import edu.wgu.osmt.HasElasticsearchReset
import edu.wgu.osmt.SpringTest
import edu.wgu.osmt.collection.CollectionEsRepo
import edu.wgu.osmt.config.CATEGORY_ASC
import edu.wgu.osmt.config.CATEGORY_DESC
import edu.wgu.osmt.config.NAME_ASC
import edu.wgu.osmt.config.NAME_DESC
import edu.wgu.osmt.jobcode.JobCodeEsRepo
import edu.wgu.osmt.keyword.KeywordEsRepo
import edu.wgu.osmt.mockdata.MockData
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.oauth2.jwt.Jwt
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.util.UriComponentsBuilder
import java.lang.String.CASE_INSENSITIVE_ORDER


@Transactional
internal class RichSkillSortOrderTest @Autowired constructor(
override val richSkillEsRepo: RichSkillEsRepo,
override val collectionEsRepo: CollectionEsRepo,
override val keywordEsRepo: KeywordEsRepo,
override val jobCodeEsRepo: JobCodeEsRepo
): SpringTest(), BaseDockerizedTest, HasDatabaseReset, HasElasticsearchReset {

@Autowired
lateinit var richSkillController: RichSkillController

private lateinit var mockData : MockData

private val nullJwt : Jwt? = null



@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class SortedResults {

var size: Int = 0
private lateinit var listOfSkills: List<RichSkillDoc>

@BeforeAll
fun setup() {
// Arrange
mockData = MockData()
size = 50
listOfSkills = mockData.getRichSkillDocs()
richSkillEsRepo.saveAll(listOfSkills)
}

@Test
fun `sorted by default(category ASC)`() {
// Act
val result = richSkillController.allPaginated(
UriComponentsBuilder.newInstance(),
size,
0,
arrayOf("draft", "published"),
"",
nullJwt
)
val rsdList: List<RichSkillDoc>? = result.body

// Assert
assertThat(rsdList).isSortedAccordingTo(
Comparator.comparing(RichSkillDoc::category, CASE_INSENSITIVE_ORDER)
)
}
@Test
fun `sorted by category ASC and name ASC`() {
// Act
val result = richSkillController.allPaginated(
UriComponentsBuilder.newInstance(),
size,
0,
arrayOf("draft", "published"),
CATEGORY_ASC,
nullJwt
)
val body: List<RichSkillDoc>? = result.body
val byNameAndCategory = Comparator.comparing(RichSkillDoc::category, CASE_INSENSITIVE_ORDER)
.thenComparing (RichSkillDoc::name, CASE_INSENSITIVE_ORDER)

// Assert
assertThat(body).isSortedAccordingTo(byNameAndCategory)

}
@Test
fun `sorted by category DESC and name ASC`() {
// Act
val result = richSkillController.allPaginated(
UriComponentsBuilder.newInstance(),
size,
0,
arrayOf("draft", "published"),
CATEGORY_DESC,
nullJwt
)
val body: List<RichSkillDoc>? = result.body
val byCategoryDescAndThenByName = Comparator.comparing(RichSkillDoc::category, CASE_INSENSITIVE_ORDER).reversed()
.thenComparing (RichSkillDoc::name, CASE_INSENSITIVE_ORDER)

// Assert
assertThat(body).isSortedAccordingTo(byCategoryDescAndThenByName)
}
@Test
fun `sorted by name ASC`() {
// Act
val result = richSkillController.allPaginated(
UriComponentsBuilder.newInstance(),
size,
0,
arrayOf("draft", "published"),
NAME_ASC,
nullJwt
)
val rsdList: List<RichSkillDoc>? = result.body

// Assert
assertThat(rsdList).isSortedAccordingTo(
Comparator.comparing(RichSkillDoc::name, CASE_INSENSITIVE_ORDER)
)
}
@Test
fun `sorted by name DESC`() {
// Act
val result = richSkillController.allPaginated(
UriComponentsBuilder.newInstance(),
size,
0,
arrayOf("draft", "published"),
NAME_DESC,
nullJwt
)
val rsdList: List<RichSkillDoc>? = result.body

// Assert
assertThat(rsdList).isSortedAccordingTo(
Comparator.comparing(RichSkillDoc::name, CASE_INSENSITIVE_ORDER).reversed()
)
}
}
}