Skip to content

Commit

Permalink
(test) evict caches before each test
Browse files Browse the repository at this point in the history
(cherry picked from commit e2b455c)
  • Loading branch information
jonathanlermitage committed Mar 18, 2018
1 parent a911f6a commit 7e26822
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/manon/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import manon.app.info.service.InfoService;
import manon.app.stats.service.PerformanceRecorder;
import manon.app.trace.service.AppTraceService;
import manon.user.UserExistsException;
Expand All @@ -11,7 +12,6 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
Expand All @@ -22,7 +22,6 @@
import java.util.List;

import static manon.app.config.SpringProfiles.METRICS;
import static manon.app.info.service.InfoServiceImpl.CACHE_GET_APPVERSION;
import static manon.app.trace.document.AppTrace.Event.APP_START;
import static manon.app.trace.document.AppTrace.Level.INFO;

Expand All @@ -38,6 +37,7 @@ public class Application extends SpringBootServletInitializer {
private final AppTraceService appTraceService;
private final Environment env;
private final PerformanceRecorder performanceRecorder;
private final InfoService infoService;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
Expand All @@ -49,8 +49,8 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
}

@PostConstruct
@CacheEvict(value = CACHE_GET_APPVERSION, allEntries = true)
public void initApp() throws UserExistsException {
infoService.evictCaches();
String initAppEvent = "Admin username is " + userAdminService.ensureAdmin().getUsername();
appTraceService.log(INFO, APP_START, initAppEvent);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/manon/app/info/service/InfoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

public interface InfoService {

void evictCaches();

String getAppVersion();
}
9 changes: 9 additions & 0 deletions src/main/java/manon/app/info/service/InfoServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package manon.app.info.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@PropertySource(value = "classpath:info.properties")
@Slf4j
public class InfoServiceImpl implements InfoService {

public static final String CACHE_GET_APPVERSION = "CACHE_GET_APPVERSION";

@Value("${version}")
private String version;

@CacheEvict(value = {CACHE_GET_APPVERSION}, allEntries = true)
@Override
public void evictCaches() {
log.debug("evict all entries of cache " + CACHE_GET_APPVERSION);
}

@Cacheable(CACHE_GET_APPVERSION)
@Override
public String getAppVersion() {
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/manon/util/basetest/InitBeforeClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import manon.Application;
import manon.app.info.service.InfoService;
import manon.user.UserExistsException;
import manon.user.document.User;
import manon.user.registration.service.RegistrationService;
Expand Down Expand Up @@ -55,6 +56,8 @@ public abstract class InitBeforeClass extends BaseTests {
protected UserAdminService userAdminService;
@Autowired
protected RegistrationService registrationService;
@Autowired
protected InfoService infoService;

@Autowired
private MongoTemplate mongoTemplate;
Expand All @@ -81,6 +84,7 @@ public void beforeMethod() throws Exception {
initDb();
initialized = true;
}
infoService.evictCaches();
}

public void clearDb() {
Expand Down

0 comments on commit 7e26822

Please sign in to comment.