Refactor test
Reuse response template and content placeholder from GoImportFilter.
Make content creation more modular: reuse FQDN, more explicit format.
Change-Id: I1a644ced9588dccf50b65c6c19dd40ac31baadfe
diff --git a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
index e728713..78c71db 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
@@ -14,6 +14,7 @@
package com.ericsson.gerrit.plugins.goimport;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.gerrit.httpd.AllRequestFilter;
import com.google.gerrit.httpd.HtmlDomUtil;
@@ -39,6 +40,25 @@
@Singleton
public class GoImportFilter extends AllRequestFilter {
+ @VisibleForTesting static final String CONTENT_PLH = "${content}";
+
+ @VisibleForTesting
+ static final String PAGE_200 =
+ "<!DOCTYPE html>\n"
+ + "<html>\n"
+ + "<head>\n"
+ + " <title>Gerrit-Go-Import</title>\n"
+ + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"
+ + " <meta name=\"go-import\" content=\""
+ + CONTENT_PLH
+ + "\"/>\n"
+ + "</head>\n"
+ + "<body>\n"
+ + "<div>\n"
+ + " Gerrit-Go-Import\n"
+ + "</div>\n"
+ + "</body>\n"
+ + "</html>";
private static final String PAGE_404 =
"<!DOCTYPE html>\n"
@@ -52,21 +72,6 @@
+ "</body>\n"
+ "</html>";
- private static final String PAGE_200 =
- "<!DOCTYPE html>\n"
- + "<html>\n"
- + "<head>\n"
- + " <title>Gerrit-Go-Import</title>\n"
- + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"
- + " <meta name=\"go-import\" content=\"${content}\"/>\n"
- + "</head>\n"
- + "<body>\n"
- + "<div>\n"
- + " Gerrit-Go-Import\n"
- + "</div>\n"
- + "</body>\n"
- + "</html>";
-
private final ProjectCache projectCache;
final String webUrl;
final String projectPrefix;
@@ -108,7 +113,7 @@
byte[] toSend = PAGE_404.getBytes();
rsp.setStatus(404);
if (!Strings.isNullOrEmpty(existent)) {
- toSend = PAGE_200.replace("${content}", getContent(existent)).getBytes();
+ toSend = PAGE_200.replace(CONTENT_PLH, getContent(existent)).getBytes();
rsp.setStatus(200);
}
CacheHeaders.setNotCacheable(rsp);
diff --git a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
index d00e03e..379644f 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
@@ -14,6 +14,8 @@
package com.ericsson.gerrit.plugins.goimport;
+import static com.ericsson.gerrit.plugins.goimport.GoImportFilter.CONTENT_PLH;
+import static com.ericsson.gerrit.plugins.goimport.GoImportFilter.PAGE_200;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
@@ -40,22 +42,21 @@
@RunWith(MockitoJUnitRunner.class)
public class GoImportFilterTest {
+ private static final String PROD_FQDN = "gerrit-review.googlesource.com";
+ private static final String PROD_URL = "https://" + PROD_FQDN;
+ private static final String PROJECT_NAME = "projectName";
+ private static final String CONTENT_FORMAT = "%1$s/%3$s git %2$s/%3$s";
+ private static final String CONTENT =
+ String.format(CONTENT_FORMAT, PROD_FQDN, auth(PROD_URL), PROJECT_NAME);
- private static final String PROD_URL = "https://gerrit-review.googlesource.com";
- private static final String PAGE_200 =
- "<!DOCTYPE html>\n"
- + "<html>\n"
- + "<head>\n"
- + " <title>Gerrit-Go-Import</title>\n"
- + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"
- + " <meta name=\"go-import\" content=\"gerrit-review.googlesource.com/projectName git https://gerrit-review.googlesource.com/a/projectName\"/>\n"
- + "</head>\n"
- + "<body>\n"
- + "<div>\n"
- + " Gerrit-Go-Import\n"
- + "</div>\n"
- + "</body>\n"
- + "</html>";
+ /** URLs that require authentication has an additional "/a" ($BASE/a/$ENDPOINT). */
+ private static String auth(String baseUrl) {
+ return baseUrl + "/a";
+ }
+
+ private static byte[] response200() {
+ return PAGE_200.replace(CONTENT_PLH, CONTENT).getBytes();
+ }
private GoImportFilter unitUnderTest;
@@ -95,7 +96,6 @@
@Test
public void testDoFilterWithoutGoGetParameter() throws Exception {
- when(mockRequest.getServletPath()).thenReturn("/projectName");
when(mockRequest.getParameter("go-get")).thenReturn(null);
unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
verify(mockOutputStream, times(0)).write(any(byte[].class));
@@ -104,7 +104,6 @@
@Test
public void testDoFilterWithWrongGoGetParameterValue() throws Exception {
- when(mockRequest.getServletPath()).thenReturn("/projectName");
when(mockRequest.getParameter("go-get")).thenReturn("2");
unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
verify(mockOutputStream, times(0)).write(any(byte[].class));
@@ -113,11 +112,11 @@
@Test
public void testDoFilterWithExistingProject() throws Exception {
- when(mockRequest.getServletPath()).thenReturn("/projectName");
+ when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME);
when(mockRequest.getParameter("go-get")).thenReturn("1");
- when(mockProjectCache.get(new Project.NameKey("projectName"))).thenReturn(mockProjectState);
+ when(mockProjectCache.get(new Project.NameKey(PROJECT_NAME))).thenReturn(mockProjectState);
unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
- verify(mockOutputStream, times(1)).write(PAGE_200.getBytes());
+ verify(mockOutputStream, times(1)).write(response200());
verify(mockChain, times(0)).doFilter(mockRequest, mockResponse);
verify(mockProjectCache, times(1)).get(any(Project.NameKey.class));
verify(mockResponse, times(1)).setStatus(200);
@@ -125,11 +124,11 @@
@Test
public void testDoFilterWithExistingProjectAndPackage() throws Exception {
- when(mockRequest.getServletPath()).thenReturn("/projectName/my/package");
+ when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME + "/my/package");
when(mockRequest.getParameter("go-get")).thenReturn("1");
- when(mockProjectCache.get(new Project.NameKey("projectName"))).thenReturn(mockProjectState);
+ when(mockProjectCache.get(new Project.NameKey(PROJECT_NAME))).thenReturn(mockProjectState);
unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
- verify(mockOutputStream, times(1)).write(PAGE_200.getBytes());
+ verify(mockOutputStream, times(1)).write(response200());
verify(mockChain, times(0)).doFilter(mockRequest, mockResponse);
verify(mockProjectCache, times(3)).get(any(Project.NameKey.class));
verify(mockResponse, times(1)).setStatus(200);
@@ -137,7 +136,7 @@
@Test
public void testDoFilterWithNonExistingProject() throws Exception {
- when(mockRequest.getServletPath()).thenReturn("/projectName");
+ when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME);
when(mockRequest.getParameter("go-get")).thenReturn("1");
when(mockProjectCache.get(any(Project.NameKey.class))).thenReturn(null);
unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
@@ -150,7 +149,7 @@
@Test
public void testDoFilterWithIOException() throws Exception {
String msg = "test-io-error";
- when(mockRequest.getServletPath()).thenReturn("/projectName");
+ when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME);
when(mockRequest.getParameter("go-get")).thenReturn("1");
doThrow(new IOException(msg)).when(mockOutputStream).write(any(byte[].class));
when(mockProjectCache.get(any(Project.NameKey.class))).thenReturn(mockProjectState);