How to access/set property via camel context programatically

3.1k views Asked by At

I am using properties stored/defined in my blueprint.xml:

<cm:property-placeholder id="props.placeholder" persistent-id="props.blueprint">
    <cm:default-properties>
        ...
        <cm:property name="time.daysHistory" value="4" />
    </cm:default-properties>
</cm:property-placeholder>

I am using this properties via injection (@PropertyInject) or with this syntax{{time.daysHistory}}.

Is there a way to read and or set those properies from within my Bluprinttests? I tried context.getProperties() but this returns an empty map.

2

There are 2 answers

6
Souciance Eqdam Rashti On

Like Claus mentioned you need to use the useOverridePropertiesWithConfigAdmin method. However note, you need to return the same pid value as the one configured in your blueprint.

Your blueprint:

<cm:property-placeholder id="props.placeholder" persistent-id="props.blueprint">
    <cm:default-properties>
        ...
        <cm:property name="time.daysHistory" value="4" />
    </cm:default-properties>
</cm:property-placeholder>

In your test add:

@Override
protected String useOverridePropertiesWithConfigAdmin(Dictionary props) {
props.put("time.daysHistory", "1");
return "props.blueprint";
}

EDIT: Here is how I have done it:

In my route I have injected properties:

  @PropertyInject("DatasetConsumePath")
  private String datasetConsumePath;
  @PropertyInject("DatasetFileExtension")
  private String datasetFileExtension;

My blueprint:

  <cm:property-placeholder id="test" persistent-id="test" update-strategy="reload">
    <cm:default-properties>
      <cm:property name="DatasetConsumePath" value="test"/>
      <cm:property name="DatasetFileExtension" value="txt"/>
      <cm:property name="DatasetAggregateBatchSize" value="1000"/>
    </cm:default-properties>
  </cm:property-placeholder>

My test:

  @Override
  protected String useOverridePropertiesWithConfigAdmin(Dictionary props) {
    // add the properties we want to override
    props.put("DatasetConsumePath", "src/test/resources/test files/test/");

    // return the PID of the config-admin we are using in the blueprint xml file
    return "test";
  }
1
Claus Ibsen On

See the documentation: http://camel.apache.org/using-propertyplaceholder.html about the loadConfigAdminConfigurationFile and useOverridePropertiesWithConfigAdmin methods you can use from unit tests.