Skip to content

Commit

Permalink
#22495 Refactoring ESMappingUtilHelperTest.testAddMappingForFields to…
Browse files Browse the repository at this point in the history
… improve performance execution (#22506)

Co-authored-by: nollymar <[email protected]>
  • Loading branch information
nollymar and nollymar authored Jul 5, 2022
1 parent 5e4065c commit 88aef64
Showing 1 changed file with 192 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,102 +134,187 @@ public static Object[][] dataProviderValidateNewsLikeMapping() {
};
}

/*Data provider used to verify that the ES mapping is applied correctly and considers exclusions in the `es-content-mapping` file
For each scenario we define:
1) testCase name
2) Field(s) type
3) Field(s) variable name(s)
4) Expected ES mapping
/**
* Data entity used to verify that the ES mapping is applied correctly and considers exclusions in the `es-content-mapping` file
* For each scenario we define:
* 1) TestCase name
* 2) A content type where the mapping will be applied
* 3) Field type and Datatype
* 4) Field(s) variable name(s)
* 5) Expected ES mapping
* 6) Boolean indicating if the field is unique
*/
@DataProvider(formatter = PlusTestNameFormatter.class)
public static Object[][] dataProviderAddMappingForFields() {
return new Object[][] {
{ "strings_as_dates", DateField.class, DataTypes.DATE,
new String[] {"originalstartdate", "recurrencestart", "recurrenceend"}, "date", false },
private class AddMappingTestCase{
String testCaseName;
ContentType contentType;
Class fieldType;
DataTypes type;
String[] fieldsVarNames;
String expectedResult;
boolean isUnique;

public AddMappingTestCase(final String testCaseName, final ContentType contentType,
final Class fieldType, final DataTypes type, final String[] fieldsVarNames,
final String expectedResult, final boolean isUnique){
this.testCaseName = testCaseName;
this.contentType = contentType;
this.fieldType = fieldType;
this.type = type;
this.fieldsVarNames = fieldsVarNames;
this.expectedResult = expectedResult;
this.isUnique = isUnique;
}
}

{ "strings_as_date_times", DateTimeField.class, DataTypes.DATE,
new String[] {"originalstartdate", "recurrencestart", "recurrenceend"}, "date", false },
private List<AddMappingTestCase> getAddMappingForFieldsTestCases() {
return CollectionsUtils.list(
getStringsAsDatesMappingTestCase(),
getStringsAsDatesTimeMappingTestCase(),
getDatesAsTextMappingTestCase(),
getKeywordMappingTestCase(),
getGeoMappingTestCase(),
getPermissionsMappingTestCase(),
getRadioAsBooleanMappingTestCase(),
getRadioAsFloatMappingTestCase(),
getRadioAsIntegerMappingTestCase(),
getSelectAsBooleanMappingTestCase(),
getSelectAsFloatMappingTestCase(),
getSelectAsIntegerMappingTestCase(),
getTextAsFloatMappingTestCase(),
getTextAsIntegerMappingTestCase(),
getTagsMappingTestCase(),
getUniqueFieldMappingTestCase()
);
}

{ "dates_as_text", TextField.class, DataTypes.TEXT,
new String[] {"originalstartdate", "recurrencestart", "recurrenceend"}, "text", false },
private AddMappingTestCase getStringsAsDatesMappingTestCase(){
return new AddMappingTestCase("strings_as_dates", new ContentTypeDataGen().nextPersisted(),
DateField.class, DataTypes.DATE,
new String[]{"originalstartdate", "recurrencestart", "recurrenceend"}, "date",
false);
}

{ "keywordmapping", TextField.class, DataTypes.TEXT,
new String[] {"categories", "tags", "conhost",
"wfstep", "structurename", "contenttype", "parentpath",
"path", "urlmap", "moduser", "owner"}, "text", false },
private AddMappingTestCase getStringsAsDatesTimeMappingTestCase(){
return new AddMappingTestCase("strings_as_date_times",
new ContentTypeDataGen().nextPersisted(), DateTimeField.class, DataTypes.DATE,
new String[]{"originalstartdate", "recurrencestart", "recurrenceend"}, "date",
false);
}

{ "geomapping", TextField.class, DataTypes.TEXT,
new String[] {"mylatlong", "mylatlon"}, null, false },
private AddMappingTestCase getDatesAsTextMappingTestCase(){
return new AddMappingTestCase("dates_as_text",
new ContentTypeDataGen().nextPersisted(), TextField.class, DataTypes.TEXT,
new String[]{"originalstartdate", "recurrencestart", "recurrenceend"}, "text",
false);
}

{ "permissions", TextField.class, DataTypes.TEXT, new String[] {"permissions"}, "text", false },
private AddMappingTestCase getKeywordMappingTestCase(){
return new AddMappingTestCase("keywordmapping",
new ContentTypeDataGen().nextPersisted(), TextField.class, DataTypes.TEXT,
new String[] {"categories", "tags", "conhost",
"wfstep", "structurename", "contenttype", "parentpath",
"path", "urlmap", "moduser", "owner"}, "text", false);
}

{ "radio_as_boolean", RadioField.class, DataTypes.BOOL,
new String[] {"MyRadioAsBoolean"}, "boolean", false },
private AddMappingTestCase getGeoMappingTestCase(){
return new AddMappingTestCase("geomapping",
new ContentTypeDataGen().nextPersisted(), TextField.class, DataTypes.TEXT,
new String[] {"mylatlong", "mylatlon"}, null, false);
}

{ "radio_as_float", RadioField.class, DataTypes.FLOAT,
new String[] {"MyRadioAsFloat"}, "double", false },
private AddMappingTestCase getPermissionsMappingTestCase(){
return new AddMappingTestCase("permissions",
new ContentTypeDataGen().nextPersisted(), TextField.class, DataTypes.TEXT,
new String[] {"permissions"}, "text", false);
}

{ "radio_as_integer", RadioField.class, DataTypes.INTEGER,
new String[] {"MyRadioAsInteger"}, "long", false },
private AddMappingTestCase getRadioAsBooleanMappingTestCase(){
return new AddMappingTestCase("radio_as_boolean",
new ContentTypeDataGen().nextPersisted(), RadioField.class, DataTypes.BOOL,
new String[] {"MyRadioAsBoolean"}, "boolean", false);
}

{ "select_as_boolean", SelectField.class, DataTypes.BOOL,
new String[] {"MySelectAsBoolean"}, "boolean", false },
private AddMappingTestCase getRadioAsFloatMappingTestCase(){
return new AddMappingTestCase("radio_as_float",
new ContentTypeDataGen().nextPersisted(), RadioField.class, DataTypes.FLOAT,
new String[] {"MyRadioAsFloat"}, "double", false);
}

{ "select_as_float", SelectField.class, DataTypes.FLOAT,
new String[] {"MySelectAsFloat"}, "double", false },
private AddMappingTestCase getRadioAsIntegerMappingTestCase(){
return new AddMappingTestCase("radio_as_integer",
new ContentTypeDataGen().nextPersisted(), RadioField.class, DataTypes.INTEGER,
new String[] {"MyRadioAsInteger"}, "long", false);
}

{ "select_as_integer", SelectField.class, DataTypes.INTEGER,
new String[] {"MySelectAsInteger"}, "long", false },
private AddMappingTestCase getSelectAsBooleanMappingTestCase(){
return new AddMappingTestCase("select_as_boolean",
new ContentTypeDataGen().nextPersisted(), SelectField.class, DataTypes.BOOL,
new String[] {"MySelectAsBoolean"}, "boolean", false);
}

{ "text_as_float", TextField.class, DataTypes.FLOAT,
new String[] {"MyTextAsFloat"}, "double", false },
private AddMappingTestCase getSelectAsFloatMappingTestCase(){
return new AddMappingTestCase("select_as_float",
new ContentTypeDataGen().nextPersisted(), SelectField.class, DataTypes.FLOAT,
new String[] {"MySelectAsFloat"}, "double", false);
}

{ "text_as_integer", TextField.class, DataTypes.INTEGER,
new String[] {"MyTextAsInteger"}, "long", false },
private AddMappingTestCase getSelectAsIntegerMappingTestCase(){
return new AddMappingTestCase("select_as_integer",
new ContentTypeDataGen().nextPersisted(), SelectField.class, DataTypes.INTEGER,
new String[] {"MySelectAsInteger"}, "long", false);
}

{ "tags", TagField.class, DataTypes.TEXT,
new String[] {"MyTagField"}, "keyword", false },
private AddMappingTestCase getTextAsFloatMappingTestCase(){
return new AddMappingTestCase("text_as_float",
new ContentTypeDataGen().nextPersisted(), TextField.class, DataTypes.FLOAT,
new String[] {"MyTextAsFloat"}, "double", false);
}

{ "uniqueField", TextField.class, DataTypes.TEXT, new String[] {"MyUniqueField"}, "keyword", true },
};
private AddMappingTestCase getTextAsIntegerMappingTestCase(){
return new AddMappingTestCase("text_as_integer",
new ContentTypeDataGen().nextPersisted(), TextField.class, DataTypes.INTEGER,
new String[] {"MyTextAsInteger"}, "long", false);
}

private AddMappingTestCase getTagsMappingTestCase(){
return new AddMappingTestCase("tags",
new ContentTypeDataGen().nextPersisted(), TagField.class, DataTypes.TEXT,
new String[] {"MyTagField"}, "keyword", false);
}

private AddMappingTestCase getUniqueFieldMappingTestCase(){
return new AddMappingTestCase("uniqueField",
new ContentTypeDataGen().nextPersisted(), TextField.class, DataTypes.TEXT,
new String[] {"MyUniqueField"}, "keyword", true);
}

/**
* <b>Method to test:</b> Internally, it tests the method ESMappingUtilHelper.addMappingForFieldIfNeeded<p></p>
* <b>Test Case:</b> Given a field or an array of fields, the right ES mapping should be set, considering exclusions in the `es-content-mapping` file<p></p>
* <b>Expected Results:</b> Each test case should match the `expectedResult` value, which is a string with the correct datatype in ES
* @param testCase
* @param fieldType
* @param type
* @param fields
* @param expectedResult
* @throws IOException
* @throws DotIndexException
* @throws DotSecurityException
* @throws DotDataException
*/
@UseDataProvider
@Test
public void testAddMappingForFields(final String testCase, final Class fieldType,
final DataTypes type,
final String[] fields, final String expectedResult, final boolean isUnique)
public void testAddMappingForFields()
throws IOException, DotIndexException, DotSecurityException, DotDataException {

Logger.info(ESMappingUtilHelperTest.class,
String.format("Testing Add Mapping for fields defined in %s template", testCase));

final ContentType contentType = new ContentTypeDataGen().nextPersisted();

final List<ESMappingUtilHelperTest.AddMappingTestCase> testCases = getAddMappingForFieldsTestCases();
String workingIndex = null;
//Build the index name
String timestamp = String.valueOf(new Date().getTime());
try {
//Adding fields
for (final String field : fields) {
final Field newField = FieldBuilder.builder(fieldType)
.name(field).variable(field).dataType(type).contentTypeId(contentType.id())
.indexed(true).unique(isUnique).build();
fieldAPI.save(newField, user);
for (ESMappingUtilHelperTest.AddMappingTestCase testCase:testCases) {
for (final String field : testCase.fieldsVarNames) {
final Field newField = FieldBuilder.builder(testCase.fieldType)
.name(field).variable(field).dataType(testCase.type)
.contentTypeId(testCase.contentType.id())
.indexed(true).unique(testCase.isUnique).build();
fieldAPI.save(newField, user);
}
}

workingIndex = new ESIndexAPI().getNameWithClusterIDPrefix(
Expand All @@ -241,44 +326,51 @@ public void testAddMappingForFields(final String testCase, final Class fieldType
//Validate
assertTrue(result);

for (final String field : fields) {

if (expectedResult == null) {
assertFalse(UtilMethods.isSet(esMappingAPI.getFieldMappingAsMap(workingIndex,
(contentType.variable() + StringPool.PERIOD + field).toLowerCase())));
} else {
Map<String, String> mapping = (Map<String, String>) esMappingAPI
.getFieldMappingAsMap(workingIndex,
(contentType.variable() + StringPool.PERIOD + field)
.toLowerCase()).get(field.toLowerCase());
assertTrue(UtilMethods.isSet(mapping.get("type")));
assertEquals(expectedResult, mapping.get("type"));

//validate _dotraw fields
mapping = (Map<String, String>) esMappingAPI
.getFieldMappingAsMap(workingIndex,
(contentType.variable() + StringPool.PERIOD + field)
.toLowerCase() + "_dotraw").get(field.toLowerCase() + "_dotraw");
assertTrue(UtilMethods.isSet(mapping.get("type")));
assertEquals("keyword", mapping.get("type"));

//validate _sha256 mapping for unique fields
if (isUnique){
for (ESMappingUtilHelperTest.AddMappingTestCase testCase:testCases) {
for (final String field : testCase.fieldsVarNames) {
final String failureMessage = "Assert failed for test case: " + testCase.testCaseName + " and field: " + field;
if (testCase.expectedResult == null) {
assertFalse(failureMessage,
UtilMethods.isSet(esMappingAPI.getFieldMappingAsMap(workingIndex,
(testCase.contentType.variable() + StringPool.PERIOD
+ field).toLowerCase())));
} else {
Map<String, String> mapping = (Map<String, String>) esMappingAPI
.getFieldMappingAsMap(workingIndex,
(testCase.contentType.variable() + StringPool.PERIOD + field)
.toLowerCase()).get(field.toLowerCase());
assertTrue(failureMessage, UtilMethods.isSet(mapping.get("type")));
assertEquals(failureMessage, testCase.expectedResult, mapping.get("type"));

//validate _dotraw fields
mapping = (Map<String, String>) esMappingAPI
.getFieldMappingAsMap(workingIndex,
(testCase.contentType.variable() + StringPool.PERIOD + field)
.toLowerCase() + "_dotraw")
.get(field.toLowerCase() + "_dotraw");
assertTrue(failureMessage, UtilMethods.isSet(mapping.get("type")));
assertEquals(failureMessage, "keyword", mapping.get("type"));

//validate _sha256 mapping for unique fields
if (testCase.isUnique) {
mapping = (Map<String, String>) esMappingAPI
.getFieldMappingAsMap(workingIndex,
(testCase.contentType.variable() + StringPool.PERIOD + field)
.toLowerCase() + ESUtils.SHA_256)
.get(field.toLowerCase() + ESUtils.SHA_256);
assertTrue(failureMessage, UtilMethods.isSet(mapping.get("type")));
assertEquals(failureMessage, "keyword", mapping.get("type"));
}

//validate _text fields
mapping = (Map<String, String>) esMappingAPI
.getFieldMappingAsMap(workingIndex,
(contentType.variable() + StringPool.PERIOD + field)
.toLowerCase() + ESUtils.SHA_256).get(field.toLowerCase() + ESUtils.SHA_256);
assertTrue(UtilMethods.isSet(mapping.get("type")));
assertEquals("keyword", mapping.get("type"));
(testCase.contentType.variable() + StringPool.PERIOD + field)
.toLowerCase() + ESMappingAPIImpl.TEXT)
.get(field.toLowerCase() + ESMappingAPIImpl.TEXT);
assertTrue(failureMessage, UtilMethods.isSet(mapping.get("type")));
assertEquals(failureMessage, "text", mapping.get("type"));
}

//validate _text fields
mapping = (Map<String, String>) esMappingAPI
.getFieldMappingAsMap(workingIndex,
(contentType.variable() + StringPool.PERIOD + field)
.toLowerCase() + ESMappingAPIImpl.TEXT).get(field.toLowerCase() + ESMappingAPIImpl.TEXT);
assertTrue(UtilMethods.isSet(mapping.get("type")));
assertEquals("text", mapping.get("type"));
}
}

Expand All @@ -287,9 +379,10 @@ public void testAddMappingForFields(final String testCase, final Class fieldType
if (workingIndex != null) {
contentletIndexAPI.delete(workingIndex);
}

if (contentType != null) {
contentTypeAPI.delete(contentType);
for (ESMappingUtilHelperTest.AddMappingTestCase testCase:testCases) {
if (testCase.contentType != null) {
contentTypeAPI.delete(testCase.contentType);
}
}
}
}
Expand Down

0 comments on commit 88aef64

Please sign in to comment.