Java Code Examples for com.hubspot.jinjava.interpret.Context#put()

The following examples show how to use com.hubspot.jinjava.interpret.Context#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KerberosUserMapper.java    From gcp-token-broker with Apache License 2.0 6 votes vote down vote up
@Override
public String map(String name) {
    Context context = new Context();
    KerberosName principal = new KerberosName(name);
    context.put("principal", principal.getFullName());
    context.put("primary", principal.getPrimary());
    context.put("instance", principal.getInstance());
    context.put("realm", principal.getRealm());
    // Look through the list of rules
    for (Rule rule : rulesList) {
        boolean isApplicable = rule.evaluateIfCondition(context);
        if (isApplicable) {
            // An applicable rule was found. Apply it to get the user mapping.
            return rule.evaluateThenExpression(context);
        }
    }
    throw new IllegalArgumentException("Principal `" + name + "` cannot be mapped to a Google identity.");
}
 
Example 2
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itFindsGlobalProperties() {
  Context context = new Context();
  context.put("java_bean", getPopulatedJavaBean());

  context =
    getContext(
      Lists.newArrayList(getNodeForClass(TagNode.class, "{% if java_bean %}")),
      Optional.of(context)
    );

  Set<String> deferredProperties = DeferredValueUtils.findAndMarkDeferredProperties(
    context
  );

  assertThat(deferredProperties).contains("java_bean");
}
 
Example 3
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itDefersTheCompleteObjectWhenAtLeastOnePropertyIsUsed() {
  Context context = new Context();
  context.put("java_bean", getPopulatedJavaBean());

  context =
    getContext(
      Lists.newArrayList(
        getNodeForClass(
          TagNode.class,
          "{% if java_bean.property_one %}",
          Optional.empty(),
          Optional.empty()
        )
      ),
      Optional.of(context)
    );

  DeferredValueUtils.findAndMarkDeferredProperties(context);
  assertThat(context.containsKey("java_bean")).isTrue();
  assertThat(context.get("java_bean")).isInstanceOf(DeferredValue.class);
  DeferredValue deferredValue = (DeferredValue) context.get("java_bean");
  JavaBean originalValue = (JavaBean) deferredValue.getOriginalValue();
  assertThat(originalValue).hasFieldOrPropertyWithValue("propertyOne", "propertyOne");
  assertThat(originalValue).hasFieldOrPropertyWithValue("propertyTwo", "propertyTwo");
}
 
Example 4
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itHandlesCaseWhereValueIsNull() {
  Context context = getContext(
    Lists.newArrayList(
      getNodeForClass(
        TagNode.class,
        "{% if property.id %}",
        Optional.empty(),
        Optional.empty()
      )
    )
  );
  context.put("property", null);
  DeferredValueUtils.findAndMarkDeferredProperties(context);

  assertThat(context.get("property")).isNull();
}
 
Example 5
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itPreservesNonDeferredProperties() {
  Context context = getContext(
    Lists.newArrayList(
      getNodeForClass(
        TagNode.class,
        "{% if deferred %}",
        Optional.empty(),
        Optional.empty()
      )
    )
  );
  context.put("deferred", "deferred");
  context.put("not_deferred", "test_value");

  DeferredValueUtils.findAndMarkDeferredProperties(context);
  assertThat(context.get("not_deferred")).isEqualTo("test_value");
}
 
Example 6
Source File: ExpressionNodeTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itRendersResultWithoutNestedExpressionInterpretation() throws Exception {
  final JinjavaConfig config = JinjavaConfig
    .newBuilder()
    .withNestedInterpretationEnabled(false)
    .build();
  JinjavaInterpreter noNestedInterpreter = new Jinjava(config).newInterpreter();
  Context contextNoNestedInterpretation = noNestedInterpreter.getContext();
  contextNoNestedInterpretation.put("myvar", "hello {{ place }}");
  contextNoNestedInterpretation.put("place", "world");

  ExpressionNode node = fixture("simplevar");
  assertThat(node.render(noNestedInterpreter).toString())
    .isEqualTo("hello {{ place }}");
}
 
Example 7
Source File: ExpressionNodeTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itRendersWithNestedExpressionInterpretationByDefault() throws Exception {
  final JinjavaConfig config = JinjavaConfig.newBuilder().build();
  JinjavaInterpreter noNestedInterpreter = new Jinjava(config).newInterpreter();
  Context contextNoNestedInterpretation = noNestedInterpreter.getContext();
  contextNoNestedInterpretation.put("myvar", "hello {{ place }}");
  contextNoNestedInterpretation.put("place", "world");

  ExpressionNode node = fixture("simplevar");
  assertThat(node.render(noNestedInterpreter).toString()).isEqualTo("hello world");
}
 
Example 8
Source File: ExpressionNodeTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itRendersNestedTags() throws Exception {
  final JinjavaConfig config = JinjavaConfig.newBuilder().build();
  JinjavaInterpreter jinjava = new Jinjava(config).newInterpreter();
  Context context = jinjava.getContext();
  context.put("myvar", "hello {% if (true) %}nasty{% endif %}");

  ExpressionNode node = fixture("simplevar");
  assertThat(node.render(jinjava).toString()).isEqualTo("hello nasty");
}
 
Example 9
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itDefersWholePropertyOnArrayAccess() {
  Context context = getContext(
    Lists.newArrayList(getNodeForClass(TagNode.class, "{{ array[0] }}"))
  );
  context.put("array", Lists.newArrayList("a", "b", "c"));

  Set<String> deferredProperties = DeferredValueUtils.findAndMarkDeferredProperties(
    context
  );
  assertThat(deferredProperties).contains("array");
}
 
Example 10
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itDefersWholePropertyOnDictAccess() {
  Context context = getContext(
    Lists.newArrayList(getNodeForClass(TagNode.class, "{{ dict['a'] }}"))
  );
  context.put("dict", Collections.singletonMap("a", "x"));

  Set<String> deferredProperties = DeferredValueUtils.findAndMarkDeferredProperties(
    context
  );
  assertThat(deferredProperties).contains("dict");
}
 
Example 11
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itRestoresContextSuccessfully() {
  Context context = new Context();
  ImmutableMap<String, String> simpleMap = ImmutableMap.of("a", "x", "b", "y");
  ImmutableMap<String, Object> nestedMap = ImmutableMap.of("nested", simpleMap);
  Integer[] simpleArray = { 1, 2, 3, 4, 5, 6 };
  JavaBean javaBean = getPopulatedJavaBean();
  context.put("simple_var", DeferredValue.instance("SimpleVar"));
  context.put("java_bean", DeferredValue.instance(javaBean));
  context.put("simple_bool", DeferredValue.instance(true));
  context.put("simple_array", DeferredValue.instance(simpleArray));
  context.put("simple_map", DeferredValue.instance(simpleMap));
  context.put("nested_map", DeferredValue.instance(nestedMap));

  context.put("simple_var_undeferred", "SimpleVarUnDeferred");
  context.put("java_bean_undeferred", javaBean);
  context.put("nested_map_undeferred", nestedMap);

  HashMap<String, Object> result = DeferredValueUtils.getDeferredContextWithOriginalValues(
    context
  );
  assertThat(result).contains(entry("simple_var", "SimpleVar"));
  assertThat(result).contains(entry("java_bean", javaBean));
  assertThat(result).contains(entry("simple_bool", true));
  assertThat(result).contains(entry("simple_array", simpleArray));
  assertThat(result).contains(entry("simple_map", simpleMap));
  assertThat(result).contains(entry("nested_map", nestedMap));

  assertThat(result)
    .doesNotContain(entry("simple_var_undeferred", "SimpleVarUnDeferred"));
  assertThat(result).doesNotContain(entry("java_bean_undeferred", javaBean));
  assertThat(result).doesNotContain(entry("nested_map_undeferred", nestedMap));
}
 
Example 12
Source File: DeferredValueUtilsTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itIgnoresUnrestorableValuesFromDeferredContext() {
  Context context = new Context();
  context.put("simple_var", DeferredValue.instance());
  context.put("java_bean", DeferredValue.instance());

  HashMap<String, Object> result = DeferredValueUtils.getDeferredContextWithOriginalValues(
    context
  );
  assertThat(result).isEmpty();
}