Skip to content

Commit

Permalink
refactor: make slug non optional
Browse files Browse the repository at this point in the history
  • Loading branch information
AvilaAndre committed Nov 7, 2024
1 parent 2322b14 commit aeef473
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class ActivityDto<T : Activity>(
val title: String,
val description: String,
val teamMembersIds: List<Long>?,
val slug: String?,
val slug: String,
var image: String?,
@JsonIgnore
var imageFile: MultipartFile? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EventDto(
title: String,
description: String,
teamMembersIds: List<Long>?,
slug: String?,
slug: String,
image: String?,

val registerUrl: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class PostDto(
val title: String,
val body: String,
val thumbnailPath: String,
val slug: String?
val slug: String
) : EntityDto<Post>()
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ProjectDto(
title: String,
description: String,
teamMembersIds: List<Long>?,
slug: String?,
slug: String,
image: String?,

val isArchived: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class Activity(

@Column(unique = true)
@field:Size(min = Constants.Slug.minSize, max = Constants.Slug.maxSize)
open val slug: String? = null,
open val slug: String,

@field:NotBlank
open var image: String,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/pt/up/fe/ni/website/backend/model/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Event(
description: String,
teamMembers: MutableList<Account> = mutableListOf(),
associatedRoles: MutableList<PerActivityRole> = mutableListOf(),
slug: String? = null,
slug: String,
image: String,
gallery: MutableList<String> = mutableListOf(),

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/pt/up/fe/ni/website/backend/model/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ class Post(

@Column(unique = true)
@field:Size(min = Constants.Slug.minSize, max = Constants.Slug.maxSize)
val slug: String? = null
val slug: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Project(
description: String,
teamMembers: MutableList<Account> = mutableListOf(),
associatedRoles: MutableList<PerActivityRole> = mutableListOf(),
slug: String? = null,
slug: String,
image: String,
gallery: MutableList<String> = mutableListOf(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import pt.up.fe.ni.website.backend.model.Activity

@Repository
interface ActivityRepository<T : Activity> : CrudRepository<T, Long> {
fun findBySlug(slug: String?): T?
fun findBySlug(slug: String): T?
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import pt.up.fe.ni.website.backend.model.Post

@Repository
interface PostRepository : JpaRepository<Post, Long> {
fun findBySlug(slug: String?): Post?
fun findBySlug(slug: String): Post?
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class AbstractActivityService<T : Activity>(
}

dto.imageFile?.let {
val fileName = fileUploader.buildFileName(it, dto.title)
val fileName = fileUploader.buildFileName(it, dto.slug)
dto.image = fileUploader.uploadImage(imageFolder, fileName, it.bytes)
}

Expand All @@ -52,7 +52,7 @@ abstract class AbstractActivityService<T : Activity>(
if (imageFile == null) {
dto.image = activity.image
} else {
val fileName = fileUploader.buildFileName(imageFile, dto.title)
val fileName = fileUploader.buildFileName(imageFile, dto.slug)
dto.image = fileUploader.uploadImage(imageFolder, fileName, imageFile.bytes)
}

Expand Down Expand Up @@ -86,7 +86,7 @@ abstract class AbstractActivityService<T : Activity>(
fun addGalleryImage(activityId: Long, image: MultipartFile, imageFolder: String): Activity {
val activity = getActivityById(activityId)

val fileName = fileUploader.buildFileName(image, activity.title)
val fileName = fileUploader.buildFileName(image, activity.slug)
val imageName = fileUploader.uploadImage("$imageFolder/gallery", fileName, image.bytes)

activity.gallery.add(imageName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ internal class EventControllerTest @Autowired constructor(
"This event was a failure",
mutableListOf(testAccount),
mutableListOf(),
null,
"bad-event",
"bad-image.png",
mutableListOf(),
null,
Expand All @@ -300,7 +300,7 @@ internal class EventControllerTest @Autowired constructor(
"This event was ok",
mutableListOf(),
mutableListOf(),
null,
"mid-event",
"mid-image.png",
mutableListOf(),
null,
Expand All @@ -316,7 +316,7 @@ internal class EventControllerTest @Autowired constructor(
"This event was a awesome",
mutableListOf(testAccount),
mutableListOf(),
null,
"cool-event",
"cool-image.png",
mutableListOf(),
null,
Expand Down Expand Up @@ -362,7 +362,7 @@ internal class EventControllerTest @Autowired constructor(
inner class CreateEvent {
private val uuid: UUID = UUID.randomUUID()
private val mockedSettings = Mockito.mockStatic(UUID::class.java)
private val expectedImagePath = "${uploadConfigProperties.staticServe}/events/${testEvent.title}-$uuid.jpeg"
private val expectedImagePath = "${uploadConfigProperties.staticServe}/events/${testEvent.slug}-$uuid.jpeg"

@BeforeEach
fun addAccount() {
Expand Down Expand Up @@ -841,7 +841,7 @@ internal class EventControllerTest @Autowired constructor(

@Test
fun `should add an image`() {
val expectedImagePath = "${uploadConfigProperties.staticServe}/events/gallery/${testEvent.title}-$uuid.jpeg"
val expectedImagePath = "${uploadConfigProperties.staticServe}/events/gallery/${testEvent.slug}-$uuid.jpeg"

mockMvc.multipartBuilder("/events/${testEvent.id}/gallery")
.asPutMethod()
Expand Down Expand Up @@ -1108,7 +1108,7 @@ internal class EventControllerTest @Autowired constructor(

@Test
fun `should update the event with same slug`() {
eventPart["slug"] = testEvent.slug!!
eventPart["slug"] = testEvent.slug
mockMvc.multipartBuilder("/events/${testEvent.id}")
.asPutMethod()
.addPart("event", objectMapper.writeValueAsString(eventPart))
Expand Down Expand Up @@ -1162,7 +1162,7 @@ internal class EventControllerTest @Autowired constructor(

@Test
fun `should update the event with image`() {
val expectedImagePath = "${uploadConfigProperties.staticServe}/events/$newTitle-$uuid.jpeg"
val expectedImagePath = "${uploadConfigProperties.staticServe}/events/$newSlug-$uuid.jpeg"

mockMvc.multipartBuilder("/events/${testEvent.id}")
.asPutMethod()
Expand Down Expand Up @@ -1204,11 +1204,12 @@ internal class EventControllerTest @Autowired constructor(
}

@Test
fun `should fail if the event does not exist`() {
fun`should fail if the event does not exist`() {
val eventPart = objectMapper.writeValueAsString(
mapOf(
"title" to "New Title",
"description" to "New Description",
"slug" to "new-slug",
"dateInterval" to DateInterval(TestUtils.createDate(2022, Calendar.DECEMBER, 1), null),
"associatedRoles" to testEvent.associatedRoles
)
Expand Down Expand Up @@ -1289,6 +1290,7 @@ internal class EventControllerTest @Autowired constructor(
requiredFields = mapOf(
"title" to testEvent.title,
"description" to testEvent.description,
"slug" to testEvent.slug,
"dateInterval" to testEvent.dateInterval,
"image" to testEvent.image
)
Expand Down Expand Up @@ -1340,6 +1342,29 @@ internal class EventControllerTest @Autowired constructor(
)
}

@NestedTest
@DisplayName("slug")
inner class SlugValidation {
@BeforeAll
fun setParam() {
validationTester.param = "slug"
}

@Test
fun `should be required`() = validationTester.isRequired()

@Test
@DisplayName(
"size should be between ${ActivityConstants.Slug.minSize}" +
" and ${ActivityConstants.Slug.maxSize}()"
)
fun size() =
validationTester.hasSizeBetween(
ActivityConstants.Slug.minSize,
ActivityConstants.Slug.maxSize
)
}

@NestedTest
@DisplayName("registerUrl")
inner class UrlValidation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ class GenerationControllerTest @Autowired constructor(
Project(
"NIJobs",
"cool project",
slug = "ni-jobs",
image = "cool-image.png",
targetAudience = "students",
github = "https://github.com/NIAEFEUP/nijobs-be",
Expand All @@ -1082,6 +1083,7 @@ class GenerationControllerTest @Autowired constructor(
Event(
title = "SINF",
description = "cool event",
slug = "sinf",
dateInterval = DateInterval(TestUtils.createDate(2023, 9, 10)),
location = null,
category = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ internal class PostControllerTest @Autowired constructor(
Post(
"NIAEFEUP gets a new president",
"New president promised to buy new chairs",
"https://thumbnails/pres.png"
"https://thumbnails/pres.png",
slug = "new-president"
)
)

Expand Down Expand Up @@ -255,6 +256,7 @@ internal class PostControllerTest @Autowired constructor(
requiredFields = mapOf(
"title" to testPost.title,
"body" to testPost.body,
"slug" to testPost.slug,
"thumbnailPath" to testPost.thumbnailPath
)
)
Expand Down Expand Up @@ -314,6 +316,9 @@ internal class PostControllerTest @Autowired constructor(
validationTester.param = "slug"
}

@Test
fun `should be required`() = validationTester.isRequired()

@Test
@DisplayName("size should be between ${Constants.Slug.minSize} and ${Constants.Slug.maxSize}()")
fun size() = validationTester.hasSizeBetween(Constants.Slug.minSize, Constants.Slug.maxSize)
Expand Down Expand Up @@ -392,7 +397,7 @@ internal class PostControllerTest @Autowired constructor(
)

@Test
fun `should update the post without the slug`() {
fun `should update the post without changing the slug`() {
val newTitle = "New Title"
val newBody = "New Body of the post"
val newThumbnailPath = "https://thumbnails/new.png"
Expand All @@ -405,7 +410,8 @@ internal class PostControllerTest @Autowired constructor(
mapOf(
"title" to newTitle,
"body" to newBody,
"thumbnailPath" to newThumbnailPath
"thumbnailPath" to newThumbnailPath,
"slug" to testPost.slug
)
)
)
Expand Down Expand Up @@ -436,7 +442,7 @@ internal class PostControllerTest @Autowired constructor(
}

@Test
fun `should update the post with the slug`() {
fun `should update the post changing the slug`() {
val newTitle = "New Title"
val newBody = "New Body of the post"
val newThumbnailPath = "https://thumbnails/new.png"
Expand Down Expand Up @@ -491,7 +497,8 @@ internal class PostControllerTest @Autowired constructor(
mapOf(
"title" to "New Title",
"body" to "New Body of the post",
"thumbnailPath" to "thumbnails/new.png"
"thumbnailPath" to "thumbnails/new.png",
"slug" to "new-slug"
)
)
)
Expand Down Expand Up @@ -562,7 +569,8 @@ internal class PostControllerTest @Autowired constructor(
requiredFields = mapOf(
"title" to testPost.title,
"body" to testPost.body,
"thumbnailPath" to testPost.thumbnailPath
"thumbnailPath" to testPost.thumbnailPath,
"slug" to testPost.slug
)
)

Expand Down Expand Up @@ -621,6 +629,9 @@ internal class PostControllerTest @Autowired constructor(
validationTester.param = "slug"
}

@Test
fun `should be required`() = validationTester.isRequired()

@Test
@DisplayName("size should be between ${Constants.Slug.minSize} and ${Constants.Slug.maxSize}()")
fun size() = validationTester.hasSizeBetween(Constants.Slug.minSize, Constants.Slug.maxSize)
Expand Down
Loading

0 comments on commit aeef473

Please sign in to comment.