Skip to content

Commit

Permalink
Annotation Service tests are implemented and the some code moved to D…
Browse files Browse the repository at this point in the history
…toConverter to increse readability (#676)

Good. Thanks.
  • Loading branch information
emresin12 authored Dec 25, 2023
1 parent 371e58f commit dc95dba
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 138 deletions.
1 change: 1 addition & 0 deletions app/annotation-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation("org.postgresql:postgresql")
compileOnly("org.projectlombok:lombok:1.18.24")

implementation("org.mockito.kotlin:mockito-kotlin:5.1.0")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,80 @@
package com.group6.annotationservice.Utils

import com.group6.annotationservice.dtos.*
import com.group6.annotationservice.entity.*
import com.group6.annotationservice.entity.Annotation
import com.group6.annotationservice.entity.FragmentSelector
import com.group6.annotationservice.entity.TextPositionSelector
import com.group6.annotationservice.entity.Target

object DtoConverter {
// fun convertAnnotationDtoToAnnotation(annotationDto: AnnotationDto): Annotation {
// val annotation = Annotation(
// motivation = annotationDto.motivation,
// creator = annotationDto.creator,
// created = annotationDto.created,
// id = annotationDto.id,
// type = annotationDto.type,
// context = annotationDto.context
// )
//
// val bodyList = ArrayList<Body>()
// if (annotationDto.body != null) {
// for (bodyDto in annotationDto.body) {
// val body = Body(
// type = bodyDto.type,
// format = bodyDto.format,
// value = bodyDto.value,
// language = bodyDto.language,
// purpose = bodyDto.purpose,
// id = bodyDto.id,
// annotation = annotation
// )
// bodyList.add(body)
// }
// }
//
// val targetList = ArrayList<Target>()
//
// for (targetDto in annotationDto.target) {
//
// val selectorList = ArrayList<Selector>()
//
// if (targetDto.selectors != null) {
// for (selectorDto in targetDto.selectors) {
//
// if (selectorDto.type == "FragmentSelector") {
// val selector = FragmentSelector(
// type = selectorDto.type,
// value = (selectorDto as FragmentSelectorDto).value
// )
// selectorList.add(selector)
// }
// else if (selectorDto.type == "TextPositionSelector"){
// val selector = TextPositionSelector(
// type = selectorDto.type,
// startIndex = (selectorDto as TextPositionSelectorDto).start,
// endIndex = selectorDto.end
// )
// selectorList.add(selector)
// }
//
// }
// }
//
// val target = Target(
// id = targetDto.id,
// format = targetDto.format,
// language = targetDto.language,
// annotations = annotation,
// selector = selectorList
// )
//
// targetList.add(target)
// }
// annotation.body = bodyList
// annotation.target = targetList
//
//
// return annotation
// }
fun convertAnnotationDtoToAnnotation(annotationDto: AnnotationDto): Annotation {

val motivationList = ArrayList<Motivation>()
if (annotationDto.motivation != null) {

for (motivation in annotationDto.motivation) {
motivationList.add(Motivation(value = motivation))
}

}
val annotation = Annotation(
creator = annotationDto.creator,
created = annotationDto.created,
id = annotationDto.id,
type = annotationDto.type,
context = annotationDto.context,
motivation = motivationList
)

val bodyList = ArrayList<Body>()
if (annotationDto.body != null) {

for (bodyDto in annotationDto.body) {
val body = Body(
type = bodyDto.type,
format = bodyDto.format,
value = bodyDto.value,
language = bodyDto.language,
purpose = bodyDto.purpose,
id = bodyDto.id,
)
bodyList.add(body)
}
}

val target = Target(
id = annotationDto.target.id,
format = annotationDto.target.format,
language = annotationDto.target.language,
)

val targetDto = annotationDto.target
if (targetDto.selector != null) {
val selectorDto = targetDto.selector!!
if (selectorDto.type == "FragmentSelector") {
val selector = FragmentSelector(
type = selectorDto.type,
value = (selectorDto as FragmentSelectorDto).value
)
annotation.selector = selector


} else if (selectorDto.type == "TextPositionSelector") {
val selector = TextPositionSelector(
type = selectorDto.type,
startIndex = (selectorDto as TextPositionSelectorDto).start,
endIndex = selectorDto.end
)
annotation.selector = selector
}
}

annotation.body = bodyList
annotation.target = target



return annotation
}

fun convertAnnotationToAnnotationDto(annotation: Annotation): AnnotationDto {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.group6.annotationservice.service

import com.group6.annotationservice.Utils.DtoConverter
import com.group6.annotationservice.dtos.AnnotationDto
import com.group6.annotationservice.dtos.FragmentSelectorDto
import com.group6.annotationservice.dtos.TextPositionSelectorDto
import com.group6.annotationservice.entity.*
import com.group6.annotationservice.entity.Annotation
import com.group6.annotationservice.entity.Target
import com.group6.annotationservice.repository.AnnotationRepository
import org.springframework.stereotype.Service

Expand All @@ -16,68 +13,7 @@ class AnnotationService (
) {
fun createAnnotation(annotationDto: AnnotationDto): Annotation {

val motivationList = ArrayList<Motivation>()
if (annotationDto.motivation != null) {

for (motivation in annotationDto.motivation) {
motivationList.add(Motivation(value = motivation))
}

}
val annotation = Annotation(
creator = annotationDto.creator,
created = annotationDto.created,
id = annotationDto.id,
type = annotationDto.type,
context = annotationDto.context,
motivation = motivationList
)

val bodyList = ArrayList<Body>()
if (annotationDto.body != null) {

for (bodyDto in annotationDto.body) {
val body = Body(
type = bodyDto.type,
format = bodyDto.format,
value = bodyDto.value,
language = bodyDto.language,
purpose = bodyDto.purpose,
id = bodyDto.id,
)
bodyList.add(body)
}
}

val target = Target(
id = annotationDto.target.id,
format = annotationDto.target.format,
language = annotationDto.target.language,
)

val targetDto = annotationDto.target
if (targetDto.selector != null) {
val selectorDto = targetDto.selector!!
if (selectorDto.type == "FragmentSelector") {
val selector = FragmentSelector(
type = selectorDto.type,
value = (selectorDto as FragmentSelectorDto).value
)
annotation.selector = selector


} else if (selectorDto.type == "TextPositionSelector") {
val selector = TextPositionSelector(
type = selectorDto.type,
startIndex = (selectorDto as TextPositionSelectorDto).start,
endIndex = selectorDto.end
)
annotation.selector = selector
}
}

annotation.body = bodyList
annotation.target = target
val annotation = DtoConverter.convertAnnotationDtoToAnnotation(annotationDto)

return annotationRepository.save(annotation)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.group6.annotationservice

import com.group6.annotationservice.controller.AnnotationController
import com.group6.annotationservice.dtos.AnnotationDto
import com.group6.annotationservice.dtos.BodyDto
import com.group6.annotationservice.dtos.FragmentSelectorDto
import com.group6.annotationservice.dtos.TargetDto
import com.group6.annotationservice.entity.*
import com.group6.annotationservice.entity.Annotation
import com.group6.annotationservice.entity.Target
import com.group6.annotationservice.service.AnnotationService
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.junit.jupiter.MockitoExtension
import org.springframework.http.HttpStatus

@ExtendWith(MockitoExtension::class)
class AnnotationControllerTests {

@Mock
private lateinit var annotationService: AnnotationService

@InjectMocks
private lateinit var annotationController: AnnotationController


@Test
fun `createAnnotation should return created status with converted DTO`() {
val inputDto = AnnotationDto(
context = "http://www.w3.org/ns/anno.jsonld",
id = "1",
type = "Annotation",
motivation = listOf("editing"),
creator = "test_creator",
body = listOf(
BodyDto(id = "1", type = "TextualBody", value = "Example text")
),
target = TargetDto(
id = "1",
format = "text/plain",
selector = FragmentSelectorDto(
type = "FragmentSelector",
value = "Example selector"
)
)
)


val createdAnnotation = Annotation(
context = "http://www.w3.org/ns/anno.jsonld",
id = "1",
type = "Annotation",
motivation = listOf(Motivation(value = "editing")),
creator = "test_creator",
body = listOf(
Body(id = "1", type = "TextualBody", value = "Example text")
),
target = Target(
id = "1",
format = "text/plain",
),
selector = FragmentSelector(
type = "FragmentSelector",
value = "Example selector"
)
)

val convertedDto = AnnotationDto(
context = "http://www.w3.org/ns/anno.jsonld",
id = "1",
type = "Annotation",
motivation = listOf("editing"),
creator = "test_creator",
created = createdAnnotation.created,
body = listOf(
BodyDto(id = "1", type = "TextualBody", value = "Example text")
),
target = TargetDto(
id = "1",
format = "text/plain",
selector = FragmentSelectorDto(
type = "FragmentSelector",
value = "Example selector"
)
)
)


`when`(annotationService.createAnnotation(inputDto)).thenReturn(createdAnnotation)
//`when`(DtoConverter.convertAnnotationToAnnotationDto(createdAnnotation)).thenReturn(convertedDto)

val response = annotationController.createAnnotation(inputDto)

assertEquals(HttpStatus.CREATED, response.statusCode)
assertEquals(convertedDto.id, response.body!!.id)
assertEquals(convertedDto.type, response.body!!.type)
assertEquals(convertedDto.motivation, response.body!!.motivation)
assertEquals(convertedDto.creator, response.body!!.creator)
assertEquals(convertedDto.created, response.body!!.created)
assertEquals(convertedDto.body, response.body!!.body)
assertEquals(convertedDto.target.id, response.body!!.target.id)
assertEquals(convertedDto.target.format, response.body!!.target.format)
assertEquals(convertedDto.target.language, response.body!!.target.language)
assertEquals(convertedDto.target.selector, response.body!!.target.selector)
}


}
Loading

0 comments on commit dc95dba

Please sign in to comment.