Refactor ExportConstantsTest to reduce duplicated code

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=142047229
This commit is contained in:
mcilwain 2016-12-14 12:11:23 -08:00 committed by Ben McIlwain
parent b0ebeed5a5
commit 1ed1ba20ba

View file

@ -14,9 +14,11 @@
package google.registry.export; package google.registry.export;
import static com.google.common.io.Resources.getResource; import static com.google.common.base.Strings.repeat;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.export.ExportConstants.getBackupKinds;
import static google.registry.export.ExportConstants.getReportingKinds;
import static google.registry.util.ResourceUtils.readResourceUtf8; import static google.registry.util.ResourceUtils.readResourceUtf8;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -26,7 +28,6 @@ import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.re2j.Pattern; import com.google.re2j.Pattern;
import java.net.URL;
import java.util.List; import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.junit.Test; import org.junit.Test;
@ -43,60 +44,43 @@ public class ExportConstantsTest {
private static final String UPDATE_INSTRUCTIONS_TEMPLATE = Joiner.on('\n').join( private static final String UPDATE_INSTRUCTIONS_TEMPLATE = Joiner.on('\n').join(
"", "",
"---------------------------------------------------------------------------------", repeat("-", 80),
"Your changes affect the list of %s kinds in the golden file:", "Your changes affect the list of %s kinds in the golden file:",
" %s", " %s",
"If these changes are desired, update the golden file with the following contents:", "If these changes are desired, update the golden file with the following contents:",
"=================================================================================", repeat("=", 80),
"%s", "%s",
"=================================================================================", repeat("=", 80),
""); "");
@Test @Test
public void testBackupKinds_matchGoldenBackupKindsFile() throws Exception { public void testBackupKinds_matchGoldenBackupKindsFile() throws Exception {
URL goldenBackupKindsResource = checkKindsMatchGoldenFile("backed-up", GOLDEN_BACKUP_KINDS_FILENAME, getBackupKinds());
getResource(ExportConstantsTest.class, GOLDEN_BACKUP_KINDS_FILENAME);
List<String> goldenKinds = extractListFromFile(GOLDEN_BACKUP_KINDS_FILENAME);
ImmutableSet<String> actualKinds = ExportConstants.getBackupKinds();
String updateInstructions =
getUpdateInstructions("backed-up", goldenBackupKindsResource.toString(), actualKinds);
assertWithMessage(updateInstructions)
.that(actualKinds)
.containsExactlyElementsIn(goldenKinds)
.inOrder();
} }
@Test @Test
public void testReportingKinds_matchGoldenReportingKindsFile() throws Exception { public void testReportingKinds_matchGoldenReportingKindsFile() throws Exception {
URL goldenReportingKindsResource = checkKindsMatchGoldenFile("reporting", GOLDEN_REPORTING_KINDS_FILENAME, getReportingKinds());
getResource(ExportConstantsTest.class, GOLDEN_REPORTING_KINDS_FILENAME);
List<String> goldenReportingKinds = extractListFromFile(GOLDEN_REPORTING_KINDS_FILENAME);
ImmutableSet<String> actualKinds = ExportConstants.getReportingKinds();
String updateInstructions =
getUpdateInstructions("reporting", goldenReportingKindsResource.toString(), actualKinds);
assertWithMessage(updateInstructions)
.that(actualKinds)
.containsExactlyElementsIn(goldenReportingKinds)
.inOrder();
} }
@Test @Test
public void testReportingKinds_areSubsetOfBackupKinds() throws Exception { public void testReportingKinds_areSubsetOfBackupKinds() throws Exception {
assertThat(ExportConstants.getBackupKinds()).containsAllIn(ExportConstants.getReportingKinds()); assertThat(getBackupKinds()).containsAllIn(getReportingKinds());
} }
/** private static void checkKindsMatchGoldenFile(
* Helper method to get update instructions String kindsName, String goldenFilename, ImmutableSet<String> actualKinds) {
* List<String> goldenKinds = extractListFromFile(goldenFilename);
* @param name - type of entity String updateInstructions =
* @param resource - Resource file contents String.format(
* @param actualKinds - data from ExportConstants UPDATE_INSTRUCTIONS_TEMPLATE,
* @return String of update instructions kindsName,
*/ Joiner.on('\n').join(goldenKinds),
private static String getUpdateInstructions( Joiner.on('\n').join(actualKinds));
String name, String resource, ImmutableSet<String> actualKinds) { assertWithMessage(updateInstructions)
return String.format( .that(actualKinds)
UPDATE_INSTRUCTIONS_TEMPLATE, name, resource, Joiner.on('\n').join(actualKinds)); .containsExactlyElementsIn(goldenKinds)
.inOrder();
} }
/** /**