Merge "Get Node API fix for attribute values with '/'"
authorSourabh Sourabh <sourabh.sourabh@est.tech>
Mon, 19 Sep 2022 10:08:46 +0000 (10:08 +0000)
committerGerrit Code Review <gerrit@onap.org>
Mon, 19 Sep 2022 10:08:46 +0000 (10:08 +0000)
1  2 
cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java

@@@ -20,6 -20,7 +20,6 @@@
  
  package org.onap.cps.spi.repository;
  
 -import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
@@@ -28,18 -29,16 +28,18 @@@ import javax.persistence.PersistenceCon
  import javax.persistence.Query;
  import javax.transaction.Transactional;
  import lombok.RequiredArgsConstructor;
 +import lombok.extern.slf4j.Slf4j;
  import org.onap.cps.cpspath.parser.CpsPathPrefixType;
  import org.onap.cps.cpspath.parser.CpsPathQuery;
  import org.onap.cps.spi.entities.FragmentEntity;
  import org.onap.cps.utils.JsonObjectMapper;
  
  @RequiredArgsConstructor
 +@Slf4j
  public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCpsPathQuery {
  
-     public static final String SIMILAR_TO_ABSOLUTE_PATH_PREFIX = "%/";
-     public static final String SIMILAR_TO_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[[^/]*])?";
+     public static final String REGEX_ABSOLUTE_PATH_PREFIX = ".*\\/";
+     public static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?$";
  
      @PersistenceContext
      private EntityManager entityManager;
@@@ -51,8 -50,8 +51,8 @@@
          final StringBuilder sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE anchor_id = :anchorId");
          final Map<String, Object> queryParameters = new HashMap<>();
          queryParameters.put("anchorId", anchorId);
-         sqlStringBuilder.append(" AND xpath SIMILAR TO :xpathRegex");
-         final String xpathRegex = getSimilarToXpathSqlRegex(cpsPathQuery);
+         sqlStringBuilder.append(" AND xpath ~ :xpathRegex");
+         final String xpathRegex = getXpathSqlRegex(cpsPathQuery);
          queryParameters.put("xpathRegex", xpathRegex);
          if (cpsPathQuery.hasLeafConditions()) {
              sqlStringBuilder.append(" AND attributes @> :leafDataAsJson\\:\\:jsonb");
          addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters);
          final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class);
          setQueryParameters(query, queryParameters);
 -        return getFragmentEntitiesAsStream(query);
 -    }
 -
 -    private List<FragmentEntity> getFragmentEntitiesAsStream(final Query query) {
 -        final List<FragmentEntity> fragmentEntities = new ArrayList<>();
 -        query.getResultStream().forEach(fragmentEntity -> {
 -            fragmentEntities.add((FragmentEntity) fragmentEntity);
 -            entityManager.detach(fragmentEntity);
 -        });
 -
 +        final List<FragmentEntity> fragmentEntities = query.getResultList();
 +        log.debug("Fetched {} fragment entities by anchor and cps path.", fragmentEntities.size());
          return fragmentEntities;
      }
  
-     private static String getSimilarToXpathSqlRegex(final CpsPathQuery cpsPathQuery) {
+     private static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery) {
          final StringBuilder xpathRegexBuilder = new StringBuilder();
          if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) {
              xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix()));
          } else {
-             xpathRegexBuilder.append(SIMILAR_TO_ABSOLUTE_PATH_PREFIX);
+             xpathRegexBuilder.append(REGEX_ABSOLUTE_PATH_PREFIX);
              xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName()));
          }
-         xpathRegexBuilder.append(SIMILAR_TO_OPTIONAL_LIST_INDEX_POSTFIX);
+         xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX);
          return xpathRegexBuilder.toString();
      }