From 25cd8c381d0de0d341a1d5333ba6f1eb9339c663 Mon Sep 17 00:00:00 2001 From: steven choi Date: Fri, 29 Sep 2023 15:44:30 +1000 Subject: [PATCH 1/7] #728 Add a feature to clear cache --- .../controllers/AdminController.js | 22 ++++++++++ grails-app/conf/application.yml | 3 ++ grails-app/conf/profile-ehcache.xml | 43 +++++++++++++++++++ .../ala/profile/hub/AdminController.groovy | 16 +++++++ .../au/org/ala/profile/hub/UrlMappings.groovy | 2 + grails-app/views/admin/admin.gsp | 10 +++++ 6 files changed, 96 insertions(+) create mode 100644 grails-app/conf/profile-ehcache.xml diff --git a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js index 1c76257a..69f7e684 100644 --- a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js +++ b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js @@ -35,11 +35,13 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer }; self.tags = []; + self.cacheRegions = []; loadBackupFileList(); loadOpusList(); loadPendingJobs(); loadTags(); + cacheManagement(); self.reloadHelpUrls = function() { var promise = $http.post(util.contextRoot() + "/admin/reloadHelpUrls"); @@ -225,4 +227,24 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer self.loadingTags = false; }); } + + function cacheManagement() { + self.loadingCacheManagement = true; + var promise = $http.get(util.contextRoot() + "/admin/cacheManagement/"); + promise.then(function (response) { + self.cacheRegions = response.data || []; + self.loadingCacheManagement = false; + }, function() { + self.loadingCacheManagement = false; + }); + } + + self.clearCache = function (cache) { + var promise = $http.get(util.contextRoot() + "/admin/clearCache/" + cache); + promise.then(function() { + messageService.success("Job clear cache for " + cache); + }, function() { + messageService.alert("Failed to clear cache the job"); + }); + } }); diff --git a/grails-app/conf/application.yml b/grails-app/conf/application.yml index e1bf4180..ec5e2f24 100644 --- a/grails-app/conf/application.yml +++ b/grails-app/conf/application.yml @@ -3,6 +3,9 @@ grails: profile: web codegen: defaultPackage: au.org.ala.profile.hub + cache: + ehcache: + ehcacheXmlLocation: 'classpath:profile-ehcache.xml' gorm: reactor: # Whether to translate GORM events into Reactor events diff --git a/grails-app/conf/profile-ehcache.xml b/grails-app/conf/profile-ehcache.xml new file mode 100644 index 00000000..82b9db6d --- /dev/null +++ b/grails-app/conf/profile-ehcache.xml @@ -0,0 +1,43 @@ + + + + + + + 1 + + + 2000 + + + + + + 1 + + + 2000 + + + + + + 1 + + + 2000 + + + + + + 1 + + + 20000 + + + + \ No newline at end of file diff --git a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy index b3c7a9a0..e01cf3dc 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy @@ -10,10 +10,12 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver import javax.validation.constraints.NotNull import static groovy.io.FileType.DIRECTORIES +import org.grails.plugin.cache.GrailsCacheManager class AdminController extends BaseController { WebService webService ProfileService profileService + GrailsCacheManager grailsCacheManager @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def index() { @@ -120,4 +122,18 @@ class AdminController extends BaseController { return grailsApplication.config.backupRestoreDir?: '/data/profile-service/backup/db' } + @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) + def cacheManagement() { + def cacheNames = grailsCacheManager.getCacheNames() + success cacheNames + } + + @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) + def clearCache() { + if (params.id) { + grailsCacheManager.getCache(params.id).clear() + } + render view: "admin.gsp" + } + } diff --git a/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy b/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy index 971755e6..156b2597 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy @@ -202,6 +202,8 @@ class UrlMappings { "/admin/job/$jobType/$jobId" controller: "admin", action: [DELETE: "deleteJob"] "/admin/job/" controller: "admin", action: [GET: "listPendingJobs"] "/admin/tag/$tagId?" controller: "admin", action: [GET: "getTag", PUT: "createTag", POST: "updateTag", DELETE: "deleteTag"] + "/admin/cacheManagement/" controller: "admin", action: [GET: "cacheManagement"] + "/admin/clearCache" controller: "admin", action: [GET: "clearCache"] "/admin/reloadHelpUrls" controller: "admin", action: [POST: "reloadHelpUrls"] "/admin" controller: "admin", action: [GET: "index"] "/alaAdmin/index" controller: "admin", action: [GET: "alaIndex"] diff --git a/grails-app/views/admin/admin.gsp b/grails-app/views/admin/admin.gsp index bb997897..23705c66 100644 --- a/grails-app/views/admin/admin.gsp +++ b/grails-app/views/admin/admin.gsp @@ -165,6 +165,16 @@

+
+

Caches Management

+
+ + + +
+
From 7fa17b943e849cc51791a918b3f260b21d3bb565 Mon Sep 17 00:00:00 2001 From: steven choi Date: Thu, 5 Oct 2023 14:22:23 +1100 Subject: [PATCH 2/7] #728 replace UI as table and remove unused import --- .../ala/profile/hub/AdminController.groovy | 4 ---- grails-app/views/admin/admin.gsp | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy index e01cf3dc..cf97c683 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy @@ -3,10 +3,7 @@ package au.org.ala.profile.hub import au.org.ala.profile.security.Role import au.org.ala.profile.security.Secured import au.org.ala.ws.service.WebService -import grails.converters.JSON -import grails.util.Environment import org.apache.http.entity.ContentType -import org.springframework.core.io.support.PathMatchingResourcePatternResolver import javax.validation.constraints.NotNull import static groovy.io.FileType.DIRECTORIES @@ -135,5 +132,4 @@ class AdminController extends BaseController { } render view: "admin.gsp" } - } diff --git a/grails-app/views/admin/admin.gsp b/grails-app/views/admin/admin.gsp index 23705c66..7535b22d 100644 --- a/grails-app/views/admin/admin.gsp +++ b/grails-app/views/admin/admin.gsp @@ -167,12 +167,19 @@

Caches Management

-
- - - +
+ + + + + + + + + + + +
Cache NameCache Clear
{{ cache }}
From a5cfb6e9f1fd1d80c518b1db4908d420003d2d5b Mon Sep 17 00:00:00 2001 From: steven choi Date: Wed, 11 Oct 2023 10:40:54 +1100 Subject: [PATCH 3/7] #728 fix as comments --- .../profileEditor/controllers/AdminController.js | 12 ++++++++---- .../au/org/ala/profile/hub/AdminController.groovy | 8 +++++++- .../au/org/ala/profile/hub/UrlMappings.groovy | 4 ++-- grails-app/views/admin/admin.gsp | 6 ++++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js index 69f7e684..9239bcd0 100644 --- a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js +++ b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js @@ -230,7 +230,7 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer function cacheManagement() { self.loadingCacheManagement = true; - var promise = $http.get(util.contextRoot() + "/admin/cacheManagement/"); + var promise = $http.get(util.contextRoot() + "/admin/cacheManagement"); promise.then(function (response) { self.cacheRegions = response.data || []; self.loadingCacheManagement = false; @@ -241,10 +241,14 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer self.clearCache = function (cache) { var promise = $http.get(util.contextRoot() + "/admin/clearCache/" + cache); - promise.then(function() { - messageService.success("Job clear cache for " + cache); + promise.then(function(response) { + if (response.data.error) { + messageService.alert(response.data.error); + } else { + messageService.success(response.data.resp); + } }, function() { - messageService.alert("Failed to clear cache the job"); + messageService.alert(response.data.error); }); } }); diff --git a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy index cf97c683..25fdbfe6 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy @@ -127,9 +127,15 @@ class AdminController extends BaseController { @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def clearCache() { + Map result = [:] if (params.id) { grailsCacheManager.getCache(params.id).clear() + result.resp = "Successfully cleared cache - " + params.id + result.statusCode = HttpStatus.SC_OK + } else { + result.error = "Failed to clear cache the job" + result.statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR } - render view: "admin.gsp" + success result } } diff --git a/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy b/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy index 156b2597..3d00d407 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy @@ -202,8 +202,8 @@ class UrlMappings { "/admin/job/$jobType/$jobId" controller: "admin", action: [DELETE: "deleteJob"] "/admin/job/" controller: "admin", action: [GET: "listPendingJobs"] "/admin/tag/$tagId?" controller: "admin", action: [GET: "getTag", PUT: "createTag", POST: "updateTag", DELETE: "deleteTag"] - "/admin/cacheManagement/" controller: "admin", action: [GET: "cacheManagement"] - "/admin/clearCache" controller: "admin", action: [GET: "clearCache"] + "/admin/cacheManagement" controller: "admin", action: [GET: "cacheManagement"] + "/admin/clearCache/$id" controller: "admin", action: [GET: "clearCache"] "/admin/reloadHelpUrls" controller: "admin", action: [POST: "reloadHelpUrls"] "/admin" controller: "admin", action: [GET: "index"] "/alaAdmin/index" controller: "admin", action: [GET: "alaIndex"] diff --git a/grails-app/views/admin/admin.gsp b/grails-app/views/admin/admin.gsp index 7535b22d..49db8f18 100644 --- a/grails-app/views/admin/admin.gsp +++ b/grails-app/views/admin/admin.gsp @@ -170,8 +170,10 @@
- - + + + + From c12a994c76b202919fee5cad5ae91fe1a3dee42c Mon Sep 17 00:00:00 2001 From: steven choi Date: Wed, 11 Oct 2023 15:43:19 +1100 Subject: [PATCH 4/7] #728 write test case --- .../profile/hub/AdminControllerSpec.groovy | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy diff --git a/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy new file mode 100644 index 00000000..c23f9592 --- /dev/null +++ b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy @@ -0,0 +1,31 @@ +package au.org.ala.profile.hub + +import grails.testing.web.controllers.ControllerUnitTest +import org.apache.http.HttpStatus +import spock.lang.Specification + +class AdminControllerSpec extends Specification implements ControllerUnitTest { + + AdminController mockAdminController + + def setup() { + mockAdminController = Mock(AdminController) + } + + def "cache management should display to the admin controller when admin mode"() { + when: + mockAdminController.cacheManagement() + + then: + assert response.status == HttpStatus.SC_OK + } + + def "clearCache() should return a 200 (OK_REQUEST) if id has been provided"() { + when: + params.id = "userDetailsCache" + mockAdminController.clearCache() + + then: + response.status == HttpStatus.SC_OK + } +} From 818b019f8cb5531e49e1d8a437dfd9bd491f18e0 Mon Sep 17 00:00:00 2001 From: steven choi Date: Wed, 11 Oct 2023 16:31:55 +1100 Subject: [PATCH 5/7] #728 add send error and HttpStatus --- .../controllers/au/org/ala/profile/hub/AdminController.groovy | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy index 25fdbfe6..49c63f9b 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy @@ -3,6 +3,7 @@ package au.org.ala.profile.hub import au.org.ala.profile.security.Role import au.org.ala.profile.security.Secured import au.org.ala.ws.service.WebService +import org.apache.http.HttpStatus import org.apache.http.entity.ContentType import javax.validation.constraints.NotNull @@ -132,10 +133,11 @@ class AdminController extends BaseController { grailsCacheManager.getCache(params.id).clear() result.resp = "Successfully cleared cache - " + params.id result.statusCode = HttpStatus.SC_OK + success result } else { result.error = "Failed to clear cache the job" result.statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR + sendError result } - success result } } From 906a6804520abca6770dfabb63168f593b44d64a Mon Sep 17 00:00:00 2001 From: steven choi Date: Tue, 17 Oct 2023 17:16:24 +1100 Subject: [PATCH 6/7] #728 code changes and fix test case --- .../controllers/AdminController.js | 2 +- .../ala/profile/hub/AdminController.groovy | 47 ++++++++++++++++--- .../profile/hub/AdminControllerSpec.groovy | 17 +++++-- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js index 9239bcd0..23760c92 100644 --- a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js +++ b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js @@ -248,7 +248,7 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer messageService.success(response.data.resp); } }, function() { - messageService.alert(response.data.error); + messageService.alert("Failed to clear cache the job"); }); } }); diff --git a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy index 49c63f9b..884070a8 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy @@ -3,6 +3,8 @@ package au.org.ala.profile.hub import au.org.ala.profile.security.Role import au.org.ala.profile.security.Secured import au.org.ala.ws.service.WebService +import net.sf.ehcache.Cache +import net.sf.ehcache.CacheManager import org.apache.http.HttpStatus import org.apache.http.entity.ContentType @@ -14,7 +16,11 @@ class AdminController extends BaseController { WebService webService ProfileService profileService GrailsCacheManager grailsCacheManager - + static long ONE_DAY_SECONDS = 86400 + static String USER_DETAILS_CACHE = "userDetailsCache" + static String USER_DETAILS_BY_ID_CACHE = "userDetailsByIdCache" + static String USER_LIST_CACHE = "userListCache" + static String VOCAB_LIST_CACHE = "vocabListCache" @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def index() { render view: "admin.gsp" @@ -122,22 +128,51 @@ class AdminController extends BaseController { @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def cacheManagement() { - def cacheNames = grailsCacheManager.getCacheNames() + def cacheNames + if (grailsCacheManager == null) { + cacheNames = getCacheManager().getCacheNames() + } else { + cacheNames = grailsCacheManager.getCacheNames() + } success cacheNames } + private static CacheManager getCacheManager() { + CacheManager manager = CacheManager.create() + Cache userDetailCache = new Cache(USER_DETAILS_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.ehcaches.get(USER_DETAILS_CACHE)) { + manager.addCache(userDetailCache) + } + Cache userDetailsByIdCache = new Cache(USER_DETAILS_BY_ID_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.getEhcache(USER_DETAILS_BY_ID_CACHE)) { + manager.addCache(userDetailsByIdCache) + } + Cache userListCache = new Cache(USER_LIST_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.getEhcache(USER_LIST_CACHE)) { + manager.addCache(userListCache) + } + Cache vocabListCache = new Cache(VOCAB_LIST_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.getEhcache(VOCAB_LIST_CACHE)) { + manager.addCache(vocabListCache) + } + return manager + } + @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def clearCache() { Map result = [:] if (params.id) { - grailsCacheManager.getCache(params.id).clear() + if (grailsCacheManager == null) { + getCacheManager().getCache(params.id).remove(params.id) + } else { + grailsCacheManager.getCache(params.id).clear() + } result.resp = "Successfully cleared cache - " + params.id result.statusCode = HttpStatus.SC_OK success result } else { - result.error = "Failed to clear cache the job" - result.statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR - sendError result + var message = "Failed to clear cache the job" + sendError(HttpStatus.SC_BAD_REQUEST, message ?:""); } } } diff --git a/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy index c23f9592..7c034429 100644 --- a/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy +++ b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy @@ -6,15 +6,15 @@ import spock.lang.Specification class AdminControllerSpec extends Specification implements ControllerUnitTest { - AdminController mockAdminController + AdminController controller def setup() { - mockAdminController = Mock(AdminController) + controller = new AdminController() } def "cache management should display to the admin controller when admin mode"() { when: - mockAdminController.cacheManagement() + controller.cacheManagement() then: assert response.status == HttpStatus.SC_OK @@ -23,9 +23,18 @@ class AdminControllerSpec extends Specification implements ControllerUnitTest Date: Wed, 18 Oct 2023 16:37:57 +1100 Subject: [PATCH 7/7] #728 code changes and fix test case --- .../ala/profile/hub/AdminController.groovy | 43 ++----------------- .../profile/hub/AdminControllerSpec.groovy | 17 ++++++-- 2 files changed, 17 insertions(+), 43 deletions(-) diff --git a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy index 884070a8..fa164a04 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy @@ -3,10 +3,9 @@ package au.org.ala.profile.hub import au.org.ala.profile.security.Role import au.org.ala.profile.security.Secured import au.org.ala.ws.service.WebService -import net.sf.ehcache.Cache -import net.sf.ehcache.CacheManager import org.apache.http.HttpStatus import org.apache.http.entity.ContentType +import org.springframework.cache.Cache import javax.validation.constraints.NotNull import static groovy.io.FileType.DIRECTORIES @@ -16,11 +15,6 @@ class AdminController extends BaseController { WebService webService ProfileService profileService GrailsCacheManager grailsCacheManager - static long ONE_DAY_SECONDS = 86400 - static String USER_DETAILS_CACHE = "userDetailsCache" - static String USER_DETAILS_BY_ID_CACHE = "userDetailsByIdCache" - static String USER_LIST_CACHE = "userListCache" - static String VOCAB_LIST_CACHE = "vocabListCache" @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def index() { render view: "admin.gsp" @@ -128,45 +122,16 @@ class AdminController extends BaseController { @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def cacheManagement() { - def cacheNames - if (grailsCacheManager == null) { - cacheNames = getCacheManager().getCacheNames() - } else { - cacheNames = grailsCacheManager.getCacheNames() - } + def cacheNames = grailsCacheManager.getCacheNames() success cacheNames } - private static CacheManager getCacheManager() { - CacheManager manager = CacheManager.create() - Cache userDetailCache = new Cache(USER_DETAILS_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) - if (!manager.ehcaches.get(USER_DETAILS_CACHE)) { - manager.addCache(userDetailCache) - } - Cache userDetailsByIdCache = new Cache(USER_DETAILS_BY_ID_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) - if (!manager.getEhcache(USER_DETAILS_BY_ID_CACHE)) { - manager.addCache(userDetailsByIdCache) - } - Cache userListCache = new Cache(USER_LIST_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) - if (!manager.getEhcache(USER_LIST_CACHE)) { - manager.addCache(userListCache) - } - Cache vocabListCache = new Cache(VOCAB_LIST_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) - if (!manager.getEhcache(VOCAB_LIST_CACHE)) { - manager.addCache(vocabListCache) - } - return manager - } - @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def clearCache() { Map result = [:] if (params.id) { - if (grailsCacheManager == null) { - getCacheManager().getCache(params.id).remove(params.id) - } else { - grailsCacheManager.getCache(params.id).clear() - } + Cache cache = grailsCacheManager.getCache(params.id) + cache.clear() result.resp = "Successfully cleared cache - " + params.id result.statusCode = HttpStatus.SC_OK success result diff --git a/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy index 7c034429..cb62b0ac 100644 --- a/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy +++ b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy @@ -2,31 +2,39 @@ package au.org.ala.profile.hub import grails.testing.web.controllers.ControllerUnitTest import org.apache.http.HttpStatus +import org.grails.plugin.cache.GrailsCacheManager +import org.springframework.cache.Cache import spock.lang.Specification class AdminControllerSpec extends Specification implements ControllerUnitTest { - AdminController controller - def setup() { - controller = new AdminController() + controller.grailsCacheManager = Mock(GrailsCacheManager) } def "cache management should display to the admin controller when admin mode"() { when: + controller.grailsCacheManager.getCacheNames() >> ["Cache1","Cache2"] controller.cacheManagement() then: - assert response.status == HttpStatus.SC_OK + def cacheNames = '["Cache1","Cache2"]' + + assert response.getContentAsString() == cacheNames } def "clearCache() should return a 200 (OK_REQUEST) if id has been provided"() { when: params.id = "userDetailsCache" + + Cache cache = Mock(Cache.class); + controller.grailsCacheManager.getCache(params.id) >> cache controller.clearCache() then: response.status == HttpStatus.SC_OK + def successMessage = '{"resp":"Successfully cleared cache - userDetailsCache","statusCode":200}' + assert response.getContentAsString() == successMessage } def "clearCache() should return a 400 (BAD_REQUEST) if id has not been provided"() { @@ -36,5 +44,6 @@ class AdminControllerSpec extends Specification implements ControllerUnitTest
Cache NameCache Clear
Cache NameActions