Example #1
0
 public String componentParameterJsonSchema(String componentName) throws Exception {
   // favor using pre generated schema if component has that
   String json = context.getComponentParameterJsonSchema(componentName);
   if (json == null) {
     // okay this requires having the component on the classpath and being instantiated
     Component component = context.getComponent(componentName);
     if (component != null) {
       ComponentConfiguration configuration = component.createComponentConfiguration();
       json = configuration.createParameterJsonSchema();
     }
   }
   return json;
 }
  @Test
  public void testComponentConfiguration() throws Exception {
    TimerComponent comp = context.getComponent("timer", TimerComponent.class);
    EndpointConfiguration conf = comp.createConfiguration("timer:foo?period=2000");

    assertEquals("2000", conf.getParameter("period"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);

    assertTrue(json.contains("\"timerName\": { \"type\": \"java.lang.String\" }"));
    assertTrue(json.contains("\"delay\": { \"type\": \"long\" }"));
  }
  @Test
  public void testComponentConfiguration() throws Exception {
    TestComponent comp = context.getComponent("test", TestComponent.class);
    EndpointConfiguration conf = comp.createConfiguration("test:my:foo?timeout=1000");

    assertEquals("1000", conf.getParameter("timeout"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);

    assertTrue(json.contains("\"retainFirst\": { \"type\": \"int\" }"));
    assertTrue(json.contains("\"timeout\": { \"type\": \"long\" }"));
  }
  @Test
  public void testComponentConfiguration() throws Exception {
    DirectComponent comp = context.getComponent("direct", DirectComponent.class);
    EndpointConfiguration conf = comp.createConfiguration("direct:foo?block=true");

    assertEquals("true", conf.getParameter("block"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);

    assertTrue(json.contains("\"timeout\": { \"type\": \"long\" }"));
    assertTrue(json.contains("\"block\": { \"type\": \"boolean\" }"));
  }
  @Test
  public void testComponentConfiguration() throws Exception {
    SqlComponent comp = context.getComponent("sql", SqlComponent.class);
    EndpointConfiguration conf =
        comp.createConfiguration(
            "sql:select?dataSourceRef=jdbc/myDataSource&allowNamedParameters=true&consumer.delay=5000");

    assertEquals("jdbc/myDataSource", conf.getParameter("dataSourceRef"));
    assertEquals("true", conf.getParameter("allowNamedParameters"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);
  }
Example #6
0
 public List<String> completeEndpointPath(
     String componentName, Map<String, Object> endpointParameters, String completionText)
     throws Exception {
   if (completionText == null) {
     completionText = "";
   }
   Component component = context.getComponent(componentName, false);
   if (component != null) {
     ComponentConfiguration configuration = component.createComponentConfiguration();
     configuration.setParameters(endpointParameters);
     return configuration.completeEndpointPath(completionText);
   } else {
     return new ArrayList<String>();
   }
 }
  @Test
  public void testComponentConfiguration() throws Exception {
    NettyHttpComponent comp = context.getComponent("netty4-http", NettyHttpComponent.class);
    EndpointConfiguration conf =
        comp.createConfiguration(
            "netty4-http:tcp://localhost:5150?sync=true"
                + "&httpMethodRestrict=POST&traceEnabled=true");

    assertEquals("true", conf.getParameter("traceEnabled"));
    assertEquals("POST", conf.getParameter("httpMethodRestrict"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);
  }
  @Test
  public void testComponentConfiguration() throws Exception {
    NettyComponent comp = context.getComponent("netty4", NettyComponent.class);
    EndpointConfiguration conf =
        comp.createConfiguration(
            "netty4:tcp://localhost:5150?sync=true"
                + "&maximumPoolSize=32&ssl=true&passphrase=#password");

    assertEquals("true", conf.getParameter("sync"));
    assertEquals("32", conf.getParameter("maximumPoolSize"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);

    assertTrue(json.contains("\"producerPoolMinEvictableIdle\": { \"type\": \"integer\" }"));
    assertTrue(json.contains("\"allowDefaultCodec\": { \"type\": \"boolean\" }"));
  }
  @Test
  public void testComponentConfiguration() throws Exception {
    DataSetComponent comp = context.getComponent("dataset", DataSetComponent.class);
    EndpointConfiguration conf =
        comp.createConfiguration(
            "dataset:foo?minRate=3&produceDelay=33&consumeDelay=333&preloadSize=3333&initialDelay=33333&disableDataSetIndex=true");

    assertEquals(
        "Unexpected endpoint configuration value for minRate", "3", conf.getParameter("minRate"));
    assertEquals(
        "Unexpected endpoint configuration value for produceDelay",
        "33",
        conf.getParameter("produceDelay"));
    assertEquals(
        "Unexpected endpoint configuration value for consumeDelay",
        "333",
        conf.getParameter("consumeDelay"));
    assertEquals(
        "Unexpected endpoint configuration value for preloadSize",
        "3333",
        conf.getParameter("preloadSize"));
    assertEquals(
        "Unexpected endpoint configuration value for initialDelay",
        "33333",
        conf.getParameter("initialDelay"));
    assertEquals(
        "Unexpected endpoint configuration value for disableDataSetIndex",
        "true",
        conf.getParameter("disableDataSetIndex"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);

    assertTrue(
        json.contains(
            "\"name\": { \"kind\": \"path\", \"group\": \"common\", \"required\": \"true\", \"type\""));
    assertTrue(
        json.contains(
            "\"kind\": \"parameter\", \"group\": \"consumer\", \"label\": \"consumer\", \"type\": \"integer\""));
    assertTrue(
        json.contains(
            "\"retainFirst\": { \"kind\": \"parameter\", \"group\": \"producer\", \"label\": \"producer\", \"type\": \"integer"));
  }
 public static void assertParameterConfig(
     ComponentConfiguration configuration, String name, Class<?> parameterType) {
   ParameterConfiguration config = configuration.getParameterConfiguration(name);
   assertNotNull("ParameterConfiguration should exist for parameter name " + name, config);
   assertEquals("ParameterConfiguration." + name + ".getName()", name, config.getName());
   assertEquals(
       "ParameterConfiguration." + name + ".getParameterType()",
       parameterType,
       config.getParameterType());
 }
  @Test
  public void testComponentConfiguration() throws Exception {
    DataSetComponent comp = context.getComponent("dataset", DataSetComponent.class);
    EndpointConfiguration conf = comp.createConfiguration("dataset:foo?minRate=3");

    assertEquals("3", conf.getParameter("minRate"));

    ComponentConfiguration compConf = comp.createComponentConfiguration();
    String json = compConf.createParameterJsonSchema();
    assertNotNull(json);

    assertTrue(json.contains("\"preloadSize\": { \"kind\": \"parameter\", \"type\": \"integer\""));
    assertTrue(json.contains("\"minRate\": { \"kind\": \"parameter\", \"type\": \"integer\""));
    assertTrue(
        json.contains(
            "\"exchangePattern\": { \"kind\": \"parameter\", \"label\": \"advanced\", \"type\": \"string\", \"javaType\": \"org.apache.camel.ExchangePattern\""
                + ", \"enum\": [ \"InOnly\", \"RobustInOnly\", \"InOut\", \"InOptionalOut\", \"OutOnly\", \"RobustOutOnly\", \"OutIn\", \"OutOptionalIn\" ]"));
    assertTrue(json.contains("\"InOut\""));
  }
  @Test
  public void testConfiguration() throws Exception {
    Component component = context().getComponent(componentName);
    ComponentConfiguration configuration = component.createComponentConfiguration();
    SortedMap<String, ParameterConfiguration> parameterConfigurationMap =
        configuration.getParameterConfigurationMap();
    if (verbose) {
      Set<Map.Entry<String, ParameterConfiguration>> entries = parameterConfigurationMap.entrySet();
      for (Map.Entry<String, ParameterConfiguration> entry : entries) {
        String name = entry.getKey();
        ParameterConfiguration config = entry.getValue();
        LOG.info("Has name: {} with type {}", name, config.getParameterType().getName());
      }
    }

    assertParameterConfig(configuration, "format", PayloadFormat.class);
    assertParameterConfig(configuration, "sObjectName", String.class);
    assertParameterConfig(configuration, "sObjectFields", String.class);
    assertParameterConfig(configuration, "updateTopic", boolean.class);

    configuration.setParameter("format", PayloadFormat.XML);
    configuration.setParameter("sObjectName", "Merchandise__c");
    configuration.setParameter("sObjectFields", "Description__c,Total_Inventory__c");
    configuration.setParameter("updateTopic", false);

    // operation name is base uri
    configuration.setBaseUri("getSObject");

    SalesforceEndpoint endpoint =
        assertIsInstanceOf(SalesforceEndpoint.class, configuration.createEndpoint());
    final SalesforceEndpointConfig endpointConfig = endpoint.getConfiguration();
    assertEquals("endpoint.format", PayloadFormat.XML, endpointConfig.getFormat());
    assertEquals("endpoint.sObjectName", "Merchandise__c", endpointConfig.getSObjectName());
    assertEquals(
        "endpoint.sObjectFields",
        "Description__c,Total_Inventory__c",
        endpointConfig.getSObjectFields());
    assertEquals("endpoint.updateTopic", false, endpointConfig.isUpdateTopic());
  }
  @Test
  public void testEndpointCompletion() throws Exception {
    Component component = context().getComponent(componentName);
    ComponentConfiguration configuration = component.createComponentConfiguration();

    // get operation names
    assertCompletionOptions(
        configuration.completeEndpointPath(""),
        "getVersions",
        "getResources",
        "getGlobalObjects",
        "getBasicInfo",
        "getDescription",
        "getSObject",
        "createSObject",
        "updateSObject",
        "deleteSObject",
        "getSObjectWithId",
        "upsertSObject",
        "deleteSObjectWithId",
        "getBlobField",
        "query",
        "queryMore",
        "queryAll",
        "search",
        "apexCall",
        "recent",
        "createJob",
        "getJob",
        "closeJob",
        "abortJob",
        "createBatch",
        "getBatch",
        "getAllBatches",
        "getRequest",
        "getResults",
        "createBatchQuery",
        "getQueryResultIds",
        "getQueryResult",
        "getRecentReports",
        "getReportDescription",
        "executeSyncReport",
        "executeAsyncReport",
        "getReportInstances",
        "getReportResults",
        "limits",
        "approval",
        "approvals",
        "[PushTopicName]");

    // get filtered operation names
    assertCompletionOptions(
        configuration.completeEndpointPath("get"),
        "getVersions",
        "getResources",
        "getGlobalObjects",
        "getBasicInfo",
        "getDescription",
        "getSObject",
        "getSObjectWithId",
        "getBlobField",
        "getJob",
        "getBatch",
        "getAllBatches",
        "getRequest",
        "getResults",
        "getQueryResultIds",
        "getQueryResult",
        "getRecentReports",
        "getReportDescription",
        "getReportInstances",
        "getReportResults");

    /* TODO support parameter completion
            // get ALL REST operation parameters
            // TODO support operation specific parameter completion
            assertCompletionOptions(configuration.completeEndpointPath("getSObject?"),
                "apiVersion", "httpClient", "format", "sObjectName", "sObjectId", "sObjectFields",
                "sObjectIdName", "sObjectIdValue", "sObjectBlobFieldName", "sObjectClass", "sObjectQuery", "sObjectSearch");

            // get filtered REST parameters
            assertCompletionOptions(configuration.completeEndpointPath("getSObject?format=XML&"),
                "apiVersion", "httpClient", "sObjectName", "sObjectId", "sObjectFields",
                "sObjectIdName", "sObjectIdValue", "sObjectBlobFieldName", "sObjectClass", "sObjectQuery", "sObjectSearch");

            // get ALL Bulk operation parameters
            // TODO support operation specific parameter completion
            assertCompletionOptions(configuration.completeEndpointPath("createJob?"),
                "apiVersion", "httpClient", "sObjectQuery", "contentType", "jobId", "batchId", "resultId");

            // get filtered Bulk operation parameters
            assertCompletionOptions(configuration.completeEndpointPath("createJob?contentType=XML&"),
                "apiVersion", "httpClient", "sObjectQuery", "jobId", "batchId", "resultId");

            // get ALL topic parameters for consumers
            assertCompletionOptions(configuration.completeEndpointPath("myTopic?"),
                "apiVersion", "httpClient", "updateTopic", "notifyForFields", "notifyForOperations");

            // get filtered topic parameters for consumers
            assertCompletionOptions(configuration.completeEndpointPath("myTopic?updateTopic=true&"),
                "apiVersion", "httpClient", "notifyForFields", "notifyForOperations");

            // get parameters from partial name
            assertCompletionOptions(configuration.completeEndpointPath("getSObject?sObject"),
                "sObjectName", "sObjectId", "sObjectFields",
                "sObjectIdName", "sObjectIdValue", "sObjectBlobFieldName", "sObjectClass", "sObjectQuery", "sObjectSearch");
    */

    // get sObjectName values, from scanned DTO packages
    assertCompletionOptions(
        configuration.completeEndpointPath("getSObject?sObjectName="),
        "Account",
        "Tasks__c",
        "Line_Item__c",
        "Merchandise__c",
        "Document",
        "MSPTest");

    // get sObjectFields values, from scanned DTO
    assertCompletionOptions(
        configuration.completeEndpointPath("getSObject?sObjectName=Merchandise__c&sObjectFields="),
        "Description__c",
        "Price__c",
        "Total_Inventory__c",
        "attributes",
        "Id",
        "OwnerId",
        "IsDeleted",
        "Name",
        "CreatedDate",
        "CreatedById",
        "LastModifiedDate",
        "LastModifiedById",
        "SystemModstamp",
        "LastActivityDate",
        "LastViewedDate",
        "LastReferencedDate");

    // get sObjectClass values, from scanned DTO packages
    assertCompletionOptions(
        configuration.completeEndpointPath("getSObject?sObjectClass="),
        Account.class.getName(),
        Tasks__c.class.getName(),
        Line_Item__c.class.getName(),
        Merchandise__c.class.getName(),
        Document.class.getName(),
        MSPTest.class.getName(),
        QueryRecordsLine_Item__c.class.getName());
  }
  @Override
  public List<String> completeEndpointPath(
      ComponentConfiguration configuration, String completionText) {
    final List<String> result = new ArrayList<String>();

    final Set<String> apiNames = collection.getApiNames();
    boolean useDefaultName = apiNames.size() == 1 && apiNames.contains("");

    // check if there is an API name present
    completionText = ObjectHelper.isEmpty(completionText) ? "" : completionText;
    final int prefixEnd = completionText.indexOf('/');
    final int pathEnd = completionText.lastIndexOf('?');

    // empty or incomplete API prefix, and no options, add API names or method names if
    // useDefaultName
    final Map<E, ? extends ApiMethodHelper<? extends ApiMethod>> apiHelpers =
        collection.getApiHelpers();
    if (prefixEnd == -1 && pathEnd == -1) {

      if (useDefaultName) {

        // complete method names for default API
        final Set<Class<? extends ApiMethod>> apiMethods = collection.getApiMethods().keySet();
        final Class<? extends ApiMethod> apiMethod = apiMethods.iterator().next();
        final ApiMethodHelper<? extends ApiMethod> helper = apiHelpers.values().iterator().next();
        getCompletedMethods(result, completionText, apiMethod, helper);
      } else {

        // complete API names
        for (String name : apiNames) {
          if (!name.isEmpty() || name.startsWith(completionText)) {
            result.add(name);
          }
        }
      }

      // path with complete API name prefix, but no options
    } else if (prefixEnd != -1 && pathEnd == -1) {

      // complete method names for specified API
      final E apiName = getApiNameOrNull(completionText.substring(0, prefixEnd));
      if (apiName != null) {
        final ApiMethodHelper<? extends ApiMethod> helper = apiHelpers.get(apiName);
        completionText = completionText.substring(prefixEnd + 1);
        for (Map.Entry<Class<? extends ApiMethod>, E> entry :
            collection.getApiMethods().entrySet()) {
          if (entry.getValue().equals(apiName)) {
            getCompletedMethods(result, completionText, entry.getKey(), helper);
            break;
          }
        }
      }

      // complete options
    } else {

      // get last option text
      final int lastParam = completionText.lastIndexOf('&');
      String optionText;
      if (lastParam != -1) {
        optionText = completionText.substring(lastParam + 1);
      } else {
        optionText = completionText.substring(pathEnd);
      }

      String methodName = null;
      ApiMethodHelper<? extends ApiMethod> helper = null;
      if (useDefaultName) {

        // get default endpoint configuration and helper
        methodName = completionText.substring(0, pathEnd);
        helper = apiHelpers.values().iterator().next();
      } else {

        // get API name and method name, if they exist
        final String[] pathElements = completionText.substring(0, pathEnd).split("/");
        if (pathElements.length == 2) {
          final E apiName = getApiNameOrNull(pathElements[0]);
          methodName = pathElements[1];
          helper = collection.getHelper(apiName);
        }
      }
      if (helper != null && !ObjectHelper.isEmpty(methodName)) {
        // get other options from configuration
        Set<String> existingOptions = configuration.getParameters().keySet();
        // get all method options
        try {
          final List<Object> arguments = helper.getArguments(methodName);
          final int nArgs = arguments.size();
          final Set<String> options = new HashSet<String>();
          for (int i = 1; i < nArgs; i += 2) {
            options.add((String) arguments.get(i));
          }
          options.removeAll(existingOptions);

          // return matching options
          for (String option : options) {
            if (option.startsWith(optionText)) {
              result.add(option);
            }
          }
        } catch (IllegalArgumentException ignore) {
          // thrown from getArguments() when no matching methods,
          // return an empty result
        }
      }
    }

    return result;
  }