Skip to content

Commit

Permalink
Updating DatabaseConfigurationTests to test Ipinfo serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
masseyke authored and joegallo committed Oct 11, 2024
1 parent 3f368e8 commit 93faac4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
DatabaseConfiguration.Maxmind.NAME,
DatabaseConfiguration.Maxmind::new
),
new NamedWriteableRegistry.Entry(
DatabaseConfiguration.Provider.class,
DatabaseConfiguration.Ipinfo.NAME,
DatabaseConfiguration.Ipinfo::new
),
new NamedWriteableRegistry.Entry(
DatabaseConfiguration.Provider.class,
DatabaseConfiguration.Local.NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.ingest.geoip.IngestGeoIpPlugin;
import org.elasticsearch.ingest.geoip.direct.DatabaseConfiguration.Ipinfo;
import org.elasticsearch.ingest.geoip.direct.DatabaseConfiguration.Local;
import org.elasticsearch.ingest.geoip.direct.DatabaseConfiguration.Maxmind;
import org.elasticsearch.ingest.geoip.direct.DatabaseConfiguration.Web;
Expand All @@ -21,6 +22,7 @@
import java.io.IOException;
import java.util.Set;

import static org.elasticsearch.ingest.geoip.direct.DatabaseConfiguration.IPINFO_NAMES;
import static org.elasticsearch.ingest.geoip.direct.DatabaseConfiguration.MAXMIND_NAMES;

public class DatabaseConfigurationTests extends AbstractXContentSerializingTestCase<DatabaseConfiguration> {
Expand All @@ -44,13 +46,14 @@ protected DatabaseConfiguration createTestInstance() {
}

public static DatabaseConfiguration randomDatabaseConfiguration(String id) {
boolean useIpinfo = randomBoolean();
DatabaseConfiguration.Provider provider = switch (between(0, 2)) {
case 0 -> new Maxmind(randomAlphaOfLength(5));
case 0 -> useIpinfo ? new Ipinfo() : new Maxmind(randomAlphaOfLength(5));
case 1 -> new Web();
case 2 -> new Local(randomAlphaOfLength(10));
default -> throw new AssertionError("failure, got illegal switch case");
};
return new DatabaseConfiguration(id, randomFrom(MAXMIND_NAMES), provider);
return new DatabaseConfiguration(id, useIpinfo ? randomFrom(IPINFO_NAMES) : randomFrom(MAXMIND_NAMES), provider);
}

@Override
Expand All @@ -61,21 +64,21 @@ protected DatabaseConfiguration mutateInstance(DatabaseConfiguration instance) {
case 1:
return new DatabaseConfiguration(
instance.id(),
randomValueOtherThan(instance.name(), () -> randomFrom(MAXMIND_NAMES)),
randomValueOtherThan(
instance.name(),
() -> instance.provider() instanceof Ipinfo ? randomFrom(IPINFO_NAMES) : randomFrom(MAXMIND_NAMES)
),
instance.provider()
);
case 2:
DatabaseConfiguration.Provider provider = instance.provider();
DatabaseConfiguration.Provider modifiedProvider;
if (provider instanceof Maxmind maxmind) {
modifiedProvider = new Maxmind(((Maxmind) instance.provider()).accountId() + randomAlphaOfLength(2));
} else if (provider instanceof Web) {
modifiedProvider = new Maxmind(randomAlphaOfLength(20)); // can't modify a Web
} else if (provider instanceof Local local) {
modifiedProvider = new Local(local.type() + randomAlphaOfLength(2));
} else {
throw new AssertionError("Unexpected provider type: " + provider.getClass());
}
DatabaseConfiguration.Provider modifiedProvider = switch (provider) {
case Maxmind maxmind -> new Maxmind(maxmind.accountId() + randomAlphaOfLength(2));
case Ipinfo ignored -> new Local(randomAlphaOfLength(20)); // can't modify Ipinfo
case Web ignored -> new Local(randomAlphaOfLength(20)); // can't modify a Web
case Local local -> new Local(local.type() + randomAlphaOfLength(2));
default -> throw new AssertionError("Unexpected provider type: " + provider.getClass());
};
return new DatabaseConfiguration(instance.id(), instance.name(), modifiedProvider);
default:
throw new AssertionError("failure, got illegal switch case");
Expand Down

0 comments on commit 93faac4

Please sign in to comment.