Skip to content

Commit

Permalink
EPMRPP-96537 fix attributes with semicolon mapping (#1053)
Browse files Browse the repository at this point in the history
* EPMRPP-96837 fix attributes with semicolon mapping
  • Loading branch information
grabsefx authored Dec 10, 2024
1 parent 1e6fe09 commit 72f99ff
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import static com.epam.ta.reportportal.jooq.Tables.WIDGET;
import static com.epam.ta.reportportal.jooq.tables.JOwnedEntity.OWNED_ENTITY;
import static org.jooq.impl.DSL.choose;
import static org.jooq.impl.DSL.coalesce;
import static org.jooq.impl.DSL.field;

import com.epam.ta.reportportal.commons.querygen.constant.TestItemCriteriaConstant;
Expand Down Expand Up @@ -550,15 +551,13 @@ private List<Field<?>> getSelectSimpleFields() {
}

private List<Field<?>> getSelectAggregatedFields() {
return Lists.newArrayList(DSL.arrayAgg(
DSL.field("concat({0}, {1}, {2}, {3}, {4})",
ITEM_ATTRIBUTE.KEY,
KEY_VALUE_SEPARATOR,
ITEM_ATTRIBUTE.VALUE,
KEY_VALUE_SEPARATOR,
ITEM_ATTRIBUTE.SYSTEM
)).as(ATTRIBUTE_ALIAS)
);
return Lists.newArrayList(
DSL.arrayAgg(DSL.jsonArray(
coalesce(ITEM_ATTRIBUTE.KEY, ""),
coalesce(ITEM_ATTRIBUTE.VALUE, ""),
ITEM_ATTRIBUTE.SYSTEM
))
.as(ATTRIBUTE_ALIAS));
}
},

Expand Down Expand Up @@ -989,15 +988,13 @@ private List<Field<?>> getSelectSimpleFields() {
}

private List<Field<?>> getSelectAggregatedFields() {
return Lists.newArrayList(DSL.arrayAgg(
DSL.field("concat({0}, {1}, {2}, {3}, {4})",
ITEM_ATTRIBUTE.KEY,
KEY_VALUE_SEPARATOR,
ITEM_ATTRIBUTE.VALUE,
KEY_VALUE_SEPARATOR,
ITEM_ATTRIBUTE.SYSTEM
)).as(ATTRIBUTE_ALIAS)
);
return Lists.newArrayList(
DSL.arrayAgg(DSL.jsonArray(
coalesce(ITEM_ATTRIBUTE.KEY, ""),
coalesce(ITEM_ATTRIBUTE.VALUE, ""),
ITEM_ATTRIBUTE.SYSTEM
))
.as(ATTRIBUTE_ALIAS));
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.stream.Collectors;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.JSON;
import org.jooq.SortField;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -186,7 +187,7 @@ public Page<Launch> findAllLatestByFilter(Queryable filter, Pageable pageable) {
List<Field<?>> simpleSelectedFields = getLaunchSimpleSelectedFields();

List<Field<?>> selectFields = new ArrayList<>(simpleSelectedFields);
selectFields.add(getAttributeConcatedFields());
selectFields.addAll(getAttributeConcatenatedFields());

List<Field<?>> groupFields = new ArrayList<>(simpleSelectedFields);
for (SortField<?> sortField : sortFieldList) {
Expand Down Expand Up @@ -223,11 +224,14 @@ public Page<Launch> findAllLatestByFilter(Queryable filter, Pageable pageable) {
);
}

private Field<String[]> getAttributeConcatedFields() {
return DSL.arrayAgg(DSL.concat(
ITEM_ATTRIBUTE.KEY, DSL.val(":"),
ITEM_ATTRIBUTE.VALUE, DSL.val(":"),
ITEM_ATTRIBUTE.SYSTEM)).as(ATTRIBUTE_ALIAS);
private ArrayList<Field<JSON[]>> getAttributeConcatenatedFields() {
return Lists.newArrayList(
DSL.arrayAgg(DSL.jsonArray(
coalesce(ITEM_ATTRIBUTE.KEY, ""),
coalesce(ITEM_ATTRIBUTE.VALUE, ""),
ITEM_ATTRIBUTE.SYSTEM
))
.as(ATTRIBUTE_ALIAS));
}

private ArrayList<Field<?>> getLaunchSimpleSelectedFields() {
Expand Down Expand Up @@ -263,7 +267,7 @@ public Optional<Launch> findLastRun(Long projectId, String mode) {
List<Field<?>> simpleSelectedFields = getLaunchSimpleSelectedFields();

List<Field<?>> selectFields = new ArrayList<>(simpleSelectedFields);
selectFields.add(getAttributeConcatedFields());
selectFields.addAll(getAttributeConcatenatedFields());

return LAUNCH_FETCHER.apply(dsl.fetch(dsl.with(FILTERED_QUERY)
.as(dsl.select(LAUNCH.ID)
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/com/epam/ta/reportportal/dao/util/RecordMappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand All @@ -118,6 +121,7 @@
import java.util.function.Function;
import org.apache.logging.log4j.util.Strings;
import org.jooq.Field;
import org.jooq.JSON;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.Result;
Expand Down Expand Up @@ -507,17 +511,19 @@ public class RecordMappers {
List<ItemAttribute> attributeList = new ArrayList<>();

if (r.get(ATTRIBUTE_ALIAS) != null) {
String[] attributesArray = r.get(ATTRIBUTE_ALIAS, String[].class);
List<JSON> attributesArray = r.get(ATTRIBUTE_ALIAS, List.class);
Gson gson = new Gson();
Type listType = new TypeToken<List<String>>() {}.getType();

for (String attributeString : attributesArray) {
if (Strings.isEmpty(attributeString)) {
for (JSON attributeEntry : attributesArray) {
if (attributeEntry == null) {
continue;
}
String[] attributes = gson.<List<String>>fromJson(attributeEntry.data(), listType)
.toArray(new String[0]);

// explode attributes from string "key:value:system"
String[] attributes = attributeString.split(":", -1);
if (attributes.length > 1 && (Strings.isNotEmpty(attributes[0]) || Strings.isNotEmpty(
attributes[1]))) {
if (attributes.length > 1 && (Strings.isNotEmpty(attributes[0])
|| Strings.isNotEmpty(attributes[1]))) {
Boolean systemAttribute;
//Case when system attribute is retrieved as 't' or 'f'
if ("t".equals(attributes[2]) || "f".equals(attributes[2])) {
Expand All @@ -532,7 +538,7 @@ public class RecordMappers {
}
}

if (attributeList.size() > 0) {
if (!attributeList.isEmpty()) {
return Optional.of(attributeList);
} else {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ void compositeAttributeHas() {

assertEquals(1, launches.size());
assertEquals(1L, launches.get(0).getId());

launches = launchRepository.findByFilter(
buildFilterWithConditions(buildCompositeAttributeCondition(Condition.HAS,
"key:semi:value:colon",
false
)));

assertEquals(1, launches.size());
assertEquals(1L, launches.get(0).getId());

launches = launchRepository.findByFilter(
buildFilterWithConditions(buildCompositeAttributeCondition(Condition.HAS,
"key:semi:value:colon,key1:value1,key2:value2,key3:value3",
false
)));

assertEquals(1, launches.size());
assertEquals(1L, launches.get(0).getId());
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/db/fill/launch/launch-filtering-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ INSERT INTO item_attribute(key, value, launch_id)
VALUES ('key2', 'value2', 1);
INSERT INTO item_attribute(key, value, launch_id)
VALUES ('key3', 'value3', 1);
INSERT INTO item_attribute(key, value, launch_id)
VALUES ('key:semi', 'value:colon', 1);

INSERT INTO launch(id, uuid, project_id, user_id, name, start_time, end_time, last_modified, mode,
status)
Expand Down

0 comments on commit 72f99ff

Please sign in to comment.