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 17, 2023
1 parent 818b019 commit 906a680
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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 ?:"");
}
}
}
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 @@ -6,15 +6,15 @@ import spock.lang.Specification

class AdminControllerSpec extends Specification implements ControllerUnitTest<AdminController> {

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
Expand All @@ -23,9 +23,18 @@ class AdminControllerSpec extends Specification implements ControllerUnitTest<Ad
def "clearCache() should return a 200 (OK_REQUEST) if id has been provided"() {
when:
params.id = "userDetailsCache"
mockAdminController.clearCache()
controller.clearCache()

then:
response.status == HttpStatus.SC_OK
}

def "clearCache() should return a 400 (BAD_REQUEST) if id has not been provided"() {
when:
params.id = ""
controller.clearCache()

then:
response.status != HttpStatus.SC_OK
}
}

0 comments on commit 906a680

Please sign in to comment.