Ejemplo n.º 1
0
  /**
   * Method to obtain a list of paths having resources of the given media type.
   *
   * @param registry the registry instance to run query on.
   * @param mediaType the media type.
   * @return an array of resource paths.
   * @throws RegistryException if the operation failed.
   */
  public static String[] getResultPaths(Registry registry, String mediaType)
      throws RegistryException {
    String[] result;
    String[] paginatedResult;
    try {
      Map<String, String> parameter = new HashMap<String, String>();
      parameter.put("1", mediaType);
      parameter.put(
          "query",
          "SELECT DISTINCT REG_PATH_ID, REG_NAME FROM REG_RESOURCE WHERE REG_MEDIA_TYPE=?");
      result = (String[]) registry.executeQuery(null, parameter).getContent();
      if (result == null || result.length == 0) {
        return new String[0];
      }
      result = removeMountPaths(result, registry);
      MessageContext messageContext = MessageContext.getCurrentMessageContext();
      if (PaginationUtils.isPaginationAnnotationFound("getPaginatedGovernanceArtifacts")
          && ((messageContext != null && PaginationUtils.isPaginationHeadersExist(messageContext))
              || PaginationContext.getInstance() != null)) {

        int rowCount = result.length;
        PaginationContext paginationContext;
        try {
          if (messageContext != null) {
            PaginationUtils.setRowCount(messageContext, Integer.toString(rowCount));
            paginationContext = PaginationUtils.initPaginationContext(messageContext);
          } else {
            paginationContext = PaginationContext.getInstance();
          }

          int start = paginationContext.getStart();
          int count = paginationContext.getCount();

          int startIndex;
          if (start == 1) {
            startIndex = 0;
          } else {
            startIndex = start;
          }
          if (rowCount < start + count) {
            paginatedResult = new String[rowCount - startIndex];
            System.arraycopy(result, startIndex, paginatedResult, 0, (rowCount - startIndex));
          } else {
            paginatedResult = new String[count];
            System.arraycopy(result, startIndex, paginatedResult, 0, count);
          }
          return paginatedResult;

        } finally {
          PaginationContext.destroy();
        }
      }
    } catch (RegistryException e) {
      String msg = "Error in getting the result for media type: " + mediaType + ".";
      log.error(msg, e);
      throw new RegistryException(msg, e);
    }
    return result;
  }
  @Test(groups = {"wso2.greg"})
  public void testPaginate() throws Exception {
    try {
      Registry gov = GovernanceUtils.getGovernanceUserRegistry(registry, "admin");
      // Should be load the governance artifact.
      GovernanceUtils.loadGovernanceArtifacts((UserRegistry) gov);
      addServices(gov);
      // Initialize the pagination context.
      // Top five services, sortBy name , and sort order descending.
      PaginationContext.init(0, 5, "DES", "overview_name", 100);
      WSRegistrySearchClient wsRegistrySearchClient = new WSRegistrySearchClient();
      // This should be execute to initialize the AttributeSearchService.
      ConfigurationContext configContext;
      String axis2Repo = FrameworkPathUtil.getSystemResourceLocation() + "client";
      String axis2Conf =
          FrameworkPathUtil.getSystemResourceLocation()
              + "axis2config"
              + File.separator
              + "axis2_client.xml";
      TestFrameworkUtils.setKeyStoreProperties(automationContext);
      configContext =
          ConfigurationContextFactory.createConfigurationContextFromFileSystem(
              axis2Repo, axis2Conf);
      configContext.setProperty(HTTPConstants.CONNECTION_TIMEOUT, TIME_OUT_VALUE);

      wsRegistrySearchClient.init(cookie, backendURL, configContext);

      //            wsRegistrySearchClient.authenticate(configContext, getServiceURL(),
      //                    automationContext.getContextTenant().getContextUser().getUserName(),
      //                    automationContext.getContextTenant().getContextUser().getPassword());

      // Initialize the GenericArtifactManager
      GenericArtifactManager artifactManager = new GenericArtifactManager(gov, "service");
      Map<String, List<String>> listMap = new HashMap<String, List<String>>();
      // Create the search attribute map
      listMap.put(
          "lcName",
          new ArrayList<String>() {
            {
              add("ServiceLifeCycle");
            }
          });
      listMap.put(
          "lcState",
          new ArrayList<String>() {
            {
              add("Development");
            }
          });
      // Find the results.
      GenericArtifact[] genericArtifacts = artifactManager.findGenericArtifacts(listMap);
      assertTrue(genericArtifacts.length > 0, "No any service found");
      assertTrue(genericArtifacts.length == 5, "Filtered service count should be 5");
      assertTrue(
          genericArtifacts[0].getQName().getLocalPart().equals("FlightService9"),
          "filter results are not sorted");
      assertTrue(
          genericArtifacts[4].getQName().getLocalPart().equals("FlightService5"),
          "filter results are not sorted");
    } finally {
      PaginationContext.destroy();
    }
  }