Move all testdata reads to use TestDataHelper, and made tests more fluent

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=192204510
This commit is contained in:
guyben 2018-04-09 16:11:14 -07:00 committed by Ben McIlwain
parent a8b6195ce2
commit 24498ff97b
7 changed files with 56 additions and 58 deletions

View file

@ -16,8 +16,7 @@ java_library(
"**/*.java", "**/*.java",
]), ]),
resources = [ resources = [
"schema.txt", ] + glob(["**/testdata/*"]),
] + glob(["**/testdata/*.xml"]),
deps = [ deps = [
"//java/google/registry/config", "//java/google/registry/config",
"//java/google/registry/dns/writer", "//java/google/registry/dns/writer",

View file

@ -14,8 +14,6 @@
package google.registry.model; package google.registry.model;
import static com.google.common.io.Resources.getResource;
import google.registry.testing.AppEngineRule; import google.registry.testing.AppEngineRule;
import google.registry.testing.GoldenFileTestHelper; import google.registry.testing.GoldenFileTestHelper;
import org.junit.Rule; import org.junit.Rule;
@ -36,10 +34,9 @@ public class SchemaVersionTest {
@Test @Test
public void testGoldenSchemaFile() throws Exception { public void testGoldenSchemaFile() throws Exception {
GoldenFileTestHelper.testGoldenFile( GoldenFileTestHelper.assertThat(SchemaVersion.getSchema())
SchemaVersion.getSchema(), .describedAs("Datastore schema")
getResource(SchemaVersionTest.class, "schema.txt"), .createdByNomulusCommand("get_schema")
"Datastore schema", .isEqualToGolden(SchemaVersionTest.class, "schema.txt");
"get_schema");
} }
} }

View file

@ -14,9 +14,6 @@
package google.registry.module.backend; package google.registry.module.backend;
import static com.google.common.io.Resources.getResource;
import google.registry.request.RouterDisplayHelper;
import google.registry.testing.GoldenFileTestHelper; import google.registry.testing.GoldenFileTestHelper;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -28,10 +25,8 @@ public class BackendRequestComponentTest {
@Test @Test
public void testRoutingMap() throws Exception { public void testRoutingMap() throws Exception {
GoldenFileTestHelper.testGoldenFile( GoldenFileTestHelper.assertThatRoutesFromComponent(BackendRequestComponent.class)
RouterDisplayHelper.extractHumanReadableRoutesFromComponent(BackendRequestComponent.class), .describedAs("backend routing map")
getResource(BackendRequestComponentTest.class, "testdata/backend_routing.txt"), .isEqualToGolden(BackendRequestComponentTest.class, "backend_routing.txt");
"backend routing map",
"get_routing_map -c " + BackendRequestComponent.class.getName());
} }
} }

View file

@ -14,9 +14,7 @@
package google.registry.module.frontend; package google.registry.module.frontend;
import static com.google.common.io.Resources.getResource;
import google.registry.request.RouterDisplayHelper;
import google.registry.testing.GoldenFileTestHelper; import google.registry.testing.GoldenFileTestHelper;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -28,10 +26,8 @@ public class FrontendRequestComponentTest {
@Test @Test
public void testRoutingMap() throws Exception { public void testRoutingMap() throws Exception {
GoldenFileTestHelper.testGoldenFile( GoldenFileTestHelper.assertThatRoutesFromComponent(FrontendRequestComponent.class)
RouterDisplayHelper.extractHumanReadableRoutesFromComponent(FrontendRequestComponent.class), .describedAs("frontend routing map")
getResource(FrontendRequestComponentTest.class, "testdata/frontend_routing.txt"), .isEqualToGolden(FrontendRequestComponentTest.class, "frontend_routing.txt");
"frontend routing map",
"get_routing_map -c " + FrontendRequestComponent.class.getName());
} }
} }

View file

@ -14,9 +14,6 @@
package google.registry.module.tools; package google.registry.module.tools;
import static com.google.common.io.Resources.getResource;
import google.registry.request.RouterDisplayHelper;
import google.registry.testing.GoldenFileTestHelper; import google.registry.testing.GoldenFileTestHelper;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -28,10 +25,8 @@ public class ToolsRequestComponentTest {
@Test @Test
public void testRoutingMap() throws Exception { public void testRoutingMap() throws Exception {
GoldenFileTestHelper.testGoldenFile( GoldenFileTestHelper.assertThatRoutesFromComponent(ToolsRequestComponent.class)
RouterDisplayHelper.extractHumanReadableRoutesFromComponent(ToolsRequestComponent.class), .describedAs("tools routing map")
getResource(ToolsRequestComponentTest.class, "testdata/tools_routing.txt"), .isEqualToGolden(ToolsRequestComponentTest.class, "tools_routing.txt");
"tools routing map",
"get_routing_map -c " + ToolsRequestComponent.class.getName());
} }
} }

View file

@ -14,12 +14,14 @@
package google.registry.testing; package google.registry.testing;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assert_; import static com.google.common.truth.Truth.assert_;
import static google.registry.util.ResourceUtils.readResourceUtf8; import static google.registry.testing.TestDataHelper.filePath;
import static google.registry.testing.TestDataHelper.loadFile;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import java.net.MalformedURLException; import google.registry.request.RouterDisplayHelper;
import java.net.URL;
/** /**
* Helper class to compare a string against a golden file and print out update instructions if * Helper class to compare a string against a golden file and print out update instructions if
@ -27,8 +29,12 @@ import java.net.URL;
*/ */
public class GoldenFileTestHelper { public class GoldenFileTestHelper {
String actualValue = null;
String nomulusCommand = null;
String goldenFileDescription = null;
private static final String UPDATE_COMMAND = private static final String UPDATE_COMMAND =
"google.registry.tools.RegistryTool -e localhost %1$s >javatests%2$s"; "google.registry.tools.RegistryTool -e localhost %1$s > %2$s";
private static final String UPDATE_INSTRUCTIONS = private static final String UPDATE_INSTRUCTIONS =
Joiner.on('\n') Joiner.on('\n')
@ -39,36 +45,46 @@ public class GoldenFileTestHelper {
UPDATE_COMMAND, UPDATE_COMMAND,
""); "");
private static String getPathProper(URL url) throws MalformedURLException {
String protocol = url.getProtocol(); public static GoldenFileTestHelper assertThat(String actualValue) {
if (protocol.equals("jar")) { return new GoldenFileTestHelper().setActualValue(actualValue);
url = new URL(url.getPath());
protocol = url.getProtocol();
}
if (protocol.equals("file")) {
String[] components = url.getPath().split("!");
if (components.length >= 2) {
return components[1];
}
}
return url.getPath();
} }
public static void testGoldenFile( public static GoldenFileTestHelper assertThatRoutesFromComponent(Class<?> component) {
String actualValue, return assertThat(RouterDisplayHelper.extractHumanReadableRoutesFromComponent(component))
URL goldenFileUrl, .createdByNomulusCommand("get_routing_map -c " + component.getName());
String goldenFileDescription, }
String nomulusCommand)
throws Exception { public GoldenFileTestHelper createdByNomulusCommand(String nomulusCommand) {
// Don't use Truth's isEqualTo() because the output is huge and unreadable for large files. checkState(this.nomulusCommand == null, "Trying to set nomulus command twice");
if (!actualValue.equals(readResourceUtf8(goldenFileUrl).trim())) { this.nomulusCommand = checkNotNull(nomulusCommand);
return this;
}
public GoldenFileTestHelper describedAs(String goldenFileDescription) {
checkState(this.goldenFileDescription == null, "Trying to set description twice");
this.goldenFileDescription = checkNotNull(goldenFileDescription);
return this;
}
public void isEqualToGolden(Class<?> context, String filename) {
checkNotNull(nomulusCommand, "Didn't set nomulus command");
checkNotNull(goldenFileDescription, "Didn't set description");
checkNotNull(context);
checkNotNull(filename);
if (!actualValue.equals(loadFile(context, filename).trim())) {
assert_() assert_()
.fail( .fail(
String.format( String.format(
UPDATE_INSTRUCTIONS, UPDATE_INSTRUCTIONS,
nomulusCommand, nomulusCommand,
getPathProper(goldenFileUrl), filePath(context, filename),
goldenFileDescription)); goldenFileDescription));
} }
} }
private GoldenFileTestHelper setActualValue(String actualValue) {
this.actualValue = checkNotNull(actualValue);
return this;
}
} }