Skip to content

Commit

Permalink
#728 code changes and fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
schoicsiro committed Oct 18, 2023
1 parent 906a680 commit 53bfa1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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> {

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"() {
Expand All @@ -36,5 +44,6 @@ class AdminControllerSpec extends Specification implements ControllerUnitTest<Ad

then:
response.status != HttpStatus.SC_OK
assert response.errorMessage == "Failed to clear cache the job"
}
}

0 comments on commit 53bfa1f

Please sign in to comment.