This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
OffsetPaginatedListLinksWriterInterceptorSpec.scala
73 lines (60 loc) · 2.61 KB
/
OffsetPaginatedListLinksWriterInterceptorSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.cerner.beadledom.pagination
import com.cerner.beadledom.pagination.models.OffsetPaginatedListDto
import javax.ws.rs.core.UriInfo
import org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext
import org.jboss.resteasy.spi.ResteasyUriInfo
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FunSpec, MustMatchers}
import scala.collection.JavaConverters._
/**
* Spec for OffsetPaginatedListLinksWriterInterceptor.
*
* @author John Leacox
*/
class OffsetPaginatedListLinksWriterInterceptorSpec extends FunSpec with MustMatchers with MockitoSugar {
describe("OffsetPaginatedListLinksWriterInterceptor") {
describe("#aroundWriteTo") {
it("replaces an OffsetPaginatedList entity with an OffsetPaginatedListDtoImpl entity") {
val list = OffsetPaginatedList.builder()
.items(List("a", "b").asJava)
.metadata("limit", 20, "offset", 0L, null, true)
.build()
val uriInfo = mockUriInfo()
val interceptor = new OffsetPaginatedListLinksWriterInterceptor
interceptor.uriInfo = uriInfo
val context = mock[AbstractWriterInterceptorContext]
when(context.setEntity(any())).thenCallRealMethod()
when(context.getEntity).thenCallRealMethod()
context.setEntity(list)
interceptor.aroundWriteTo(context)
val listWithLinks = context.getEntity.asInstanceOf[OffsetPaginatedListDto[String]]
listWithLinks.items.asScala mustBe List("a", "b")
listWithLinks.firstLink() mustBe "example.com?offset=0&limit=20"
listWithLinks.lastLink() mustBe null
listWithLinks.nextLink() mustBe "example.com?offset=20&limit=20"
listWithLinks.prevLink() mustBe null
}
it("does not replace entities of other types") {
val uriInfo = mockUriInfo()
val interceptor = new OffsetPaginatedListLinksWriterInterceptor
interceptor.uriInfo = uriInfo
val context = mock[AbstractWriterInterceptorContext]
when(context.setEntity(any())).thenCallRealMethod()
when(context.getEntity).thenCallRealMethod()
val entity = List("a", "b", "c").asJava
context.setEntity(entity)
interceptor.aroundWriteTo(context)
context.getEntity mustBe entity
}
}
}
private def mockUriInfo(queryParams: (String, String)*): UriInfo = {
val queryString = queryParams
.filter({ case (_, v) => v != null })
.map({ case (k, v) => s"$k=$v" }).mkString("&")
val uriInfo = new ResteasyUriInfo("example.com", queryString, "")
uriInfo
}
}