Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: UUID rate semver FPF-14284 #38

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.8'
implementation group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'

implementation group: 'com.telefonica.baikal', name: 'spark-commons_2.12', version: '2.2.0'
implementation group: 'com.telefonica.baikal', name: 'spark-commons_2.12', version: '55e808c6ddee0d8620bbe381459afe3428f0bec0'

// XXX: custom lib with performance improvements: https://github.com/Telefonica/libphonenumber-1
implementation group: 'com.googlecode.libphonenumber', name: 'libphonenumber', version: '8.12.25-4p'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,15 @@ public String random(String logicalType, Map propertiesProp) {
case "feature-collection":
int numFeatures = 10;
ArrayList<Feature> features = new ArrayList<>(numFeatures);

Feature feat = null;
for (int i = 0; i < numFeatures; i++) {
features.add(new Feature(new Geometry(null, null)));
}

return new FeatureCollection(features).toString();
case "uuid-string":
return UUID.randomUUID().toString();
case "semver":
return faker.regexify("(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(-((0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(\\.(0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\\+([0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*))?");
default:
throw new IllegalArgumentException("Unsupported logical type: " + logicalType);
}
Expand All @@ -315,4 +318,12 @@ public Double randomDouble(String logicalType, Map propertiesProp) {
}
}

public Float randomFloat(String logicalType, Map propertiesProp) {
switch (logicalType) {
case "rate":
return random.nextFloat();
default:
throw new IllegalArgumentException("Unsupported logical type: " + logicalType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.confluent.avro.random.generator.geojson.crs.CRS;
import io.confluent.avro.random.generator.geojson.crs.EPSG4326;
import com.telefonica.baikal.utils.Validations;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -74,27 +75,44 @@ private String randomCoordinates() {
)
).toString();
case MultiPolygon:
String first1 = new Point(null, null, crs).toString();
String first2 = new Point(null, null, crs).toString();

return Arrays.asList(
List.of(
int i = 0;
String result = null;
while (i < 10000) {
String first1 = new Point(null, null, crs).toString();
String first2 = new Point(null, null, crs).toString();
Point point1 = new Point(null, null, crs);
Point point2 = new Point(point1.latitude, null, crs);
Point point3 = new Point(null, point2.longitude, crs);
Point point4 = new Point(null, null, crs);
Point point5 = new Point(point4.latitude, null, crs);
Point point6 = new Point(null, point5.longitude, crs);
/* NOTE: The polygon is formed by 5 points due to geojson library */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Es curioso que el .isValid() no permita "triángulos" (4 puntos, el primero coincidiendo con el último).

result = Arrays.asList(
List.of(
Arrays.asList(
first1,
new Point(null, null, crs).toString(),
new Point(null, null, crs).toString(),
point1.toString(),
point2.toString(),
point3.toString(),
first1
)
),
List.of(
),
Arrays.asList(
first2,
new Point(null, null, crs).toString(),
new Point(null, null, crs).toString(),
point4.toString(),
point5.toString(),
point6.toString(),
first2
)
)
).toString();
)
).toString();

String geojson = "{\"type\": \"" + geometryType + "\", \"coordinates\": " + result + "}";
if (Validations.isValidGeometry(geojson)) {
return result;
}
i++;
}
throw new RuntimeException("Could not generate a valid MultiPolygon after 1000 attempts.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¿Sabemos las iteraciones, en media, que esto suele tardar?

default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ protected void setBounds(

public double randomLatitude() {
Random r = new Random();
return bottomBound + (upperBound - bottomBound) * r.nextDouble();
return -90 + r.nextInt(180) + r.nextDouble();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¿No funcionaba el mecanismo anterior?

}

public double randomLongitude() {
Random r = new Random();
return leftBound + (rightBound - leftBound) * r.nextDouble();
return -180 + r.nextInt(360) + r.nextDouble();
}

public abstract String toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ public void shouldCreateValidLongitude() {
@Test
public void shouldCreateValidLGeometry() {
GenericRecord record = generateRecordWithSchema("test-schemas/logical-types/geometry.json");

String field = "geometry";
assertNotNull(record.get(field));
String value = record.get(field).toString();
Expand All @@ -291,6 +292,7 @@ public void shouldCreateValidLGeometry() {
@Test
public void shouldCreateValidLFeature() {
GenericRecord record = generateRecordWithSchema("test-schemas/logical-types/feature.json");

String field = "feature";
assertNotNull(record.get(field));
String value = record.get(field).toString();
Expand All @@ -301,11 +303,44 @@ public void shouldCreateValidLFeature() {
@Test
public void shouldCreateValidLFeatureCollection() {
GenericRecord record = generateRecordWithSchema("test-schemas/logical-types/feature-collection.json");

String field = "featureCollection";
assertNotNull(record.get(field));
String value = record.get(field).toString();
System.out.println("Generated value is: " + value);
assertTrue("Invalid feature-collection: " + value, Validations.isValidFeatureCollection(value));
}

@Test
public void shouldCreateValidUUID() {
GenericRecord record = generateRecordWithSchema("test-schemas/logical-types/uuid-string.json");

String field = "uuid";
assertNotNull(record.get(field));
String value = record.get(field).toString();
System.out.println("Generated value is: " + value);
assertTrue("Invalid uuid: " + value, Validations.isValidUUID(value));
}

@Test
public void shouldCreateValidRate() {
GenericRecord record = generateRecordWithSchema("test-schemas/logical-types/rate.json");

String field = "rate";
assertNotNull(record.get(field));
Float value = (float)record.get(field);
System.out.println("Generated value is: " + value);
assertTrue("Invalid rate: " + value, Validations.isValidRate(value));
}

@Test
public void shouldCreateValidSemVer() {
GenericRecord record = generateRecordWithSchema("test-schemas/logical-types/semver.json");

String field = "semver";
assertNotNull(record.get(field));
String value = record.get(field).toString();
System.out.println("Generated value is: " + value);
assertTrue("Invalid semver: " + value, Validations.isValidSemVer(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public void shouldStringifyCorrectly() {
FeatureCollection featureCollection = createFeatureCollection();
assertNotNull(featureCollection.toString());
Gson gson = new Gson();

String fcollection = featureCollection.toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No parece necesario sacarlo fuera, ¿no? Si se fuera a usar en otro sitio la variable, ahí sí.

try {
gson.fromJson(featureCollection.toString(), JsonObject.class);
} catch (JsonSyntaxException exception) {
gson.fromJson(fcollection, JsonObject.class);
} catch (Exception exception) {

exception.printStackTrace();
fail();
}

Expand Down
14 changes: 14 additions & 0 deletions app/src/test/resources/test-schemas/logical-types/rate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ "type": "record",
"name": "logicals",
"namespace": "io.confluent.avro.random.generator",
"fields":
[
{
"name": "rate",
"type": {
"type": "float",
"logicalType": "rate"
}
}
]
}
14 changes: 14 additions & 0 deletions app/src/test/resources/test-schemas/logical-types/semver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ "type": "record",
"name": "logicals",
"namespace": "io.confluent.avro.random.generator",
"fields":
[
{
"name": "semver",
"type": {
"type": "string",
"logicalType": "semver"
}
}
]
}
14 changes: 14 additions & 0 deletions app/src/test/resources/test-schemas/logical-types/uuid-string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ "type": "record",
"name": "logicals",
"namespace": "io.confluent.avro.random.generator",
"fields":
[
{
"name": "uuid",
"type": {
"type": "string",
"logicalType": "uuid-string"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
{"name":"IPV6", "type":{"type":"string", "logicalType":"ipv6", "x-fp-data-protection": "pseudonymize"}, "doc":"Internet Protocol Address v6"},
{"name":"LATITUDE", "type":{"type":"double", "logicalType":"latitude"}, "doc":"Latitude"},
{"name":"LONGITUDE", "type":{"type":"double", "logicalType":"longitude"}, "doc":"Longitude"},
{"name": "SUB_RECORD", "type":{"name":"SUB_RECORD", "type": "record", "fields": [
{"name":"DECIMAL", "type": {"type": "string", "logicalType": "decimal-string"}, "doc": "A string representation of a decimal numeral"},
{"name":"GEOMETRY", "aliases": ["geometry"], "type": {"type": "string", "logicalType": "geometry"}, "doc": "A Geometry object represents points, curves, and surfaces in coordinate space"},
{"name":"FEATURE", "aliases": ["feature"], "type": {"type": "string", "logicalType": "feature"}, "doc": "A Feature object represents a spatially bounded thing"},
{"name":"FEATURE_COLLECTION", "aliases": ["feature_collection"], "type": {"type": "string", "logicalType": "feature-collection"}, "doc": "List of features"},
{"name":"UUID", "aliases": ["uuid-string"], "type": {"type": "string", "logicalType": "uuid-string"}, "doc": "A string representation of a UUID"},
{"name":"RATE", "aliases": ["rate"], "type": {"type": "float", "logicalType": "rate"}, "doc": "A float representation of a rate"},
{"name":"SEMVER", "aliases": ["semver"], "type": {"type": "string", "logicalType": "semver"}, "doc": "A string representation of a SemVer"},
{"name":"SUB_RECORD", "type":{"name":"SUB_RECORD", "type": "record", "fields": [
{"name":"PHONE_NUMBER", "type": { "type": "string", "logicalType": "phone-number", "x-fp-data-protection": "pseudonymize" }},
{"name":"IMSI", "type": { "type":"string", "logicalType":"imsi", "x-fp-data-protection": "pseudonymize" }},
{"name":"DATETIME", "type": { "type":"string", "logicalType":"datetime" }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@
"logicalType": "feature-collection"
},
"doc": "List of features"
},
{
"name": "UUID",
"aliases": ["uuid-string"],
"type": {
"type": "string",
"logicalType": "uuid-string"
},
"doc": "A string representation of a UUID"
},
{
"name": "RATE",
"aliases": ["rate"],
"type": {
"type": "float",
"logicalType": "rate"
},
"doc": "A float representation of a rate"
},
{
"name": "SEMVER",
"aliases": ["semver"],
"type": {
"type": "string",
"logicalType": "semver"
},
"doc": "A string representation of a SemVer"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,14 @@
"name" : "FEATURE_COLLECTION",
"type" : {
"type" : "string",
"logicalType" : "feature_collection"
"logicalType" : "feature-collection"
},
"doc" : "List of features",
"aliases" : [ "feature_collection" ]
} ]
},
{"name":"UUID","aliases":["uuid"],"type":{"type":"string","logicalType":"uuid-string"},"doc":"A string representation of uuid"},
{"name":"SEMVER","aliases":["SemVer"],"type":{"type":"string","logicalType":"semver"},"doc":"A string representation of SemVer"},
{"name":"RATE","aliases":["rate"],"type":{"type":"float","logicalType":"rate"},"doc":"A float representation of rate"} ]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@
"name" : "FEATURE_COLLECTION",
"type" : {
"type" : "string",
"logicalType" : "feature_collection"
"logicalType" : "feature-collection"
},
"doc" : "List of features",
"aliases" : [ "feature_collection" ]
} ]
},
{"name":"UUID","aliases":["uuid"],"type":{"type":"string","logicalType":"uuid-string"},"doc":"A string representation of uuid"},
{"name":"SEMVER","aliases":["SemVer"],"type":{"type":"string","logicalType":"semver"},"doc":"A string representation of SemVer"},
{"name":"RATE","aliases":["rate"],"type":{"type":"float","logicalType":"rate"},"doc":"A float representation of rate"} ]
}
}
} ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,14 @@
"name" : "FEATURE_COLLECTION",
"type" : {
"type" : "string",
"logicalType" : "feature_collection"
"logicalType" : "feature-collection"
},
"doc" : "List of features",
"aliases" : [ "feature_collection" ]
} ]
},
{"name":"UUID","aliases":["uuid"],"type":{"type":"string","logicalType":"uuid-string"},"doc":"A string representation of uuid"},
{"name":"SEMVER","aliases":["SemVer"],"type":{"type":"string","logicalType":"semver"},"doc":"A string representation of SemVer"},
{"name":"RATE","aliases":["rate"],"type":{"type":"float","logicalType":"rate"},"doc":"A float representation of rate"} ]
}
}
} ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,14 @@
"name" : "FEATURE_COLLECTION",
"type" : {
"type" : "string",
"logicalType" : "feature_collection"
"logicalType" : "feature-collection"
},
"doc" : "List of features",
"aliases" : [ "feature_collection" ]
} ]
},
{"name":"UUID","aliases":["uuid"],"type":{"type":"string","logicalType":"uuid-string"},"doc":"A string representation of uuid"},
{"name":"SEMVER","aliases":["SemVer"],"type":{"type":"string","logicalType":"semver"},"doc":"A string representation of SemVer"},
{"name":"RATE","aliases":["rate"],"type":{"type":"float","logicalType":"rate"},"doc":"A float representation of rate"} ]
}
} ]
}
Expand Down
Loading
Loading