-
Notifications
You must be signed in to change notification settings - Fork 18
/
AdminControllerSpec.scala
570 lines (491 loc) · 28.3 KB
/
AdminControllerSpec.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package controllers
import java.io.{ File, ByteArrayOutputStream }
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import com.m3.octoparts.cache.CacheOps
import com.m3.octoparts.cache.dummy.DummyCacheOps
import com.m3.octoparts.model.HttpMethod
import com.m3.octoparts.model.config._
import com.m3.octoparts.repository.MutableConfigsRepository
import com.m3.octoparts.repository.config._
import com.m3.octoparts.support.db.RequiresDB
import com.m3.octoparts.support.mocks.ConfigDataMocks
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.content.FileBody
import org.mockito.{ Mockito, ArgumentCaptor }
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest._
import org.scalatest.concurrent.{ IntegrationPatience, ScalaFutures }
import org.scalatest.mock.MockitoSugar.mock
import play.api.http.Writeable
import play.api.libs.json.Json
import play.api.mvc.EssentialAction
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.mvc.Result
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.language.postfixOps
class AdminControllerCompanionSpec extends FunSpec with Matchers with ConfigDataMocks {
it("should append 3 underscores") {
AdminController.makeNewName("hello", Set("hello__", "hello", "hello_", "hi")) should equal("hello___")
}
it("should not append any star") {
AdminController.makeNewName("hello", Set.empty, "*") should equal("hello")
}
describe(".shouldBustCache(endpoint, endpoint)") {
it("should return false if nothing changed at all") {
AdminController.shouldBustCache(mockHttpPartConfig, mockHttpPartConfig) should be(false)
}
it("should return false if nothing important was changed") {
AdminController.shouldBustCache(
mockHttpPartConfig,
mockHttpPartConfig.copy(alertMailsEnabled = !mockHttpPartConfig.alertMailsEnabled)) should be(false)
}
it("should return true if URI was changed ") {
AdminController.shouldBustCache(
mockHttpPartConfig,
mockHttpPartConfig.copy(uriToInterpolate = s"${mockHttpPartConfig.uriToInterpolate}/whoa")) should be(true)
}
it("should return true if CacheTTL was changed to be shorter") {
AdminController.shouldBustCache(
mockHttpPartConfig.copy(cacheTtl = Some(3.second)),
mockHttpPartConfig.copy(cacheTtl = Some(2.second))) should be(true)
}
it("should return true if CacheTTL was changed from None to Some(duration)") {
AdminController.shouldBustCache(
mockHttpPartConfig.copy(cacheTtl = None),
mockHttpPartConfig.copy(cacheTtl = Some(1.second))) should be(true)
}
it("should return false if CacheTTL was changed to be longer") {
AdminController.shouldBustCache(
mockHttpPartConfig.copy(cacheTtl = Some(3.second)),
mockHttpPartConfig.copy(cacheTtl = Some(4.second))) should be(false)
}
it("should return true if additionalValidStatuses was changed ") {
AdminController.shouldBustCache(
mockHttpPartConfig,
mockHttpPartConfig.copy(additionalValidStatuses = mockHttpPartConfig.additionalValidStatuses + 911)) should be(true)
}
it("should return true if method was changed ") {
AdminController.shouldBustCache(
mockHttpPartConfig,
mockHttpPartConfig.copy(method = HttpMethod.values.filter(_ != mockHttpPartConfig.method).firstKey)) should be(true)
}
}
describe(".shouldBustCache(param)") {
val unremarkableParam = mockPartParam.copy(required = false, versioned = false)
it("should return false the param is neither required nor versioned") {
AdminController.shouldBustCache(unremarkableParam) should be(false)
}
it("should return true if the param is required") {
AdminController.shouldBustCache(unremarkableParam.copy(required = true)) should be(true)
}
it("should return true if the param is versioned") {
AdminController.shouldBustCache(unremarkableParam.copy(versioned = true)) should be(true)
}
}
}
class AdminControllerSpec extends FunSpec
with Matchers
with ScalaFutures
with IntegrationPatience
with ConfigDataMocks
with RequiresDB {
protected val aGroup = "some group"
protected def part = {
mockHttpPartConfig.copy(
id = Some(3),
hystrixConfig = Some(mockHystrixConfig.copy(
threadPoolConfig = Some(mockThreadConfig),
threadPoolConfigId = mockThreadConfig.id))
)
}
protected val validPartEditFormParams: List[(String, String)] = List(
"partId" -> "aNewName",
"description" -> "stuff",
"uri" -> "http://www.example2.com",
"method" -> "post",
"commandKey" -> "commandKey",
"commandGroupKey" -> "commandGroupKey",
"timeoutInMs" -> "5000",
"threadPoolConfigId" -> "3"
)
it("should show a list of parts") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(Seq(part.copy(uriToInterpolate = "http://www.example.com", owner = "aName")))).when(repository).findAllConfigs()
val listParts = adminController.listParts(FakeRequest())
contentAsString(listParts) should include("http://www.example.com")
contentAsString(listParts) should include("aName")
}
it("should show a form to create a new part") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(Seq(part))).when(repository).findAllConfigs()
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(Seq(mockThreadConfig))).when(repository).findAllThreadPoolConfigs()
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroups()
val result = adminController.newPart.apply(FakeRequest())
status(result) should equal(OK)
contentAsString(result) should include("""input type="text" id="partId" name="partId" value="" """)
}
it("should show a form to edit an existing part") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(Seq(part))).when(repository).findAllConfigs()
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(Seq(mockThreadConfig))).when(repository).findAllThreadPoolConfigs()
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroups()
val result = adminController.editPart(part.partId).apply(FakeRequest())
status(result) should equal(OK)
contentAsString(result) should include(s"""input type="text" id="partId" name="partId" value="${part.partId}" """)
}
it("should create a new part") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(124L)).when(repository).save(anyObject[HttpPartConfig]())(anyObject[ConfigMapper[HttpPartConfig]])
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroupsByName(anyVararg[String]())
doReturn(Future.successful(Seq(mockThreadConfig))).when(repository).findAllThreadPoolConfigs()
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroups()
val result = adminController.createPart.apply(FakeRequest().withFormUrlEncodedBody(validPartEditFormParams: _*))
whenReady(result) { r =>
status(result) should be(FOUND)
redirectLocation(result).get should include(routes.AdminController.showPart("aNewName").url)
val newCiCaptor = ArgumentCaptor.forClass(classOf[HttpPartConfig])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[HttpPartConfig]])
verify(repository).findAllCacheGroupsByName(Seq.empty: _*)
newCiCaptor.getValue.uriToInterpolate should be("http://www.example2.com")
}
}
describe("when updating an existing part") {
def setupController: (AdminController, CacheOps, MutableConfigsRepository, HttpPartConfig) = {
val repository = mock[MutableConfigsRepository]
val cacheOps = mock[CacheOps]
val adminController = new AdminController(cacheOps = cacheOps, repository = repository)
val part2 = part.copy(hystrixConfig = Some(mockHystrixConfig))
doReturn(Future.successful(())).when(cacheOps).increasePartVersion(anyString())
doReturn(Future.successful(Some(part2))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(1)).when(repository).deleteConfigByPartId(part.partId)
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroupsByName(anyVararg[String]())
doReturn(Future.successful(Some(mockPartParam))).when(repository).findParamById(anyLong())
doReturn(Future.successful(Seq(mockThreadConfig))).when(repository).findAllThreadPoolConfigs()
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroups()
doReturn(Future.successful(Seq(part2))).when(repository).findAllConfigs()
(adminController, cacheOps, repository, part2)
}
describe("when all is good with the world") {
it("should update the item and redirect to the part's detail page") {
val (controller, cacheOps, repository, part) = setupController
doReturn(Future.successful(124L)).when(repository).save(anyObject[HttpPartConfig]())(anyObject[ConfigMapper[HttpPartConfig]])
val result = controller.updatePart(part.partId).apply(FakeRequest().withFormUrlEncodedBody(validPartEditFormParams: _*))
status(result) should be(FOUND)
redirectLocation(result).get should include(routes.AdminController.showPart("aNewName").url)
verify(cacheOps, Mockito.timeout(1000)).increasePartVersion(part.partId)
}
}
describe("when the part doesn't exist") {
it("should redirect to the part list page") {
val (controller, _, repository, part) = setupController
doReturn(Future.successful(None)).when(repository).findConfigByPartId("this is not here")
val result = controller.updatePart("this is not here").apply(FakeRequest().withFormUrlEncodedBody(validPartEditFormParams: _*))
status(result) should be(FOUND)
redirectLocation(result).get should include(routes.AdminController.listParts.url)
}
}
describe("when the save fails") {
it("should show the form again, with the user's filled-in information") {
val (controller, _, repository, part) = setupController
doReturn(Future.failed(new RuntimeException("oooh shiiieeet"))).when(repository).save(anyObject[HttpPartConfig]())(anyObject[ConfigMapper[HttpPartConfig]])
val result = controller.updatePart(part.partId).apply(FakeRequest().withFormUrlEncodedBody(validPartEditFormParams: _*))
status(result) should be(OK)
contentAsString(result) should include("""input type="text" id="partId" name="partId" value="aNewName" """)
}
}
}
describe("importing parts") {
it("should show the form") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
val showImportParts = adminController.showImportParts()(FakeRequest())
status(showImportParts) should be(OK)
contentAsString(showImportParts) should include("jsonfile")
contentAsString(showImportParts) should include(routes.AdminController.doImportParts().url)
verifyNoMoreInteractions(repository)
}
def makeFileUpload(data: Array[Byte], action: EssentialAction, fileKey: String = "jsonfile", fileContentType: ContentType = ContentType.APPLICATION_JSON): Future[Result] = {
val jsonFile = {
val tmpFile = File.createTempFile("upload", "tmp")
tmpFile.deleteOnExit()
Files.write(tmpFile.toPath, data)
tmpFile
}
val entity = MultipartEntityBuilder
.create()
.addPart(fileKey, new FileBody(jsonFile, fileContentType, jsonFile.getName))
.build()
val postReq = FakeRequest("POST", "ignored").withBody {
val outputStream = new ByteArrayOutputStream()
entity.writeTo(outputStream)
outputStream.toByteArray
}.withHeaders(CONTENT_TYPE -> entity.getContentType.getValue)
// override Play's equivalent Writeable so that the content-type header from the FakeRequest is used instead of application/octet-stream
call(action, postReq)(Writeable(identity, None))
}
it("should import a valid JSON file") {
import com.m3.octoparts.json.format.ConfigModel._
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
val jsonParts = Seq(HttpPartConfig.toJsonModel(part))
doReturn(Future.successful(Seq(part.partId))).when(repository).importConfigs(jsonParts)
val data = Json.toJson(jsonParts).toString()
val doImportParts = makeFileUpload(data.getBytes(StandardCharsets.UTF_8), adminController.doImportParts())
status(doImportParts) should be(FOUND)
redirectLocation(doImportParts).getOrElse(fail()) should include(routes.AdminController.listParts().url)
flash(doImportParts).get(BootstrapFlashStyles.danger.toString) should be(None)
flash(doImportParts).get(BootstrapFlashStyles.success.toString) shouldNot be(None)
verify(repository).importConfigs(jsonParts)
verifyNoMoreInteractions(repository)
}
it("could never import a broken JSON file") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
val doImportParts = makeFileUpload("INVALID JSON".getBytes(StandardCharsets.UTF_8), adminController.doImportParts())
status(doImportParts) should be(FOUND)
redirectLocation(doImportParts).getOrElse(fail()) should include(routes.AdminController.listParts().url)
flash(doImportParts).get(BootstrapFlashStyles.danger.toString) shouldNot be(None)
verifyNoMoreInteractions(repository)
}
}
describe("deleting a part") {
describe("when all is well with the world") {
it("should delete the part") {
val repository = mock[MutableConfigsRepository]
val cacheOps = mock[CacheOps]
when(cacheOps.increasePartVersion(anyString())).thenReturn(Future.successful(()))
val adminController = new AdminController(cacheOps = cacheOps, repository = repository)
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(1)).when(repository).deleteConfigByPartId(part.partId)
val deletePart = adminController.deletePart(part.partId)(FakeRequest())
whenReady(deletePart) { result =>
status(deletePart) should be(FOUND)
redirectLocation(deletePart).fold(fail())(_ should include(routes.AdminController.listParts().url))
verify(repository).findConfigByPartId(part.partId)
verify(repository).deleteConfigByPartId(part.partId)
verify(cacheOps, Mockito.timeout(1000)).increasePartVersion(part.partId)
}
}
}
describe("when the part does not exist") {
it("should redirect to the list view with an error message") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(None)).when(repository).findConfigByPartId("someOther")
val deletePart = adminController.deletePart("someOther")(FakeRequest())
whenReady(deletePart) { result =>
status(deletePart) should be(FOUND)
redirectLocation(deletePart).fold(fail())(_ should include(routes.AdminController.listParts.url))
flash(deletePart).get(BootstrapFlashStyles.danger.toString) should be('defined)
verify(repository).findConfigByPartId("someOther")
}
}
}
}
describe("when copying a part") {
describe("when all is well with the world") {
it("should copy the part and show the edit form for the newly created part") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(Seq(part))).when(repository).findAllConfigs()
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(124L)).when(repository).save(anyObject[HttpPartConfig]())(anyObject[ConfigMapper[HttpPartConfig]])
val result = adminController.copyPart(part.partId).apply(FakeRequest())
status(result) should be(FOUND)
val expectedNewPartId = part.partId + "_"
redirectLocation(result).get should include(routes.AdminController.editPart(expectedNewPartId).url)
}
}
describe("when the part does not exist") {
it("should redirect to the list view with an error message") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(None)).when(repository).findConfigByPartId("someOther")
val deletePart = adminController.copyPart("someOther")(FakeRequest())
whenReady(deletePart) { result =>
status(deletePart) should be(FOUND)
redirectLocation(deletePart).fold(fail())(_ should include(routes.AdminController.listParts.url))
flash(deletePart).get(BootstrapFlashStyles.danger.toString) should be('defined)
verify(repository).findConfigByPartId("someOther")
}
}
}
}
describe("when copying a part") {
describe("when all is well with the world") {
it("should update the part with a new param based on the found param") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(Some(mockPartParam))).when(repository).findParamById(mockPartParam.id.get)
doReturn(Future.successful(37L)).when(repository).save(anyObject[PartParam]())(anyObject[ConfigMapper[PartParam]])
val existingParam = part.parameters.head
val copyParam = adminController.copyParam(part.partId, mockPartParam.id.get)(FakeRequest())
whenReady(copyParam) { result =>
status(copyParam) should equal(FOUND)
redirectLocation(copyParam).fold(fail())(_ should include(routes.AdminController.showPart(part.partId).url))
verify(repository).findConfigByPartId(part.partId)
verify(repository).findParamById(mockPartParam.id.get)
val newCiCaptor = ArgumentCaptor.forClass(classOf[PartParam])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[PartParam]])
newCiCaptor.getValue.inputName should be(existingParam.inputName + "_")
newCiCaptor.getValue.outputName should be(existingParam.outputName + "_")
newCiCaptor.getValue.paramType should be(existingParam.paramType)
}
}
}
describe("when the part does not exist") {
it("should redirect to the part detail view with an error message") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(None)).when(repository).findParamById(anyLong())
val copyParam = adminController.copyParam(part.partId, 12345L)(FakeRequest())
whenReady(copyParam) { result =>
status(copyParam) should equal(FOUND)
redirectLocation(copyParam).fold(fail())(_ should include(routes.AdminController.showPart(part.partId).url))
verify(repository).findConfigByPartId(part.partId)
verify(repository).findParamById(12345)
}
}
}
}
it("should insert a new param") {
val repository = mock[MutableConfigsRepository]
val cacheOps = mock[CacheOps]
when(cacheOps.increasePartVersion(anyString())).thenReturn(Future.successful(()))
val adminController = new AdminController(cacheOps = cacheOps, repository = repository)
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(37L)).when(repository).save(anyObject[PartParam]())(anyObject[ConfigMapper[PartParam]])
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroupsByName(anyVararg[String]())
val createParam = adminController.createParam(part.partId)(
FakeRequest().withFormUrlEncodedBody("outputName" -> "someName", "paramType" -> "cookie", "required" -> "true")
)
whenReady(createParam) { result =>
status(createParam) should equal(FOUND)
redirectLocation(createParam).fold(fail())(_ should include(routes.AdminController.showPart(part.partId).url))
verify(repository).findConfigByPartId(part.partId)
val newCiCaptor = ArgumentCaptor.forClass(classOf[PartParam])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[PartParam]])
verify(repository).findAllCacheGroupsByName(Seq.empty: _*)
verify(cacheOps, Mockito.timeout(1000)).increasePartVersion(part.partId)
newCiCaptor.getValue.outputName should be("someName")
newCiCaptor.getValue.paramType should be(ParamType.Cookie)
}
}
it("should find and update an existing param") {
val repository = mock[MutableConfigsRepository]
val cacheOps = mock[CacheOps]
when(cacheOps.increasePartVersion(anyString())).thenReturn(Future.successful(()))
val adminController = new AdminController(cacheOps = cacheOps, repository = repository)
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(37L)).when(repository).save(anyObject[PartParam]())(anyObject[ConfigMapper[PartParam]])
doReturn(Future.successful(Some(mockPartParam))).when(repository).findParamById(mockPartParam.id.get)
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroupsByName(anyVararg[String]())
val updateParam = adminController.updateParam(part.partId, mockPartParam.id.get)(
FakeRequest().withFormUrlEncodedBody("outputName" -> "newName", "paramType" -> "body", "versioned" -> "true")
)
whenReady(updateParam) { result =>
status(updateParam) should equal(FOUND)
redirectLocation(updateParam).fold(fail())(_ should include(routes.AdminController.showPart(part.partId).url))
verify(repository).findConfigByPartId(part.partId)
val newCiCaptor = ArgumentCaptor.forClass(classOf[PartParam])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[PartParam]])
verify(repository).findAllCacheGroupsByName(Seq.empty: _*)
verify(cacheOps, Mockito.timeout(1000)).increasePartVersion(part.partId)
newCiCaptor.getValue.outputName should be("newName")
newCiCaptor.getValue.paramType should be(ParamType.Body)
}
}
it("should delete an existing param") {
val repository = mock[MutableConfigsRepository]
val cacheOps = mock[CacheOps]
when(cacheOps.increasePartVersion(anyString())).thenReturn(Future.successful(()))
val adminController = new AdminController(cacheOps = cacheOps, repository = repository)
when(repository.deletePartParamById(anyLong())).thenReturn(Future.successful(1))
doReturn(Future.successful(Some(part))).when(repository).findConfigByPartId(part.partId)
doReturn(Future.successful(Some(mockPartParam))).when(repository).findParamById(mockPartParam.id.get)
doReturn(Future.successful(Seq.empty)).when(repository).findAllCacheGroupsByName(anyVararg[String]())
val deleteParam = adminController.deleteParam(part.partId, mockPartParam.id.get)(FakeRequest())
status(deleteParam) should equal(FOUND)
redirectLocation(deleteParam).fold(fail())(_ should include(routes.AdminController.showPart(part.partId).url))
whenReady(deleteParam) { result =>
verify(repository).findConfigByPartId(part.partId)
val newCiCaptor = ArgumentCaptor.forClass(classOf[Long])
verify(repository).deletePartParamById(newCiCaptor.capture())
verify(cacheOps, Mockito.timeout(1000)).increasePartVersion(part.partId)
newCiCaptor.getValue should be(part.id.get)
}
}
it("should add a new Thread Pool Config") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(76L)).when(repository).save(anyObject[ThreadPoolConfig]())(anyObject[ConfigMapper[ThreadPoolConfig]])
val createThreadPool = adminController.createThreadPool(
FakeRequest().withFormUrlEncodedBody("threadPoolKey" -> "myNewThreadPool", "coreSize" -> "99")
)
whenReady(createThreadPool) { result =>
status(createThreadPool) should be(FOUND)
val newCiCaptor = ArgumentCaptor.forClass(classOf[ThreadPoolConfig])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[ThreadPoolConfig]])
newCiCaptor.getValue.threadPoolKey should be("myNewThreadPool")
newCiCaptor.getValue.coreSize should be(99)
}
}
it("should update a thread pool config") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(76L)).when(repository).save(anyObject[ThreadPoolConfig]())(anyObject[ConfigMapper[ThreadPoolConfig]])
val tpc = mockThreadConfig.copy(id = Some(123))
doReturn(Future.successful(Some(tpc))).when(repository).findThreadPoolConfigById(anyLong())
val updateThreadPool = adminController.updateThreadPool(123L)(FakeRequest().withFormUrlEncodedBody("threadPoolKey" -> "myNewThreadPool", "coreSize" -> "99"))
whenReady(updateThreadPool) { result =>
verify(repository).findThreadPoolConfigById(123L)
val newCiCaptor = ArgumentCaptor.forClass(classOf[ThreadPoolConfig])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[ThreadPoolConfig]])
newCiCaptor.getValue.threadPoolKey should be("myNewThreadPool")
newCiCaptor.getValue.coreSize should be(99)
newCiCaptor.getValue.createdAt should be(tpc.createdAt)
}
}
it("should add a new CacheGroup") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(76L)).when(repository).save(anyObject[CacheGroup]())(anyObject[ConfigMapper[CacheGroup]])
val createCacheGroup = adminController.createCacheGroup(FakeRequest().withFormUrlEncodedBody("name" -> "newCacheGroup", "description" -> "hello"))
whenReady(createCacheGroup) { result =>
val newCiCaptor = ArgumentCaptor.forClass(classOf[CacheGroup])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[CacheGroup]])
newCiCaptor.getValue.name should be("newCacheGroup")
newCiCaptor.getValue.description should be("hello")
}
}
it("should update a CacheGroup") {
val repository = mock[MutableConfigsRepository]
val adminController = new AdminController(cacheOps = DummyCacheOps, repository = repository)
doReturn(Future.successful(76L)).when(repository).save(anyObject[CacheGroup]())(anyObject[ConfigMapper[CacheGroup]])
doReturn(Future.successful(Seq(part))).when(repository).findAllConfigs()
val cacheGroup = mockCacheGroup.copy(id = Some(123))
doReturn(Future.successful(Some(cacheGroup))).when(repository).findCacheGroupByName(anyString())
val saveCacheGroup = adminController.updateCacheGroup("123")(FakeRequest().withFormUrlEncodedBody("name" -> "myEditedThreadPool", "description" -> "harooo"))
whenReady(saveCacheGroup) { result =>
verify(repository).findCacheGroupByName("123")
val newCiCaptor = ArgumentCaptor.forClass(classOf[CacheGroup])
verify(repository).save(newCiCaptor.capture())(anyObject[ConfigMapper[CacheGroup]])
newCiCaptor.getValue.name should be("myEditedThreadPool")
newCiCaptor.getValue.description should be("harooo")
newCiCaptor.getValue.createdAt should be(cacheGroup.createdAt)
}
}
}