Deprecate plugin Change-Id: I356345b6f48038691ed90fa83c3f35baf74abc11
diff --git a/BUCK b/BUCK deleted file mode 100644 index acdb2af..0000000 --- a/BUCK +++ /dev/null
@@ -1,88 +0,0 @@ -include_defs('//bucklets/gerrit_plugin.bucklet') -include_defs('//bucklets/java_sources.bucklet') -include_defs('//bucklets/maven_jar.bucklet') - -SOURCES = glob(['src/main/java/**/*.java']) -RESOURCES = glob(['src/main/resources/**/*']) - -DEPS = [ - ':wiremock', -] - -PROVIDED_DEPS = GERRIT_TESTS + [ - '//lib:gson', -] - -TEST_DEPS = GERRIT_PLUGIN_API + PROVIDED_DEPS + DEPS + [ - ':evict-cache__plugin', - ':mockito', -] - -gerrit_plugin( - name = 'evict-cache', - srcs = SOURCES, - resources = RESOURCES, - manifest_entries = [ - 'Gerrit-PluginName: evict-cache', - 'Gerrit-ApiType: plugin', - 'Gerrit-Module: com.ericsson.gerrit.plugins.evictcache.Module', - 'Gerrit-HttpModule: com.ericsson.gerrit.plugins.evictcache.HttpModule', - 'Implementation-Title: evict-cache plugin', - 'Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/evict-cache', - 'Implementation-Vendor: Ericsson', - ], - provided_deps = PROVIDED_DEPS, - deps = DEPS, -) - -java_sources( - name = 'evict-cache-sources', - srcs = SOURCES + RESOURCES, -) - -java_library( - name = 'classpath', - deps = TEST_DEPS, -) - -java_test( - name = 'evict-cache_tests', - srcs = glob(['src/test/java/**/*.java']), - labels = ['evict-cache'], - deps = TEST_DEPS, -) - -maven_jar( - name = 'wiremock', - id = 'com.github.tomakehurst:wiremock:1.58:standalone', - sha1 = '21c8386a95c5dc54a9c55839c5a95083e42412ae', - license = 'Apache2.0', - attach_source = False, -) - -maven_jar( - name = 'mockito', - id = 'org.mockito:mockito-core:2.5.0', - sha1 = 'be28d46a52c7f2563580adeca350145e9ce916f8', - license = 'MIT', - deps = [ - ':byte-buddy', - ':objenesis', - ], -) - -maven_jar( - name = 'byte-buddy', - id = 'net.bytebuddy:byte-buddy:1.5.12', - sha1 = 'b1ba1d15f102b36ed43b826488114678d6d413da', - license = 'DO_NOT_DISTRIBUTE', - attach_source = False, -) - -maven_jar( - name = 'objenesis', - id = 'org.objenesis:objenesis:2.4', - sha1 = '2916b6c96b50c5b3ec4452ed99401db745aabb27', - license = 'DO_NOT_DISTRIBUTE', - attach_source = False, -)
diff --git a/README-THIS-REPO-IS-DEPRECATED.txt b/README-THIS-REPO-IS-DEPRECATED.txt new file mode 100644 index 0000000..4579684 --- /dev/null +++ b/README-THIS-REPO-IS-DEPRECATED.txt
@@ -0,0 +1,3 @@ +As of branch 2.13, this plugin is deprecated. The functionality has been merged into + +https://gerrit-review.googlesource.com/#/admin/projects/plugins/high-availability
diff --git a/lib/BUCK b/lib/BUCK deleted file mode 100644 index 8892994..0000000 --- a/lib/BUCK +++ /dev/null
@@ -1,8 +0,0 @@ -include_defs('//bucklets/maven_jar.bucklet') - -maven_jar( - name = 'gson', - id = 'com.google.code.gson:gson:2.1', - sha1 = '2e66da15851f9f5b5079228f856c2f090ba98c38', - license = 'Apache2.0', -)
diff --git a/lib/gerrit/BUCK b/lib/gerrit/BUCK deleted file mode 100644 index f6c68a4..0000000 --- a/lib/gerrit/BUCK +++ /dev/null
@@ -1,22 +0,0 @@ -include_defs('//bucklets/maven_jar.bucklet') - -VER = '2.13.3' -REPO = MAVEN_CENTRAL - -maven_jar( - name = 'plugin-api', - id = 'com.google.gerrit:gerrit-plugin-api:' + VER, - sha1 = '78df190269b0d5a4bc4f61ab3a66a49252b779eb', - license = 'Apache2.0', - attach_source = False, - repository = REPO, -) - -maven_jar( - name = 'acceptance-framework', - id = 'com.google.gerrit:gerrit-acceptance-framework:' + VER, - sha1 = '76eceefa7e31c6945513c36c4d106066d1df3e63', - license = 'Apache2.0', - attach_source = False, - repository = REPO, -)
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/CacheResponseHandler.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/CacheResponseHandler.java deleted file mode 100644 index 768ee72..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/CacheResponseHandler.java +++ /dev/null
@@ -1,74 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT; - -import com.ericsson.gerrit.plugins.evictcache.CacheResponseHandler.CacheResult; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.ResponseHandler; -import org.apache.http.util.EntityUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; - -class CacheResponseHandler implements ResponseHandler<CacheResult> { - - static class CacheResult { - private boolean successful; - private String message; - - CacheResult(boolean successful, String message) { - this.successful = successful; - this.message = message; - } - - boolean isSuccessful() { - return successful; - } - - String getMessage() { - return message; - } - } - - private static final Logger log = - LoggerFactory.getLogger(CacheResponseHandler.class); - - @Override - public CacheResult handleResponse(HttpResponse response) { - return new CacheResult(isSuccessful(response), parseResponse(response)); - } - - private boolean isSuccessful(HttpResponse response) { - return response.getStatusLine().getStatusCode() == SC_NO_CONTENT; - } - - private String parseResponse(HttpResponse response) { - HttpEntity entity = response.getEntity(); - String asString = ""; - if (entity != null) { - try { - asString = EntityUtils.toString(entity); - } catch (IOException e) { - log.error("Error parsing entity", e); - } - } - return asString; - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/Configuration.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/Configuration.java deleted file mode 100644 index e342f88..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/Configuration.java +++ /dev/null
@@ -1,86 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.common.base.CharMatcher; -import com.google.common.base.Strings; -import com.google.gerrit.extensions.annotations.PluginName; -import com.google.gerrit.server.config.PluginConfig; -import com.google.gerrit.server.config.PluginConfigFactory; -import com.google.inject.Inject; -import com.google.inject.Singleton; - -@Singleton -class Configuration { - private static final int DEFAULT_TIMEOUT_MS = 5000; - private static final int DEFAULT_MAX_TRIES = 5; - private static final int DEFAULT_RETRY_INTERVAL = 1000; - private static final int DEFAULT_THREAD_POOL_SIZE = 1; - - private final String url; - private final String user; - private final String password; - private final int connectionTimeout; - private final int socketTimeout; - private final int maxTries; - private final int retryInterval; - private final int threadPoolSize; - - @Inject - Configuration(PluginConfigFactory config, - @PluginName String pluginName) { - PluginConfig cfg = config.getFromGerritConfig(pluginName, true); - url = Strings.nullToEmpty(cfg.getString("url")); - user = Strings.nullToEmpty(cfg.getString("user")); - password = Strings.nullToEmpty(cfg.getString("password")); - connectionTimeout = cfg.getInt("connectionTimeout", DEFAULT_TIMEOUT_MS); - socketTimeout = cfg.getInt("socketTimeout", DEFAULT_TIMEOUT_MS); - maxTries = cfg.getInt("maxTries", DEFAULT_MAX_TRIES); - retryInterval = cfg.getInt("retryInterval", DEFAULT_RETRY_INTERVAL); - threadPoolSize = cfg.getInt("threadPoolSize", DEFAULT_THREAD_POOL_SIZE); - } - - int getConnectionTimeout() { - return connectionTimeout; - } - - int getMaxTries() { - return maxTries; - } - - int getRetryInterval() { - return retryInterval; - } - - int getSocketTimeout() { - return socketTimeout; - } - - String getUrl() { - return CharMatcher.is('/').trimTrailingFrom(url); - } - - String getUser() { - return user; - } - - String getPassword() { - return password; - } - - int getThreadPoolSize() { - return threadPoolSize; - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/Context.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/Context.java deleted file mode 100644 index 0b0519b..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/Context.java +++ /dev/null
@@ -1,43 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -/** - * Allows to tag a forwarded event to avoid infinitely looping events. - */ -class Context { - private static final ThreadLocal<Boolean> FORWARDED_EVENT = - new ThreadLocal<Boolean>() { - @Override - protected Boolean initialValue() { - return false; - } - }; - - private Context() { - } - - static Boolean isForwardedEvent() { - return FORWARDED_EVENT.get(); - } - - static void setForwardedEvent() { - FORWARDED_EVENT.set(true); - } - - static void unsetForwardedEvent() { - FORWARDED_EVENT.remove(); - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutor.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutor.java deleted file mode 100644 index 8106f55..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutor.java +++ /dev/null
@@ -1,26 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import com.google.inject.BindingAnnotation; - -import java.lang.annotation.Retention; - -@Retention(RUNTIME) -@BindingAnnotation -@interface EvictCacheExecutor { -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutorProvider.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutorProvider.java deleted file mode 100644 index e1de403..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutorProvider.java +++ /dev/null
@@ -1,55 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.gerrit.extensions.annotations.PluginName; -import com.google.gerrit.extensions.events.LifecycleListener; -import com.google.gerrit.server.git.WorkQueue; -import com.google.inject.Inject; -import com.google.inject.Provider; -import com.google.inject.Singleton; - -import java.util.concurrent.ScheduledThreadPoolExecutor; - -@Singleton -class EvictCacheExecutorProvider - implements Provider<ScheduledThreadPoolExecutor>, LifecycleListener { - private WorkQueue.Executor executor; - - @Inject - EvictCacheExecutorProvider(WorkQueue workQueue, - @PluginName String pluginName, - Configuration config) { - executor = workQueue.createQueue(config.getThreadPoolSize(), - "Evict cache [" + pluginName + " plugin]"); - } - - @Override - public void start() { - // do nothing - } - - @Override - public void stop() { - executor.shutdown(); - executor.unregisterWorkQueue(); - executor = null; - } - - @Override - public ScheduledThreadPoolExecutor get() { - return executor; - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheHandler.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheHandler.java deleted file mode 100644 index fe8dc3a..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheHandler.java +++ /dev/null
@@ -1,74 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.common.cache.RemovalNotification; -import com.google.gerrit.server.cache.CacheRemovalListener; -import com.google.inject.Inject; - -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.regex.Pattern; - -class EvictCacheHandler<K, V> implements CacheRemovalListener<K, V> { - private final ScheduledThreadPoolExecutor executor; - private final RestSession restSession; - private final Pattern pattern; - - @Inject - EvictCacheHandler(RestSession restSession, - @EvictCacheExecutor ScheduledThreadPoolExecutor executor) { - this.restSession = restSession; - this.executor = executor; - pattern = Pattern.compile( - "^accounts.*|^groups.*|ldap_groups|ldap_usernames|^project.*|sshkeys|web_sessions"); - } - - @Override - public void onRemoval(String pluginName, String cacheName, - RemovalNotification<K, V> notification) { - if (!Context.isForwardedEvent() && !notification.wasEvicted() - && isSynchronized(cacheName)) { - executor.execute( - new EvictCacheTask(pluginName, cacheName, notification.getKey())); - } - } - - private boolean isSynchronized(String cacheName) { - return pattern.matcher(cacheName).matches(); - } - - class EvictCacheTask implements Runnable { - private String pluginName; - private String cacheName; - private Object key; - - EvictCacheTask(String pluginName, String cacheName, Object key) { - this.pluginName = pluginName; - this.cacheName = cacheName; - this.key = key; - } - - @Override - public void run() { - restSession.evict(pluginName, cacheName, key); - } - - @Override - public String toString() { - return String.format("Evict key '%s' from cache '%s' in target instance", - key, cacheName); - } - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheRestApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheRestApiServlet.java deleted file mode 100644 index bb4f0ba..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheRestApiServlet.java +++ /dev/null
@@ -1,93 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; -import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT; - -import com.google.common.base.Splitter; -import com.google.common.cache.Cache; -import com.google.gerrit.extensions.registration.DynamicMap; -import com.google.inject.Inject; -import com.google.inject.Singleton; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.List; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -@Singleton -class EvictCacheRestApiServlet extends HttpServlet { - private static final int CACHENAME_INDEX = 1; - private static final long serialVersionUID = -1L; - private static final String SOURCE_NAME = "gerrit"; - private static final String PROJECT_LIST = "project_list"; - private static final Logger logger = - LoggerFactory.getLogger(EvictCacheRestApiServlet.class); - - private final DynamicMap<Cache<?, ?>> cacheMap; - - @Inject - EvictCacheRestApiServlet(DynamicMap<Cache<?, ?>> cacheMap) { - this.cacheMap = cacheMap; - } - - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse rsp) - throws IOException, ServletException { - rsp.setContentType("text/plain"); - rsp.setCharacterEncoding("UTF-8"); - try { - List<String> params = Splitter.on('/').splitToList(req.getPathInfo()); - String cacheName = params.get(CACHENAME_INDEX); - String json = req.getReader().readLine(); - Object key = GsonParser.fromJson(cacheName, json); - Cache<?, ?> cache = cacheMap.get(SOURCE_NAME, cacheName); - Context.setForwardedEvent(); - evictCache(cache, cacheName, key); - rsp.setStatus(SC_NO_CONTENT); - } catch (IOException e) { - logger.error("Failed to process eviction request: " + e.getMessage(), e); - sendError(rsp, SC_BAD_REQUEST, e.getMessage()); - } finally { - Context.unsetForwardedEvent(); - } - } - - private static void sendError(HttpServletResponse rsp, int statusCode, - String message) { - try { - rsp.sendError(statusCode, message); - } catch (IOException e) { - logger.error("Failed to send error messsage: " + e.getMessage(), e); - } - } - - private void evictCache(Cache<?, ?> cache, String cacheName, Object key) { - if (PROJECT_LIST.equals(cacheName)) { - // One key is holding the list of projects - cache.invalidateAll(); - } else { - cache.invalidate(key); - } - logger.debug("Invalidated " + cacheName); - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/GsonParser.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/GsonParser.java deleted file mode 100644 index d752c06..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/GsonParser.java +++ /dev/null
@@ -1,80 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.common.base.Strings; -import com.google.gerrit.reviewdb.client.Account; -import com.google.gerrit.reviewdb.client.AccountGroup; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -final class GsonParser { - private static final String ACCOUNTS = "accounts"; - private static final String GROUPS = "groups"; - private static final String GROUPS_BYINCLUDE = "groups_byinclude"; - private static final String GROUPS_MEMBERS = "groups_members"; - private static final String PROJECT_LIST = "project_list"; - - private GsonParser() { - } - - static Object fromJson(String cacheName, String json) { - Gson gson = new GsonBuilder().create(); - Object key; - // Need to add a case for 'adv_bases' - switch (cacheName) { - case ACCOUNTS: - key = gson.fromJson(Strings.nullToEmpty(json).trim(), Account.Id.class); - break; - case GROUPS: - key = gson.fromJson(Strings.nullToEmpty(json).trim(), - AccountGroup.Id.class); - break; - case GROUPS_BYINCLUDE: - case GROUPS_MEMBERS: - key = gson.fromJson(Strings.nullToEmpty(json).trim(), - AccountGroup.UUID.class); - break; - case PROJECT_LIST: - key = gson.fromJson(Strings.nullToEmpty(json), Object.class); - break; - default: - key = gson.fromJson(Strings.nullToEmpty(json).trim(), String.class); - } - return key; - } - - static String toJson(String cacheName, Object key) { - Gson gson = new GsonBuilder().create(); - String json; - // Need to add a case for 'adv_bases' - switch (cacheName) { - case ACCOUNTS: - json = gson.toJson(key, Account.Id.class); - break; - case GROUPS: - json = gson.toJson(key, AccountGroup.Id.class); - break; - case GROUPS_BYINCLUDE: - case GROUPS_MEMBERS: - json = gson.toJson(key, AccountGroup.UUID.class); - break; - case PROJECT_LIST: - default: - json = gson.toJson(key); - } - return json; - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpClientProvider.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpClientProvider.java deleted file mode 100644 index df42800..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpClientProvider.java +++ /dev/null
@@ -1,206 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.inject.Inject; -import com.google.inject.Provider; -import com.google.inject.Singleton; - -import org.apache.http.HttpResponse; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.HttpRequestRetryHandler; -import org.apache.http.client.ServiceUnavailableRetryStrategy; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.config.Registry; -import org.apache.http.config.RegistryBuilder; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.conn.socket.ConnectionSocketFactory; -import org.apache.http.conn.socket.PlainConnectionSocketFactory; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.apache.http.protocol.HttpContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.URI; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.X509Certificate; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLException; -import javax.net.ssl.SSLSession; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -/** - * Provides an HTTP client with SSL capabilities. - */ -@Singleton -class HttpClientProvider implements Provider<CloseableHttpClient> { - private static final Logger log = - LoggerFactory.getLogger(HttpClientProvider.class); - private static final int CONNECTIONS_PER_ROUTE = 100; - // Up to 2 target instances with the max number of connections per host: - private static final int MAX_CONNECTIONS = 2 * CONNECTIONS_PER_ROUTE; - - private static final int ERROR_CODES = 500; - - private static final int MAX_CONNECTION_INACTIVITY = 10000; - - private final Configuration cfg; - private final SSLConnectionSocketFactory sslSocketFactory; - - @Inject - HttpClientProvider(Configuration cfg) { - this.cfg = cfg; - this.sslSocketFactory = buildSslSocketFactory(); - } - - @Override - public CloseableHttpClient get() { - return HttpClients.custom().setSSLSocketFactory(sslSocketFactory) - .setConnectionManager(customConnectionManager()) - .setDefaultCredentialsProvider(buildCredentials()) - .setDefaultRequestConfig(customRequestConfig()) - .setRetryHandler(customRetryHandler()) - .setServiceUnavailableRetryStrategy(customServiceUnavailRetryStrategy()) - .build(); - } - - private RequestConfig customRequestConfig() { - return RequestConfig.custom().setConnectTimeout(cfg.getConnectionTimeout()) - .setSocketTimeout(cfg.getSocketTimeout()) - .setConnectionRequestTimeout(cfg.getConnectionTimeout()) - .build(); - } - - private HttpRequestRetryHandler customRetryHandler() { - return new HttpRequestRetryHandler() { - - @Override - public boolean retryRequest(IOException exception, int executionCount, - HttpContext context) { - if (executionCount > cfg.getMaxTries() - || exception instanceof SSLException) { - return false; - } - logRetry(exception.getMessage()); - try { - Thread.sleep(cfg.getRetryInterval()); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - return true; - } - }; - } - - private ServiceUnavailableRetryStrategy customServiceUnavailRetryStrategy() { - return new ServiceUnavailableRetryStrategy() { - @Override - public boolean retryRequest(HttpResponse response, int executionCount, - HttpContext context) { - if (executionCount > cfg.getMaxTries()) { - return false; - } - if (response.getStatusLine().getStatusCode() >= ERROR_CODES) { - logRetry(response.getStatusLine().getReasonPhrase()); - return true; - } - return false; - } - - @Override - public long getRetryInterval() { - return cfg.getRetryInterval(); - } - }; - } - - private void logRetry(String cause) { - log.warn("Retrying request to '" + cfg.getUrl() + "' Cause: " + cause); - } - - private HttpClientConnectionManager customConnectionManager() { - Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder - .<ConnectionSocketFactory> create().register("https", sslSocketFactory) - .register("http", PlainConnectionSocketFactory.INSTANCE).build(); - PoolingHttpClientConnectionManager connManager = - new PoolingHttpClientConnectionManager(socketFactoryRegistry); - connManager.setDefaultMaxPerRoute(CONNECTIONS_PER_ROUTE); - connManager.setMaxTotal(MAX_CONNECTIONS); - connManager.setValidateAfterInactivity(MAX_CONNECTION_INACTIVITY); - return connManager; - } - - private SSLConnectionSocketFactory buildSslSocketFactory() { - return new SSLConnectionSocketFactory(buildSslContext(), - new DummyHostnameVerifier()); - } - - private SSLContext buildSslContext() { - try { - TrustManager[] trustAllCerts = - new TrustManager[] {new DummyX509TrustManager()}; - SSLContext context = SSLContext.getInstance("TLS"); - context.init(null, trustAllCerts, null); - return context; - } catch (KeyManagementException | NoSuchAlgorithmException e) { - log.warn("Error building SSLContext object", e); - return null; - } - } - - private BasicCredentialsProvider buildCredentials() { - URI uri = URI.create(cfg.getUrl()); - BasicCredentialsProvider creds = new BasicCredentialsProvider(); - creds.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), - new UsernamePasswordCredentials(cfg.getUser(), cfg.getPassword())); - return creds; - } - - private static class DummyX509TrustManager implements X509TrustManager { - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - - @Override - public void checkClientTrusted(X509Certificate[] certs, String authType) { - // no check - } - - @Override - public void checkServerTrusted(X509Certificate[] certs, String authType) { - // no check - } - } - - private static class DummyHostnameVerifier implements HostnameVerifier { - @Override - public boolean verify(String hostname, SSLSession session) { - // always accept - return true; - } - } -} \ No newline at end of file
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpModule.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpModule.java deleted file mode 100644 index 27cf205..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpModule.java +++ /dev/null
@@ -1,24 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.gerrit.httpd.plugins.HttpPluginModule; - -class HttpModule extends HttpPluginModule { - @Override - protected void configureServlets() { - serve("/gerrit/evict/*").with(EvictCacheRestApiServlet.class); - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpSession.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpSession.java deleted file mode 100644 index 35fbd65..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/HttpSession.java +++ /dev/null
@@ -1,47 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.common.net.MediaType; -import com.google.inject.Inject; - -import com.ericsson.gerrit.plugins.evictcache.CacheResponseHandler.CacheResult; - -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.IOException; - -class HttpSession { - private final CloseableHttpClient httpClient; - private final String url; - - @Inject - HttpSession(CloseableHttpClient httpClient, - @SyncUrl String url) { - this.httpClient = httpClient; - this.url = url; - } - - CacheResult post(String endpoint, String json) throws IOException { - HttpPost post = new HttpPost(url + endpoint); - StringEntity params = new StringEntity(json.trim()); - post.addHeader("Content-Type", MediaType.JSON_UTF_8.toString()); - post.addHeader("Accept", MediaType.JSON_UTF_8.toString()); - post.setEntity(params); - return httpClient.execute(post, new CacheResponseHandler()); - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/Module.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/Module.java deleted file mode 100644 index 28ba756..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/Module.java +++ /dev/null
@@ -1,49 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.gerrit.extensions.registration.DynamicSet; -import com.google.gerrit.lifecycle.LifecycleModule; -import com.google.gerrit.server.cache.CacheRemovalListener; -import com.google.inject.Provides; -import com.google.inject.Scopes; - -import org.apache.http.impl.client.CloseableHttpClient; - -import java.util.concurrent.ScheduledThreadPoolExecutor; - -class Module extends LifecycleModule { - - @Override - protected void configure() { - bind(CloseableHttpClient.class).toProvider(HttpClientProvider.class) - .in(Scopes.SINGLETON); - bind(Configuration.class); - bind(HttpSession.class); - bind(RestSession.class); - bind(ScheduledThreadPoolExecutor.class) - .annotatedWith(EvictCacheExecutor.class) - .toProvider(EvictCacheExecutorProvider.class); - listener().to(EvictCacheExecutorProvider.class); - DynamicSet.bind(binder(), CacheRemovalListener.class).to( - EvictCacheHandler.class); - } - - @Provides - @SyncUrl - String syncUrl(Configuration config) { - return config.getUrl(); - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/RestSession.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/RestSession.java deleted file mode 100644 index 1295ce2..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/RestSession.java +++ /dev/null
@@ -1,56 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import com.google.common.base.Joiner; -import com.google.gerrit.extensions.annotations.PluginName; -import com.google.inject.Inject; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; - -class RestSession { - private static final Logger log = LoggerFactory.getLogger(RestSession.class); - private static final String EVICT = "evict"; - private final HttpSession httpSession; - private final String pluginName; - - @Inject - RestSession(HttpSession httpClient, - @PluginName String pluginName) { - this.httpSession = httpClient; - this.pluginName = pluginName; - } - - boolean evict(String sourceName, String cacheName, Object key) { - try { - String json = GsonParser.toJson(cacheName, key); - String buildEndpoint = buildEndpoint(pluginName, sourceName, cacheName); - return httpSession - .post(buildEndpoint, json) - .isSuccessful(); - } catch (IOException e) { - log.error("Error trying to evict for cache " + cacheName, e); - return false; - } - } - - private String buildEndpoint(String pluginName, String sourceName, - String cacheName) { - return Joiner.on("/").join("/plugins", pluginName, sourceName, EVICT, cacheName); - } -}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/evictcache/SyncUrl.java b/src/main/java/com/ericsson/gerrit/plugins/evictcache/SyncUrl.java deleted file mode 100644 index 14c2880..0000000 --- a/src/main/java/com/ericsson/gerrit/plugins/evictcache/SyncUrl.java +++ /dev/null
@@ -1,26 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import com.google.inject.BindingAnnotation; - -import java.lang.annotation.Retention; - -@Retention(RUNTIME) -@BindingAnnotation -@interface SyncUrl { -}
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md deleted file mode 100644 index a5adccc..0000000 --- a/src/main/resources/Documentation/about.md +++ /dev/null
@@ -1,10 +0,0 @@ -The @PLUGIN@ plugin allows to synchronize the eviction of caches between two -Gerrit instances sharing the same repositories and database. - -The plugin needs to be installed in both instances and every time a cache -eviction occurs in one of the instances, the other instance's cache is updated. -This way, both caches are kept synchronized. - -For this synchronization to work, http must be enabled in both instances and the -plugin must be configured with valid credentials. For further information, refer -to [config](config.md) documentation.
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md deleted file mode 100644 index b5b5ed7..0000000 --- a/src/main/resources/Documentation/build.md +++ /dev/null
@@ -1,106 +0,0 @@ -Build -===== - -This plugin is built with Buck. - -Two build modes are supported: Standalone and in Gerrit tree. Standalone -build mode is recommended, as this mode doesn't require local Gerrit -tree to exist. - -Build standalone ----------------- - -Clone bucklets library: - -``` - git clone https://gerrit.googlesource.com/bucklets - -``` -and link it to @PLUGIN@ directory: - -``` - cd @PLUGIN@ && ln -s ../bucklets . -``` - -Add link to the .buckversion file: - -``` - cd @PLUGIN@ && ln -s bucklets/buckversion .buckversion -``` - -Add link to the .watchmanconfig file: - -``` - cd @PLUGIN@ && ln -s bucklets/watchmanconfig .watchmanconfig -``` - -To build the plugin, issue the following command: - -``` - buck build plugin -``` - -The output is created in: - -``` - buck-out/gen/@PLUGIN@.jar -``` - -This project can be imported into the Eclipse IDE: - -``` - ./bucklets/tools/eclipse.py -``` - -To execute the tests run: - -``` - buck test -``` - -To build plugin sources run: - -``` - buck build src -``` - -The output is created in: - -``` - buck-out/gen/@PLUGIN@-sources.jar -``` - -Build in Gerrit tree --------------------- - -Clone or link this plugin to the plugins directory of Gerrit's source -tree, and issue the command: - -``` - buck build plugins/@PLUGIN@ -``` - -The output is created in: - -``` - buck-out/gen/plugins/@PLUGIN@/@PLUGIN@.jar -``` - -This project can be imported into the Eclipse IDE: - -``` - ./tools/eclipse/project.py -``` - -To execute the tests run: - -``` - buck test --include @PLUGIN@ -``` - -How to build the Gerrit Plugin API is described in the [Gerrit -documentation](../../../Documentation/dev-buck.html#_extension_and_plugin_api_jar_files). - -[Back to @PLUGIN@ documentation index][index] - -[index]: index.html \ No newline at end of file
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md deleted file mode 100644 index da544a7..0000000 --- a/src/main/resources/Documentation/config.md +++ /dev/null
@@ -1,46 +0,0 @@ -@PLUGIN@ Configuration -========================= - -In order for the synchronization to work, the @PLUGIN@ plugin must be installed -in both instances and the following fields should be specified in the -corresponding Gerrit configuration file: - -File 'gerrit.config' --------------------- - -plugin.@PLUGIN@.url -: Specify the URL for the secondary (target) instance. - -plugin.@PLUGIN@.user -: Username to connect to the secondary (target) instance. - -plugin.@PLUGIN@.password -: Password to connect to the secondary (target) instance. This value can - also be defined in secure.config. - -@PLUGIN@ plugin uses REST API calls to synchronize cache evictions in the target -instance. It is possible to customize the parameters of the underlying http client -doing these calls by specifying the following fields: - -@PLUGIN@.connectionTimeout -: Maximum interval of time in milliseconds the plugin waits for a connection - to the target instance. When not specified, the default value is set to 5000ms. - -@PLUGIN@.socketTimeout -: Maximum interval of time in milliseconds the plugin waits for a response from the - target instance once the connection has been established. When not specified, - the default value is set to 5000ms. - -@PLUGIN@.maxTries -: Maximum number of times the plugin should attempt to evict the cache in the - target instance. Setting this value to 0 will disable retries. When not - specified, the default value is 5. After this number of failed tries, an error - is logged so that admins can flush the cache manually. - -@PLUGIN@.retryInterval -: The interval of time in milliseconds between the subsequent auto-retries. - When not specified, the default value is set to 1000ms. - -@PLUGIN@.threadPoolSize -: Maximum number of threads used to send cache evictions to the target instance. - Defaults to 1.
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/CacheResponseHandlerTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/CacheResponseHandlerTest.java deleted file mode 100644 index 8e94727..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/CacheResponseHandlerTest.java +++ /dev/null
@@ -1,69 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.ericsson.gerrit.plugins.evictcache.CacheResponseHandler.CacheResult; - -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.entity.StringEntity; -import org.junit.Before; -import org.junit.Test; - -import java.io.UnsupportedEncodingException; - -public class CacheResponseHandlerTest { - private static final int ERROR = 400; - private static final int OK = 204; - private static final String EMPTY_ENTITY = ""; - private static final String ERROR_ENTITY = "Error"; - - private CacheResponseHandler handler; - - @Before - public void setUp() throws Exception { - handler = new CacheResponseHandler(); - } - - @Test - public void testIsSuccessful() throws Exception { - HttpResponse response = setupMocks(OK, EMPTY_ENTITY); - CacheResult result = handler.handleResponse(response); - assertThat(result.isSuccessful()).isTrue(); - assertThat(result.getMessage()).isEmpty(); - } - - @Test - public void testIsNotSuccessful() throws Exception { - HttpResponse response = setupMocks(ERROR, ERROR_ENTITY); - CacheResult result = handler.handleResponse(response); - assertThat(result.isSuccessful()).isFalse(); - assertThat(result.getMessage()).contains(ERROR_ENTITY); - } - - private HttpResponse setupMocks(int httpCode, String entity) - throws UnsupportedEncodingException { - StatusLine status = mock(StatusLine.class); - when(status.getStatusCode()).thenReturn(httpCode); - HttpResponse response = mock(HttpResponse.class); - when(response.getStatusLine()).thenReturn(status); - when(response.getEntity()).thenReturn(new StringEntity(entity)); - return response; - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/ConfigurationTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/ConfigurationTest.java deleted file mode 100644 index 88ddc54..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/ConfigurationTest.java +++ /dev/null
@@ -1,107 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.when; - -import com.google.gerrit.server.config.PluginConfig; -import com.google.gerrit.server.config.PluginConfigFactory; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class ConfigurationTest { - private static final String PASS = "fakePass"; - private static final String USER = "fakeUser"; - private static final String URL = "fakeUrl"; - private static final String EMPTY = ""; - private static final boolean CUSTOM_VALUES = true; - private static final boolean DEFAULT_VALUES = false; - private static final int TIMEOUT = 5000; - private static final int MAX_TRIES = 5; - private static final int RETRY_INTERVAL = 1000; - private static final int THREAD_POOL_SIZE = 1; - - @Mock - private PluginConfigFactory cfgFactoryMock; - @Mock - private PluginConfig configMock; - private Configuration configuration; - private String pluginName = "evict-cache"; - - @Before - public void setUp() throws Exception { - when(cfgFactoryMock.getFromGerritConfig(pluginName, true)) - .thenReturn(configMock); - } - - @Test - public void testValuesPresentInGerritConfig() throws Exception { - buildMocks(CUSTOM_VALUES); - assertThat(configuration.getUrl()).isEqualTo(URL); - assertThat(configuration.getUser()).isEqualTo(USER); - assertThat(configuration.getPassword()).isEqualTo(PASS); - assertThat(configuration.getConnectionTimeout()).isEqualTo(TIMEOUT); - assertThat(configuration.getSocketTimeout()).isEqualTo(TIMEOUT); - assertThat(configuration.getMaxTries()).isEqualTo(MAX_TRIES); - assertThat(configuration.getRetryInterval()).isEqualTo(RETRY_INTERVAL); - assertThat(configuration.getThreadPoolSize()).isEqualTo(THREAD_POOL_SIZE); - } - - @Test - public void testValuesNotPresentInGerritConfig() throws Exception { - buildMocks(DEFAULT_VALUES); - assertThat(configuration.getUrl()).isEqualTo(EMPTY); - assertThat(configuration.getUser()).isEqualTo(EMPTY); - assertThat(configuration.getPassword()).isEqualTo(EMPTY); - assertThat(configuration.getConnectionTimeout()).isEqualTo(0); - assertThat(configuration.getSocketTimeout()).isEqualTo(0); - assertThat(configuration.getMaxTries()).isEqualTo(0); - assertThat(configuration.getRetryInterval()).isEqualTo(0); - assertThat(configuration.getThreadPoolSize()).isEqualTo(0); - } - - @Test - public void testUrlTrailingSlashIsDropped() throws Exception { - when(configMock.getString("url")).thenReturn(URL + "/"); - - configuration = new Configuration(cfgFactoryMock, pluginName); - assertThat(configuration).isNotNull(); - assertThat(configuration.getUrl()).isEqualTo(URL); - } - - private void buildMocks(boolean values) { - when(configMock.getString("url")).thenReturn(values ? URL : null); - when(configMock.getString("user")).thenReturn(values ? USER : null); - when(configMock.getString("password")).thenReturn(values ? PASS : null); - when(configMock.getInt("connectionTimeout", TIMEOUT)) - .thenReturn(values ? TIMEOUT : 0); - when(configMock.getInt("socketTimeout", TIMEOUT)) - .thenReturn(values ? TIMEOUT : 0); - when(configMock.getInt("maxTries", MAX_TRIES)) - .thenReturn(values ? MAX_TRIES : 0); - when(configMock.getInt("retryInterval", RETRY_INTERVAL)) - .thenReturn(values ? RETRY_INTERVAL : 0); - when(configMock.getInt("threadPoolSize", THREAD_POOL_SIZE)) - .thenReturn(values ? THREAD_POOL_SIZE : 0); - - configuration = new Configuration(cfgFactoryMock, pluginName); - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/Constants.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/Constants.java deleted file mode 100644 index f7ba16c..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/Constants.java +++ /dev/null
@@ -1,32 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -final class Constants { - - private Constants() { - } - - static final String URL = "http://localhost:18888"; - static final String ENDPOINT_BASE = "/plugins/evict-cache/gerrit/evict/"; - static final String PROJECT_LIST = "project_list"; - static final String ACCOUNTS = "accounts"; - static final String GROUPS = "groups"; - static final String GROUPS_BYINCLUDE = "groups_byinclude"; - static final String GROUPS_MEMBERS = "groups_members"; - static final String DEFAULT = "projects"; - - static final int PORT = 18888; -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/ContextTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/ContextTest.java deleted file mode 100644 index f7f51b6..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/ContextTest.java +++ /dev/null
@@ -1,45 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; - -public class ContextTest { - - @Test - public void testInitialValueNotNull() throws Exception { - assertThat(Context.isForwardedEvent()).isNotNull(); - assertThat(Context.isForwardedEvent()).isFalse(); - } - - @Test - public void testSetForwardedEvent() throws Exception { - Context.setForwardedEvent(); - try { - assertThat(Context.isForwardedEvent()).isTrue(); - } finally { - Context.unsetForwardedEvent(); - } - } - - @Test - public void testUnsetForwardedEvent() throws Exception { - Context.setForwardedEvent(); - Context.unsetForwardedEvent(); - assertThat(Context.isForwardedEvent()).isFalse(); - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutorProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutorProviderTest.java deleted file mode 100644 index a01b187..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheExecutorProviderTest.java +++ /dev/null
@@ -1,64 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.gerrit.server.git.WorkQueue; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class EvictCacheExecutorProviderTest { - private static final String PLUGIN_NAME = "evict-cache"; - - @Mock - private WorkQueue.Executor executorMock; - private EvictCacheExecutorProvider evictCacheExecutorProvider; - - @Before - public void setUp() throws Exception { - WorkQueue workQueueMock = mock(WorkQueue.class); - when(workQueueMock.createQueue(4, - "Evict cache [" + PLUGIN_NAME + " plugin]")).thenReturn(executorMock); - Configuration configMock = mock(Configuration.class); - when(configMock.getThreadPoolSize()).thenReturn(4); - - evictCacheExecutorProvider = - new EvictCacheExecutorProvider(workQueueMock, PLUGIN_NAME, configMock); - } - - @Test - public void shouldReturnExecutor() throws Exception { - assertThat(evictCacheExecutorProvider.get()).isEqualTo(executorMock); - } - - @Test - public void testStop() throws Exception { - evictCacheExecutorProvider.start(); - assertThat(evictCacheExecutorProvider.get()).isEqualTo(executorMock); - evictCacheExecutorProvider.stop(); - verify(executorMock).shutdown(); - verify(executorMock).unregisterWorkQueue(); - assertThat(evictCacheExecutorProvider.get()).isNull(); - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheIT.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheIT.java deleted file mode 100644 index b44ac31..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheIT.java +++ /dev/null
@@ -1,74 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.givenThat; -import static com.github.tomakehurst.wiremock.client.WireMock.post; -import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; -import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.verify; - -import com.google.gerrit.acceptance.GerritConfig; -import com.google.gerrit.acceptance.GerritConfigs; -import com.google.gerrit.acceptance.NoHttpd; -import com.google.gerrit.acceptance.PluginDaemonTest; - -import com.github.tomakehurst.wiremock.http.Request; -import com.github.tomakehurst.wiremock.http.RequestListener; -import com.github.tomakehurst.wiremock.http.Response; -import com.github.tomakehurst.wiremock.junit.WireMockRule; - -import org.apache.http.HttpStatus; -import org.junit.Rule; -import org.junit.Test; - -import java.util.concurrent.TimeUnit; - -@NoHttpd -public class EvictCacheIT extends PluginDaemonTest { - - @Rule - public WireMockRule wireMockRule = new WireMockRule(Constants.PORT); - - @Test - @GerritConfigs({ - @GerritConfig(name = "plugin.evict-cache.url", value = Constants.URL), - @GerritConfig(name = "plugin.evict-cache.user", value = "admin") - }) - public void flushAndSendPost() throws Exception { - final String flushRequest = - Constants.ENDPOINT_BASE + Constants.PROJECT_LIST; - wireMockRule.addMockServiceRequestListener(new RequestListener() { - @Override - public void requestReceived(Request request, Response response) { - if (request.getAbsoluteUrl().contains(flushRequest)) { - synchronized (flushRequest) { - flushRequest.notify(); - } - } - } - }); - givenThat(post(urlEqualTo(flushRequest)) - .willReturn(aResponse().withStatus(HttpStatus.SC_OK))); - - adminSshSession - .exec("gerrit flush-caches --cache " + Constants.PROJECT_LIST); - synchronized (flushRequest) { - flushRequest.wait(TimeUnit.SECONDS.toMillis(2)); - } - verify(postRequestedFor(urlEqualTo(flushRequest))); - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheRestApiServletTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheRestApiServletTest.java deleted file mode 100644 index 3db1f75..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/EvictCacheRestApiServletTest.java +++ /dev/null
@@ -1,131 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; -import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.common.cache.Cache; -import com.google.gerrit.extensions.registration.DynamicMap; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import java.io.BufferedReader; -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -@RunWith(MockitoJUnitRunner.class) -public class EvictCacheRestApiServletTest { - @Mock - private HttpServletRequest request; - @Mock - private HttpServletResponse response; - @Mock - private BufferedReader reader; - @Mock - private DynamicMap<Cache<?, ?>> cacheMap; - private EvictCacheRestApiServlet servlet; - - @Before - public void setUp() { - servlet = new EvictCacheRestApiServlet(cacheMap); - } - - @Test - public void evictAccounts() throws Exception { - configureMocksFor(Constants.ACCOUNTS); - verifyResponseIsOK(); - } - - @Test - public void evictProjectList() throws Exception { - configureMocksFor(Constants.PROJECT_LIST); - verifyResponseIsOK(); - } - - @Test - public void evictGroups() throws Exception { - configureMocksFor(Constants.GROUPS); - verifyResponseIsOK(); - } - - @Test - public void evictGroupsByInclude() throws Exception { - configureMocksFor(Constants.GROUPS_BYINCLUDE); - verifyResponseIsOK(); - } - - @Test - public void evictGroupsMembers() throws Exception { - configureMocksFor(Constants.GROUPS_MEMBERS); - servlet.doPost(request, response); - - } - - @Test - public void evictDefault() throws Exception { - configureMocksFor(Constants.DEFAULT); - verifyResponseIsOK(); - } - - private void verifyResponseIsOK() throws Exception { - servlet.doPost(request, response); - verify(response).setStatus(SC_NO_CONTENT); - } - - @Test - public void badRequest() throws Exception { - when(request.getPathInfo()).thenReturn("/someCache"); - String errorMessage = "someError"; - doThrow(new IOException(errorMessage)).when(request).getReader(); - servlet.doPost(request, response); - verify(response).sendError(SC_BAD_REQUEST, errorMessage); - } - - @Test - public void errorWhileSendingErrorMessage() throws Exception { - when(request.getPathInfo()).thenReturn("/someCache"); - String errorMessage = "someError"; - doThrow(new IOException(errorMessage)).when(request).getReader(); - servlet.doPost(request, response); - verify(response).sendError(SC_BAD_REQUEST, errorMessage); - } - - @SuppressWarnings("unchecked") - private void configureMocksFor(String cacheName) throws IOException { - when(cacheMap.get("gerrit", cacheName)).thenReturn(mock(Cache.class)); - when(request.getPathInfo()).thenReturn("/" + cacheName); - when(request.getReader()).thenReturn(reader); - - if (Constants.DEFAULT.equals(cacheName)) { - when(reader.readLine()).thenReturn("abc"); - } else if (Constants.GROUPS_BYINCLUDE.equals(cacheName) - || Constants.GROUPS_MEMBERS.equals(cacheName)) { - when(reader.readLine()).thenReturn("{\"uuid\":\"abcd1234\"}"); - } else { - when(reader.readLine()).thenReturn("{}"); - } - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/GsonParserTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/GsonParserTest.java deleted file mode 100644 index 638456d..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/GsonParserTest.java +++ /dev/null
@@ -1,65 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.gerrit.reviewdb.client.Account; -import com.google.gerrit.reviewdb.client.AccountGroup; - -import org.junit.Test; - -public class GsonParserTest { - private static final Object EMPTY_JSON = "{}"; - - @Test - public void AccountIDParse() { - Account.Id accountId = new Account.Id(1); - String json = GsonParser.toJson(Constants.ACCOUNTS, accountId); - assertThat(accountId) - .isEqualTo(GsonParser.fromJson(Constants.ACCOUNTS, json)); - } - - @Test - public void AccountGroupIDParse() { - AccountGroup.Id accountGroupId = new AccountGroup.Id(1); - String json = GsonParser.toJson(Constants.GROUPS, accountGroupId); - assertThat(accountGroupId) - .isEqualTo(GsonParser.fromJson(Constants.GROUPS, json)); - } - - @Test - public void AccountGroupUUIDParse() { - AccountGroup.UUID accountGroupUuid = new AccountGroup.UUID("abc123"); - String json = - GsonParser.toJson(Constants.GROUPS_BYINCLUDE, accountGroupUuid); - assertThat(accountGroupUuid) - .isEqualTo(GsonParser.fromJson(Constants.GROUPS_BYINCLUDE, json)); - } - - @Test - public void StringParse() { - String key = "key"; - String json = GsonParser.toJson(Constants.DEFAULT, key); - assertThat(key).isEqualTo(GsonParser.fromJson(Constants.DEFAULT, json)); - } - - @Test - public void NoKeyParse() { - Object object = new Object(); - String json = GsonParser.toJson(Constants.PROJECT_LIST, object); - assertThat(json).isEqualTo(EMPTY_JSON); - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/HttpClientProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/HttpClientProviderTest.java deleted file mode 100644 index e64cf7c..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/HttpClientProviderTest.java +++ /dev/null
@@ -1,68 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.when; - -import com.google.gerrit.lifecycle.LifecycleModule; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Scopes; - -import org.apache.http.impl.client.CloseableHttpClient; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class HttpClientProviderTest { - private static final int TIME_INTERVAL = 1000; - private static final String EMPTY = ""; - - @Mock - private Configuration config; - - @Before - public void setUp() throws Exception { - when(config.getUrl()).thenReturn(EMPTY); - when(config.getUser()).thenReturn(EMPTY); - when(config.getPassword()).thenReturn(EMPTY); - when(config.getConnectionTimeout()).thenReturn(TIME_INTERVAL); - when(config.getSocketTimeout()).thenReturn(TIME_INTERVAL); - } - - @Test - public void testGet() throws Exception { - Injector injector = Guice.createInjector(new TestModule()); - CloseableHttpClient httpClient1 = - injector.getInstance(CloseableHttpClient.class); - assertThat(httpClient1).isNotNull(); - CloseableHttpClient httpClient2 = - injector.getInstance(CloseableHttpClient.class); - assertThat(httpClient1).isEqualTo(httpClient2); - } - - class TestModule extends LifecycleModule { - @Override - protected void configure() { - bind(Configuration.class).toInstance(config); - bind(CloseableHttpClient.class).toProvider(HttpClientProvider.class) - .in(Scopes.SINGLETON); - } - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/HttpSessionTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/HttpSessionTest.java deleted file mode 100644 index f45e52d..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/HttpSessionTest.java +++ /dev/null
@@ -1,188 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.post; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; -import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.ericsson.gerrit.plugins.evictcache.CacheResponseHandler.CacheResult; -import com.github.tomakehurst.wiremock.http.Fault; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import com.github.tomakehurst.wiremock.stubbing.Scenario; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; - -import java.net.SocketTimeoutException; - -public class HttpSessionTest { - private static final int MAX_TRIES = 3; - private static final int RETRY_INTERVAL = 250; - private static final int TIMEOUT = 500; - private static final int ERROR = 500; - private static final int OK = 204; - private static final int NOT_FOUND = 404; - private static final int UNAUTHORIZED = 401; - - private static final String ENDPOINT = Constants.ENDPOINT_BASE + "cache"; - private static final String EMPTYJSON = "{}"; - private static final String ERROR_MESSAGE = "Error message"; - private static final String REQUEST_MADE = "Request made"; - private static final String SECOND_TRY = "Second try"; - private static final String THIRD_TRY = "Third try"; - private static final String RETRY_AT_ERROR = "Retry at error"; - private static final String RETRY_AT_DELAY = "Retry at delay"; - - private HttpSession httpSession; - - @Rule - public WireMockRule wireMockRule = new WireMockRule(Constants.PORT); - - @Before - public void setUp() throws Exception { - Configuration cfg = mock(Configuration.class); - when(cfg.getUrl()).thenReturn(Constants.URL); - when(cfg.getUser()).thenReturn("user"); - when(cfg.getPassword()).thenReturn("pass"); - when(cfg.getMaxTries()).thenReturn(MAX_TRIES); - when(cfg.getConnectionTimeout()).thenReturn(TIMEOUT); - when(cfg.getSocketTimeout()).thenReturn(TIMEOUT); - when(cfg.getRetryInterval()).thenReturn(RETRY_INTERVAL); - - httpSession = - new HttpSession(new HttpClientProvider(cfg).get(), Constants.URL); - } - - @Test - public void testResponseOK() throws Exception { - stubFor(post(urlEqualTo(ENDPOINT)).willReturn(aResponse().withStatus(OK))); - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).isSuccessful()).isTrue(); - } - - @Test - public void testNotAuthorized() throws Exception { - String expected = "unauthorized"; - stubFor(post(urlEqualTo(ENDPOINT)).willReturn( - aResponse().withStatus(UNAUTHORIZED).withBody(expected))); - - CacheResult result = httpSession.post(ENDPOINT, EMPTYJSON); - assertThat(result.isSuccessful()).isFalse(); - assertThat(result.getMessage()).isEqualTo(expected); - } - - @Test - public void testNotFound() throws Exception { - String expected = "not found"; - stubFor(post(urlEqualTo(ENDPOINT)).willReturn( - aResponse().withStatus(NOT_FOUND).withBody(expected))); - - CacheResult result = httpSession.post(ENDPOINT, EMPTYJSON); - assertThat(result.isSuccessful()).isFalse(); - assertThat(result.getMessage()).isEqualTo(expected); - } - - @Test - public void testBadResponseRetryThenOK() throws Exception { - stubFor(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_ERROR) - .whenScenarioStateIs(Scenario.STARTED).willSetStateTo(REQUEST_MADE) - .willReturn(aResponse().withStatus(ERROR))); - stubFor(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_ERROR) - .whenScenarioStateIs(REQUEST_MADE) - .willReturn(aResponse().withStatus(OK))); - - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).isSuccessful()).isTrue(); - } - - @Test - public void testBadResponseRetryThenGiveUp() throws Exception { - stubFor(post(urlEqualTo(ENDPOINT)).willReturn( - aResponse().withStatus(ERROR).withBody(ERROR_MESSAGE))); - - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).isSuccessful()).isFalse(); - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).getMessage()) - .isEqualTo(ERROR_MESSAGE); - } - - @Test - public void testRetryAfterDelay() throws Exception { - stubFor(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(Scenario.STARTED).willSetStateTo(REQUEST_MADE) - .willReturn(aResponse().withStatus(ERROR).withFixedDelay(TIMEOUT / 2))); - stubFor(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(REQUEST_MADE) - .willReturn(aResponse().withStatus(OK))); - - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).isSuccessful()).isTrue(); - } - - @Test - public void testRetryAfterTimeoutThenOK() throws Exception { - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(Scenario.STARTED).willSetStateTo(REQUEST_MADE) - .willReturn(aResponse().withFixedDelay(TIMEOUT))); - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(REQUEST_MADE).willSetStateTo(SECOND_TRY) - .willReturn(aResponse().withFixedDelay(TIMEOUT))); - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(SECOND_TRY).willSetStateTo(THIRD_TRY) - .willReturn(aResponse().withFixedDelay(TIMEOUT))); - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(THIRD_TRY) - .willReturn(aResponse().withStatus(OK))); - - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).isSuccessful()).isTrue(); - } - - @Test(expected = SocketTimeoutException.class) - public void testMaxRetriesAfterTimeoutThenGiveUp() throws Exception { - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(Scenario.STARTED).willSetStateTo(REQUEST_MADE) - .willReturn(aResponse().withFixedDelay(TIMEOUT))); - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(REQUEST_MADE).willSetStateTo(SECOND_TRY) - .willReturn(aResponse().withFixedDelay(TIMEOUT))); - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(SECOND_TRY).willSetStateTo(THIRD_TRY) - .willReturn(aResponse().withFixedDelay(TIMEOUT))); - wireMockRule.givenThat(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(THIRD_TRY) - .willReturn(aResponse().withFixedDelay(TIMEOUT))); - - httpSession.post(ENDPOINT, EMPTYJSON); - } - - @Test - public void testGiveUpAtTimeout() throws Exception { - stubFor(post(urlEqualTo(ENDPOINT)).inScenario(RETRY_AT_DELAY) - .whenScenarioStateIs(Scenario.STARTED).willSetStateTo(REQUEST_MADE) - .willReturn(aResponse().withStatus(ERROR).withFixedDelay(TIMEOUT))); - - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).isSuccessful()).isFalse(); - } - - @Test - public void testResponseWithMalformedResponse() throws Exception { - stubFor(post(urlEqualTo(ENDPOINT)).willReturn( - aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK))); - - assertThat(httpSession.post(ENDPOINT, EMPTYJSON).isSuccessful()).isFalse(); - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/ModuleTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/ModuleTest.java deleted file mode 100644 index 479ed90..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/ModuleTest.java +++ /dev/null
@@ -1,34 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.junit.Test; - -public class ModuleTest { - - @Test - public void testSyncUrlProvider() { - Configuration configMock = mock(Configuration.class); - String expected = "someUrl"; - when(configMock.getUrl()).thenReturn(expected); - - Module module = new Module(); - assertThat(module.syncUrl(configMock)).isEqualTo(expected); - } -}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/evictcache/RestSessionTest.java b/src/test/java/com/ericsson/gerrit/plugins/evictcache/RestSessionTest.java deleted file mode 100644 index 731792b..0000000 --- a/src/test/java/com/ericsson/gerrit/plugins/evictcache/RestSessionTest.java +++ /dev/null
@@ -1,124 +0,0 @@ -// Copyright (C) 2015 Ericsson -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.ericsson.gerrit.plugins.evictcache; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.google.common.base.Joiner; -import com.google.gerrit.reviewdb.client.Account; -import com.google.gerrit.reviewdb.client.AccountGroup; - -import com.ericsson.gerrit.plugins.evictcache.CacheResponseHandler.CacheResult; - -import org.junit.Test; - -import java.io.IOException; - -public class RestSessionTest { - private static final String EVICT = "evict"; - private static final String SOURCE_NAME = "gerrit"; - private static final String PLUGIN_NAME = "evict-cache"; - private static final String EMPTY_JSON = "{}"; - private static final String EMPTY_JSON2 = "\"{}\""; - private static final String ID_RESPONSE = "{\"id\":0}"; - private static final boolean OK_RESPONSE = true; - private static final boolean FAIL_RESPONSE = false; - private static final boolean THROW_EXCEPTION = true; - private static final boolean DO_NOT_THROW_EXCEPTION = false; - - private RestSession restClient; - - @Test - public void testEvictCacheOK() throws Exception { - setupMocks(Constants.DEFAULT, EMPTY_JSON2, OK_RESPONSE, - DO_NOT_THROW_EXCEPTION); - assertThat(restClient.evict(SOURCE_NAME, Constants.DEFAULT, EMPTY_JSON)) - .isTrue(); - } - - @Test - public void testEvictAccountsOK() throws Exception { - setupMocks(Constants.ACCOUNTS, ID_RESPONSE, OK_RESPONSE, - DO_NOT_THROW_EXCEPTION); - assertThat(restClient.evict(SOURCE_NAME, Constants.ACCOUNTS, - mock(Account.Id.class))).isTrue(); - } - - @Test - public void testEvictGroupsOK() throws Exception { - setupMocks(Constants.GROUPS, ID_RESPONSE, OK_RESPONSE, - DO_NOT_THROW_EXCEPTION); - assertThat(restClient.evict(SOURCE_NAME, Constants.GROUPS, - mock(AccountGroup.Id.class))).isTrue(); - } - - @Test - public void testEvictGroupsByIncludeOK() throws Exception { - setupMocks(Constants.GROUPS_BYINCLUDE, EMPTY_JSON, OK_RESPONSE, - DO_NOT_THROW_EXCEPTION); - assertThat(restClient.evict(SOURCE_NAME, Constants.GROUPS_BYINCLUDE, - mock(AccountGroup.UUID.class))).isTrue(); - } - - @Test - public void testEvictGroupsMembersOK() throws Exception { - setupMocks(Constants.GROUPS_MEMBERS, EMPTY_JSON, OK_RESPONSE, - DO_NOT_THROW_EXCEPTION); - assertThat(restClient.evict(SOURCE_NAME, Constants.GROUPS_MEMBERS, - mock(AccountGroup.UUID.class))).isTrue(); - } - - @Test - public void testEvictProjectListOK() throws Exception { - setupMocks(Constants.PROJECT_LIST, EMPTY_JSON, OK_RESPONSE, - DO_NOT_THROW_EXCEPTION); - assertThat( - restClient.evict(SOURCE_NAME, Constants.PROJECT_LIST, new Object())) - .isTrue(); - } - - @Test - public void testEvictCacheFailed() throws Exception { - setupMocks(Constants.DEFAULT, EMPTY_JSON2, FAIL_RESPONSE, - DO_NOT_THROW_EXCEPTION); - assertThat(restClient.evict(SOURCE_NAME, Constants.DEFAULT, EMPTY_JSON)) - .isFalse(); - } - - @Test - public void testEvictCacheThrowsException() throws Exception { - setupMocks(Constants.DEFAULT, EMPTY_JSON2, FAIL_RESPONSE, THROW_EXCEPTION); - assertThat(restClient.evict(SOURCE_NAME, Constants.DEFAULT, EMPTY_JSON)) - .isFalse(); - } - - private void setupMocks(String cacheName, String json, boolean ok, - boolean exception) throws IOException { - String request = Joiner.on("/").join("/plugins", PLUGIN_NAME, SOURCE_NAME, - EVICT, cacheName); - HttpSession httpSession = mock(HttpSession.class); - if (exception) { - doThrow(new IOException()).when(httpSession).post(request, json); - } else { - CacheResult result = new CacheResult(ok, "Error"); - when(httpSession.post(request, json)).thenReturn(result); - } - - restClient = new RestSession(httpSession, PLUGIN_NAME); - } -}