CreateRrdOperations { public List<Operation> create(LookupContext context, boolean recursive, boolean optional) { LookupResult lookupResult = context.lookupResult; List<Operation> operations = new ArrayList<>(); lookupResult.templates().stream() .filter(template -> optional == template.isOptional()) .forEach(template -> { int missingMetadata = lookupResult.missingMetadata(template); if (missingMetadata != ALL_PRESENT) { ResourceAddress address; Operation.Builder builder = null; if (missingMetadata == NOTHING_PRESENT) { address = template.resolve(securityContextStatementContext); builder = new Operation.Builder(address, READ_RESOURCE_DESCRIPTION_OPERATION) .param(ACCESS_CONTROL, COMBINED_DESCRIPTIONS) .param(OPERATIONS, true); } else if (missingMetadata == RESOURCE_DESCRIPTION_PRESENT) { address = template.resolve(securityContextStatementContext); builder = new Operation.Builder(address, READ_RESOURCE_DESCRIPTION_OPERATION) .param(ACCESS_CONTROL, TRIM_DESCRIPTIONS) .param(OPERATIONS, true); } else if (missingMetadata == SECURITY_CONTEXT_PRESENT) { address = template.resolve(resourceDescriptionStatementContext); builder = new Operation.Builder(address, READ_RESOURCE_DESCRIPTION_OPERATION) .param(OPERATIONS, true); } if (builder != null) { if (recursive) { builder.param(RECURSIVE_DEPTH, depth); } builder.param(LOCALE, locale); operations.add(builder.build()); } } }); return operations; } CreateRrdOperations(Environment environment, StatementContext statementContext, String locale, int depth); List<Operation> create(LookupContext context, boolean recursive, boolean optional);  }
@Test public void noTemplates() { LookupResult lookupResult = new LookupResult(Collections.emptySet()); List<Operation> operations = rrdOps.create(new LookupContext(lookupResult), false, false); assertTrue(operations.isEmpty()); }
@Test public void mixed() { AddressTemplate nothingPresent = AddressTemplate.of("nothing=present"); AddressTemplate descriptionPresent = AddressTemplate.of("description=present"); AddressTemplate securityContextPresent = AddressTemplate.of("securityContext=present"); AddressTemplate allPresent = AddressTemplate.of("all=present"); LookupResult lookupResult = new LookupResult( Sets.newHashSet(nothingPresent, descriptionPresent, securityContextPresent, allPresent)); lookupResult.markMetadataPresent(descriptionPresent, RESOURCE_DESCRIPTION_PRESENT); lookupResult.markMetadataPresent(securityContextPresent, SECURITY_CONTEXT_PRESENT); lookupResult.markMetadataPresent(allPresent, RESOURCE_DESCRIPTION_PRESENT); lookupResult.markMetadataPresent(allPresent, SECURITY_CONTEXT_PRESENT); List<Operation> inputs = rrdOps.create(new LookupContext(lookupResult), false, false); assertEquals(3, inputs.size()); Operation operation = findOperation(inputs, nothingPresent); assertEquals(COMBINED_DESCRIPTIONS, operation.get(ACCESS_CONTROL).asString()); assertTrue(operation.get(OPERATIONS).asBoolean()); operation = findOperation(inputs, descriptionPresent); assertEquals("trim-descriptions", operation.get(ACCESS_CONTROL).asString()); assertTrue(operation.get(OPERATIONS).asBoolean()); operation = findOperation(inputs, securityContextPresent); assertFalse(operation.get(ACCESS_CONTROL).isDefined()); assertTrue(operation.get(OPERATIONS).asBoolean()); }
@Test public void recursive() { LookupResult lookupResult = new LookupResult(Sets.<AddressTemplate>newHashSet(AddressTemplate.of("foo=bar"))); List<Operation> operations = rrdOps.create(new LookupContext(lookupResult), true, false); Operation operation = operations.get(0); assertEquals(RRD_DEPTH, operation.get(RECURSIVE_DEPTH).asInt()); }