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",
]),
resources = [
"schema.txt",
] + glob(["**/testdata/*.xml"]),
] + glob(["**/testdata/*"]),
deps = [
"//java/google/registry/config",
"//java/google/registry/dns/writer",

View file

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

View file

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

View file

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

View file

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

View file

@ -14,12 +14,14 @@
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 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 java.net.MalformedURLException;
import java.net.URL;
import google.registry.request.RouterDisplayHelper;
/**
* 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 {
String actualValue = null;
String nomulusCommand = null;
String goldenFileDescription = null;
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 =
Joiner.on('\n')
@ -39,36 +45,46 @@ public class GoldenFileTestHelper {
UPDATE_COMMAND,
"");
private static String getPathProper(URL url) throws MalformedURLException {
String protocol = url.getProtocol();
if (protocol.equals("jar")) {
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 GoldenFileTestHelper assertThat(String actualValue) {
return new GoldenFileTestHelper().setActualValue(actualValue);
}
public static void testGoldenFile(
String actualValue,
URL goldenFileUrl,
String goldenFileDescription,
String nomulusCommand)
throws Exception {
// Don't use Truth's isEqualTo() because the output is huge and unreadable for large files.
if (!actualValue.equals(readResourceUtf8(goldenFileUrl).trim())) {
public static GoldenFileTestHelper assertThatRoutesFromComponent(Class<?> component) {
return assertThat(RouterDisplayHelper.extractHumanReadableRoutesFromComponent(component))
.createdByNomulusCommand("get_routing_map -c " + component.getName());
}
public GoldenFileTestHelper createdByNomulusCommand(String nomulusCommand) {
checkState(this.nomulusCommand == null, "Trying to set nomulus command twice");
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_()
.fail(
String.format(
UPDATE_INSTRUCTIONS,
nomulusCommand,
getPathProper(goldenFileUrl),
filePath(context, filename),
goldenFileDescription));
}
}
private GoldenFileTestHelper setActualValue(String actualValue) {
this.actualValue = checkNotNull(actualValue);
return this;
}
}