Merge branch 'stable-2.16' into stable-3.0 * stable-2.16: Upgrade JGit to 5.1.13.202002110435-r Change-Id: I8d75be40a111167309b004a98f6e0ac50bc09588
diff --git a/.bazelversion b/.bazelversion index fd2a018..1545d96 100644 --- a/.bazelversion +++ b/.bazelversion
@@ -1 +1 @@ -3.1.0 +3.5.0
diff --git a/BUILD b/BUILD index 8eb7d2a..81ab40d 100644 --- a/BUILD +++ b/BUILD
@@ -41,6 +41,5 @@ exports = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [ ":lfs__plugin", "@jgit-lfs//jar", - "@joda-time//jar", ], )
diff --git a/WORKSPACE b/WORKSPACE index 28d10d5..0d0aacd 100644 --- a/WORKSPACE +++ b/WORKSPACE
@@ -3,28 +3,17 @@ load("//:bazlets.bzl", "load_bazlets") load_bazlets( - commit = "8ad33887665f4f6adf7cb465a03f6bd81b95e01d", + commit = "6d2b8f41ed34dad925b767399d1e4be378abb029", #local_path = "/home/<user>/projects/bazlets", ) -# Release Plugin API load( "@com_googlesource_gerrit_bazlets//:gerrit_api.bzl", "gerrit_api", ) -# Snapshot Plugin API -#load( -# "@com_googlesource_gerrit_bazlets//:gerrit_api_maven_local.bzl", -# "gerrit_api_maven_local", -#) - -# Load release Plugin API gerrit_api() -# Load snapshot Plugin API -#gerrit_api_maven_local() - load(":external_plugin_deps.bzl", "external_plugin_deps") external_plugin_deps()
diff --git a/external_plugin_deps.bzl b/external_plugin_deps.bzl index af81233..1b3231e 100644 --- a/external_plugin_deps.bzl +++ b/external_plugin_deps.bzl
@@ -1,15 +1,14 @@ load("//tools/bzl:maven_jar.bzl", "GERRIT", "MAVEN_CENTRAL", "MAVEN_LOCAL", "maven_jar") -JGIT_VERSION = "5.1.13.202002110435-r" +JGIT_VERSION = "5.3.7.202002110540-r" REPO = MAVEN_CENTRAL def external_plugin_deps(): maven_jar( name = "jgit-http-apache", artifact = "org.eclipse.jgit:org.eclipse.jgit.http.apache:" + JGIT_VERSION, - sha1 = "a919543ad9a0344b0bf55c6cb4e6c21199b83a8e", + sha1 = "2a7795f4af2589c4dfeb5fedca4ebe3985d350e0", repository = REPO, - unsign = True, exclude = [ "about.html", "plugin.properties", @@ -19,9 +18,8 @@ maven_jar( name = "jgit-lfs", artifact = "org.eclipse.jgit:org.eclipse.jgit.lfs:" + JGIT_VERSION, - sha1 = "be6edb7d1406a3afbd43d17fb1bab30f1a33275f", + sha1 = "09c56d0c76823dd59c31b4843aa6782d1811303a", repository = REPO, - unsign = True, exclude = [ "about.html", "plugin.properties", @@ -31,17 +29,10 @@ maven_jar( name = "jgit-lfs-server", artifact = "org.eclipse.jgit:org.eclipse.jgit.lfs.server:" + JGIT_VERSION, - sha1 = "5df9e9da05ea8fa30dd30b1b80f5875c7720ceeb", + sha1 = "10fd5160ef668c9c55b32cce1cbf8507c03c2c1a", repository = REPO, - unsign = True, exclude = [ "about.html", "plugin.properties", ], ) - - maven_jar( - name = "joda-time", - artifact = "joda-time:joda-time:2.9.9", - sha1 = "f7b520c458572890807d143670c9b24f4de90897", - )
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/HttpModule.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/HttpModule.java index 2bd7091..aabbd69 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/HttpModule.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/HttpModule.java
@@ -62,11 +62,6 @@ populateRepository(backend); } - // Only for GWT-UI - DynamicSet.bind(binder(), WebUiPlugin.class) - .toInstance(new JavaScriptPlugin("lfs-project-info.js")); - - // Only for PolyGerrit-UI DynamicSet.bind(binder(), WebUiPlugin.class).toInstance(new JavaScriptPlugin("gr-lfs.html")); }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/InitLfs.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/InitLfs.java index 89e6238..f44c8d8 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/InitLfs.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/InitLfs.java
@@ -36,7 +36,4 @@ public void run() throws Exception { lfs.set(PLUGIN_KEY, name); } - - @Override - public void postRun() throws Exception {} }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsApiServlet.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsApiServlet.java index 8982dbe..992dada 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsApiServlet.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsApiServlet.java
@@ -22,9 +22,9 @@ import static com.google.gerrit.server.permissions.ProjectPermission.PUSH_AT_LEAST_ONE_REF; import com.google.common.flogger.FluentLogger; -import com.google.gerrit.common.ProjectUtil; import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.server.CurrentUser; +import com.google.gerrit.server.ProjectUtil; import com.google.gerrit.server.permissions.PermissionBackend; import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectState; @@ -81,7 +81,7 @@ throw new LfsException("no repository at " + pathInfo); } String projName = matcher.group(1); - Project.NameKey project = Project.NameKey.parse(ProjectUtil.stripGitSuffix(projName)); + Project.NameKey project = Project.nameKey(ProjectUtil.stripGitSuffix(projName)); ProjectState state = projectCache.get(project); if (state == null || state.getProject().getState() == HIDDEN) { throw new LfsRepositoryNotFound(project.get());
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsDateTime.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsDateTime.java index 4a1936c..1a4c689 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsDateTime.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsDateTime.java
@@ -20,6 +20,8 @@ import java.util.Locale; public class LfsDateTime { + private LfsDateTime() {} + private static final DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") .withZone(ZoneOffset.UTC)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGson.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGson.java index 66398fe..f2af66e 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGson.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGson.java
@@ -17,8 +17,6 @@ import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonIOException; -import com.google.gson.JsonSyntaxException; import com.google.inject.Singleton; import java.io.Reader; @@ -34,7 +32,7 @@ .create(); } - public void toJson(Object src, Appendable writer) throws JsonIOException { + public void toJson(Object src, Appendable writer) { gson.toJson(src, writer); } @@ -42,8 +40,7 @@ return gson.toJson(src); } - public <T> T fromJson(Reader json, Class<T> classOfT) - throws JsonSyntaxException, JsonIOException { + public <T> T fromJson(Reader json, Class<T> classOfT) { return gson.fromJson(json, classOfT); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthUserProvider.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthUserProvider.java index fcd4736..cc19dd3 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthUserProvider.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthUserProvider.java
@@ -14,6 +14,7 @@ package com.googlesource.gerrit.plugins.lfs.auth; +import static com.google.common.base.MoreObjects.firstNonNull; import static com.googlesource.gerrit.plugins.lfs.auth.LfsSshRequestAuthorizer.SSH_AUTH_PREFIX; import com.google.common.base.Strings; @@ -30,7 +31,7 @@ @Singleton public class LfsAuthUserProvider { private final Provider<AnonymousUser> anonymous; - private final Provider<CurrentUser> user; + private final Provider<CurrentUser> currentUser; private final LfsSshRequestAuthorizer sshAuth; private final AccountCache accounts; private final IdentifiedUser.GenericFactory userFactory; @@ -38,12 +39,12 @@ @Inject LfsAuthUserProvider( Provider<AnonymousUser> anonymous, - Provider<CurrentUser> user, + Provider<CurrentUser> currentUser, LfsSshRequestAuthorizer sshAuth, AccountCache accounts, IdentifiedUser.GenericFactory userFactory) { this.anonymous = anonymous; - this.user = user; + this.currentUser = currentUser; this.sshAuth = sshAuth; this.accounts = accounts; this.userFactory = userFactory; @@ -61,7 +62,6 @@ } } } - CurrentUser currentUser = user.get(); - return currentUser != null ? currentUser : anonymous.get(); + return firstNonNull(currentUser.get(), anonymous.get()); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsSshAuth.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsSshAuth.java index 9fd57b9..3dd7a86 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsSshAuth.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/auth/LfsSshAuth.java
@@ -44,7 +44,7 @@ } @Override - public String authenticate(CurrentUser user, List<String> args) throws UnloggedFailure, Failure { + public String authenticate(CurrentUser user, List<String> args) throws Failure { if (args.size() != 2) { throw new UnloggedFailure(1, "Unexpected number of arguments"); }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksAction.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksAction.java index 4b6afb4..32536f7 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksAction.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksAction.java
@@ -22,10 +22,10 @@ import static org.eclipse.jgit.util.HttpSupport.HDR_AUTHORIZATION; import com.google.common.flogger.FluentLogger; -import com.google.gerrit.common.ProjectUtil; import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.server.CurrentUser; +import com.google.gerrit.server.ProjectUtil; import com.google.gerrit.server.permissions.PermissionBackend; import com.google.gerrit.server.permissions.PermissionBackend.ForProject; import com.google.gerrit.server.permissions.PermissionBackendException; @@ -100,7 +100,7 @@ throws LfsException, IOException; protected ProjectState getProject(String name) throws LfsRepositoryNotFound { - Project.NameKey project = Project.NameKey.parse(ProjectUtil.stripGitSuffix(name)); + Project.NameKey project = Project.nameKey(ProjectUtil.stripGitSuffix(name)); ProjectState state = projectCache.get(project); if (state == null || state.getProject().getState() == HIDDEN) { throw new LfsRepositoryNotFound(project.get());
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java index 1de28f4..66ee924 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java
@@ -14,6 +14,8 @@ package com.googlesource.gerrit.plugins.lfs.locks; +import static java.util.stream.Collectors.groupingBy; + import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; @@ -29,7 +31,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; import org.eclipse.jgit.lfs.errors.LfsException; @Singleton @@ -66,14 +67,10 @@ }; } - private final PathToLockId toLockId; private final LoadingCache<Project.NameKey, LfsProjectLocks> projectLocks; @Inject - LfsLocksHandler( - PathToLockId toLockId, - @Named(CACHE_NAME) LoadingCache<Project.NameKey, LfsProjectLocks> projectLocks) { - this.toLockId = toLockId; + LfsLocksHandler(@Named(CACHE_NAME) LoadingCache<Project.NameKey, LfsProjectLocks> projectLocks) { this.projectLocks = projectLocks; } @@ -99,10 +96,9 @@ } LfsLock lock = hasLock.get(); - if (lock.owner.name.equals(user.getUserName().get())) { - locks.deleteLock(lock); - return new LfsLockResponse(lock); - } else if (input.force) { + Optional<String> username = user.getUserName(); + if ((username.isPresent() && lock.owner.name.equals(username.get())) + || Boolean.TRUE.equals(input.force)) { locks.deleteLock(lock); return new LfsLockResponse(lock); } @@ -116,18 +112,13 @@ LfsProjectLocks locks = projectLocks.getUnchecked(project); Map<Boolean, List<LfsLock>> groupByOurs = locks.getLocks().stream() - .collect( - Collectors.groupingBy( - (in) -> { - return in.owner.name.equals(user.getUserName().get()); - })); + .collect(groupingBy(in -> in.owner.name.equals(user.getUserName().get()))); return new LfsVerifyLocksResponse(groupByOurs.get(true), groupByOurs.get(false), null); } LfsGetLocksResponse listLocksByPath(Project.NameKey project, String path) { log.atFine().log("Get lock for %s path in %s project", path, project); - String lockId = toLockId.apply(path); - return listLocksById(project, lockId); + return listLocksById(project, PathToLockId.CONVERTER.convert(path)); } LfsGetLocksResponse listLocksById(Project.NameKey project, String id) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java index 4526fa4..e8987f9 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
@@ -43,19 +43,13 @@ private static final FluentLogger log = FluentLogger.forEnclosingClass(); private final LfsGson gson; - private final PathToLockId toLockId; private final String project; private final Path locksPath; private final Cache<String, LfsLock> locks; @Inject - LfsProjectLocks( - LfsGson gson, - PathToLockId toLockId, - LfsLocksPathProvider locksPath, - @Assisted Project.NameKey project) { + LfsProjectLocks(LfsGson gson, LfsLocksPathProvider locksPath, @Assisted Project.NameKey project) { this.gson = gson; - this.toLockId = toLockId; this.project = project.get(); this.locksPath = Paths.get(locksPath.get(), this.project); this.locks = CacheBuilder.newBuilder().build(); @@ -94,7 +88,7 @@ LfsLock createLock(CurrentUser user, LfsCreateLockInput input) throws LfsException { log.atFine().log("Create lock for %s in project %s", input.path, project); - String lockId = toLockId.apply(input.path); + String lockId = PathToLockId.CONVERTER.convert(input.path); LfsLock lock = locks.getIfPresent(lockId); if (lock != null) { throw new LfsLockExistsException(lock);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java index d90ebb8..4449469 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java
@@ -14,16 +14,22 @@ package com.googlesource.gerrit.plugins.lfs.locks; -import com.google.common.base.Function; -import com.google.common.hash.HashCode; +import static java.nio.charset.StandardCharsets.UTF_8; + import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; -import java.nio.charset.StandardCharsets; -public class PathToLockId implements Function<String, String> { - @Override - public String apply(String path) { - HashCode hash = Hashing.sha256().hashString(path, StandardCharsets.UTF_8); - return BaseEncoding.base16().lowerCase().encode(hash.asBytes()); +public class PathToLockId { + private PathToLockId() {} + + @FunctionalInterface + public interface PathToLockIdInterface { + String convert(String path); } + + static final PathToLockIdInterface CONVERTER = + path -> + BaseEncoding.base16() + .lowerCase() + .encode(Hashing.sha256().hashString(path, UTF_8).asBytes()); }
diff --git a/src/main/resources/static/gr-lfs.html b/src/main/resources/static/gr-lfs.html index 8493051..1a82aa8 100644 --- a/src/main/resources/static/gr-lfs.html +++ b/src/main/resources/static/gr-lfs.html
@@ -20,8 +20,6 @@ <dom-module id="gr-lfs"> <script> Gerrit.install(plugin => { - if (!window.Polymer) { return; } - plugin.registerCustomComponent('repo-config', 'gr-lfs-project-info'); }); </script>
diff --git a/src/main/resources/static/lfs-project-info.js b/src/main/resources/static/lfs-project-info.js deleted file mode 100644 index f81f975..0000000 --- a/src/main/resources/static/lfs-project-info.js +++ /dev/null
@@ -1,74 +0,0 @@ -// Copyright (C) 2016 The Android Open Source Project -// -// 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. - -Gerrit.install(function(self) { - - if (window.Polymer) { return; } - - // The code below is only used by the GWT-UI - - function toUpperCamel(str) { - var words = str.split('_'); - for (i=0; i<words.length; i++) { - words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1); - } - return words.join(' '); - } - - function createRow(doc, name, value) { - var tr = doc.createElement('tr'); - var td = doc.createElement('td'); - td.appendChild(doc.createTextNode(toUpperCamel(name) + ':')); - tr.appendChild(td); - td = doc.createElement('td'); - td.appendChild(doc.createTextNode(value)); - tr.appendChild(td); - return tr; - } - - function onProjectInfo(panel) { - var url = 'projects/' - + encodeURI(panel.p.PROJECT_NAME).replace(/\//g, '%2F') //escape slashes - + '/' + this.pluginName + '~' - + 'lfs:config-project'; - - Gerrit.get(url, function(lfs) { - // Don't show LFS header if nothing is configured - if (Object.getOwnPropertyNames(lfs).length < 1) { - return; - } - - var doc = document; - // create header - var td = doc.createElement('td'); - td.appendChild(Gerrit.html( - '<div class=\"smallHeading\">LFS Options</div>')); - var tr = doc.createElement('tr'); - tr.appendChild(td); - var table = doc.createElement('table'); - table.appendChild(tr); - - // add all properties - for (var name in lfs) { - table.appendChild(createRow(doc, name, lfs[name])); - } - - var frg = doc.createDocumentFragment(); - frg.appendChild(table); - panel.body.appendChild(frg); - }); - } - - self.panel('PROJECT_INFO_SCREEN_BOTTOM', onProjectInfo); - });
diff --git a/src/test/java/com/googlesource/gerrit/plugins/lfs/LfsDateTimeTest.java b/src/test/java/com/googlesource/gerrit/plugins/lfs/LfsDateTimeTest.java deleted file mode 100644 index e2852c5..0000000 --- a/src/test/java/com/googlesource/gerrit/plugins/lfs/LfsDateTimeTest.java +++ /dev/null
@@ -1,32 +0,0 @@ -// Copyright (C) 2017 The Android Open Source Project -// -// 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.googlesource.gerrit.plugins.lfs; - -import static com.google.common.truth.Truth.assertThat; - -import java.time.Instant; -import org.joda.time.DateTime; -import org.joda.time.format.ISODateTimeFormat; -import org.junit.Test; - -public class LfsDateTimeTest { - @Test - public void format() throws Exception { - DateTime now = DateTime.now(); - String jodaFormat = ISODateTimeFormat.dateTime().withZoneUTC().print(now); - String javaFormat = LfsDateTime.format(Instant.ofEpochMilli(now.getMillis())); - assertThat(javaFormat).isEqualTo(jodaFormat); - } -}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthTokenTest.java b/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthTokenTest.java index bdf7c21..f527aab 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthTokenTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsAuthTokenTest.java
@@ -15,6 +15,7 @@ package com.googlesource.gerrit.plugins.lfs.auth; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; import com.googlesource.gerrit.plugins.lfs.LfsDateTime; import java.time.Instant; @@ -33,7 +34,7 @@ String serialized = processor.serialize(token); assertThat(serialized).isNotEmpty(); Optional<TestToken> deserialized = processor.deserialize(serialized); - assertThat(deserialized.isPresent()).isTrue(); + assertThat(deserialized).isPresent(); assertThat(token.issued).isEqualTo(deserialized.get().issued); }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsCipherTest.java b/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsCipherTest.java index 08d8dc8..3072767 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsCipherTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/lfs/auth/LfsCipherTest.java
@@ -15,44 +15,44 @@ package com.googlesource.gerrit.plugins.lfs.auth; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; import java.util.Optional; import org.junit.Test; public class LfsCipherTest { + private static final String PLAIN_TEXT = "plain text"; + private final LfsCipher cipher = new LfsCipher(); @Test public void testCipherTextIsDifferentThanInput() throws Exception { - String plain = "plain text"; - String encrypted = cipher.encrypt(plain); + String encrypted = cipher.encrypt(PLAIN_TEXT); assertThat(encrypted).isNotEmpty(); - assertThat(encrypted).isNotEqualTo(plain); + assertThat(encrypted).isNotEqualTo(PLAIN_TEXT); } @Test public void testVerifyDecodeAgainstEncodedInput() throws Exception { - String plain = "plain text"; - String encrypted = cipher.encrypt(plain); + String encrypted = cipher.encrypt(PLAIN_TEXT); Optional<String> decrypted = cipher.decrypt(encrypted); - assertThat(decrypted.isPresent()).isTrue(); - assertThat(decrypted.get()).isEqualTo(plain); + assertThat(decrypted).isPresent(); + assertThat(decrypted.get()).isEqualTo(PLAIN_TEXT); } @Test public void testVerifyDecodeAgainstInvalidInput() throws Exception { - String plain = "plain text"; - String encrypted = cipher.encrypt(plain); + String encrypted = cipher.encrypt(PLAIN_TEXT); // there is a chance that two first chars in token are the same // in such case re-generate the token while (encrypted.charAt(0) == encrypted.charAt(1)) { - encrypted = cipher.encrypt(plain); + encrypted = cipher.encrypt(PLAIN_TEXT); } Optional<String> decrypted = cipher.decrypt( encrypted.substring(1, 2) + encrypted.substring(0, 1) + encrypted.substring(2)); - assertThat(decrypted.isPresent()).isTrue(); - assertThat(decrypted.get()).isNotEqualTo(plain); + assertThat(decrypted).isPresent(); + assertThat(decrypted.get()).isNotEqualTo(PLAIN_TEXT); } }