StaticELSpecificationProvider implements ELSpecificationProvider { @Override public Optional<ELSpecification> getELSpecification(final String agentClass) { if (StringUtils.isBlank(agentClass)) { throw new IllegalArgumentException("AgentClass is required to obtain the EL specification"); } final List<AgentManifest> agentManifests = agentService.getAgentManifests(agentClass); if (agentManifests.isEmpty()) { throw new IllegalArgumentException("No manifests exists for the given agent class"); } final AgentManifest agentManifest = agentManifests.get(0); if (agentManifest.getAgentType() == null) { throw new IllegalArgumentException("AgentManifest AgentType is required to obtain the EL specification"); } String mappedType; final String agentType = agentManifest.getAgentType(); if (agentType.equals(MINIFI_JAVA_TYPE) || agentType.equals("java")) { mappedType = MINIFI_JAVA_TYPE; } else if (agentType.equals(MINIFI_CPP_TYPE) || agentType.equals("cpp")) { mappedType = MINIFI_CPP_TYPE; } else { throw new IllegalArgumentException("Unexpected agent type '" + agentType + "'"); } String version = null; final BuildInfo buildInfo = agentManifest.getBuildInfo(); if (buildInfo != null && buildInfo.getVersion() != null) { final String buildInfoVersion = buildInfo.getVersion(); final int snapshotIndex = buildInfoVersion.indexOf("-SNAPSHOT"); if (snapshotIndex > 0) { version = buildInfoVersion.substring(0, snapshotIndex); } else { version = buildInfoVersion; } } if (mappedType.equals(MINIFI_CPP_TYPE)) { boolean hasELBundle = false; if (agentManifest.getBundles() != null) { for (final Bundle bundle : agentManifest.getBundles()) { if (MINIFI_CPP_EL_BUNDLE_ARTIFACT.equals(bundle.getArtifact())) { hasELBundle = true; break; } } } if (!hasELBundle) { final ELSpecification emptySpec = new ELSpecification(); emptySpec.setSpecificationKey("missing-el-bundle"); emptySpec.setOperations(Collections.emptyMap()); return Optional.of(emptySpec); } } if (version == null) { return getCompatibleELSpecification(mappedType); } else { final String key = getKey(mappedType, version); final ELSpecification exactSpecification = specifications.get(key); if (exactSpecification == null) { LOGGER.warn("No EL specification was found for {}, attempting to find compatible specification", new Object[]{key}); return getCompatibleELSpecification(mappedType); } else { return Optional.of(exactSpecification); } } } @Autowired  StaticELSpecificationProvider(final ELSpecificationProperties properties, final AgentService agentService, final ObjectMapper objectMapper); @PostConstruct void loadSpecifications(); @Override Optional<ELSpecification> getELSpecification(final String agentClass);  }
@Test public void testGetCppELSpecificationExactVersionExists() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("0.5.0", "cpp"); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertTrue(specification.isPresent()); assertEquals("minifi-cpp-0.5.0", specification.get().getSpecificationKey()); final Map<String, ELOperation> operations = specification.get().getOperations(); assertNotNull(operations); assertEquals(1, operations.size()); final ELOperation operation = operations.get("isNull"); assertNotNull(operation); assertEquals("isNull", operation.getName()); }
@Test public void testGetCppELSpecificationExactVersionWhenNoELBundle() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("0.5.0", "cpp"); defaultManifest.setBundles(Collections.emptyList()); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertTrue(specification.isPresent()); assertEquals("missing-el-bundle", specification.get().getSpecificationKey()); final Map<String, ELOperation> operations = specification.get().getOperations(); assertNotNull(operations); assertEquals(0, operations.size()); }
@Test public void testGetJavaELSpecificationExactVersionExists() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("0.6.0", "minifi-java"); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertTrue(specification.isPresent()); assertEquals("minifi-java-0.6.0", specification.get().getSpecificationKey()); }
@Test public void testGetCppELSpecificationWithAgentTypeVariation() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("0.6.0", "minifi-cpp"); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertTrue(specification.isPresent()); assertEquals("minifi-cpp-0.6.0", specification.get().getSpecificationKey()); }
@Test public void testGetJavaELSpecificationWithAgentTypeVariation() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("0.6.0", "java"); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertTrue(specification.isPresent()); assertEquals("minifi-java-0.6.0", specification.get().getSpecificationKey()); }
@Test public void testGetJavaELSpecificationWithSnapshotVersion() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("0.6.0-SNAPSHOT", "minifi-java"); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertTrue(specification.isPresent()); assertEquals("minifi-java-0.6.0", specification.get().getSpecificationKey()); }
@Test public void testGetELSpecWhenVersionDoesNotExistAndMultipleCompatible() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("X.Y.Z", "cpp"); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertFalse(specification.isPresent()); }
@Test public void testGetELSpecWhenVersionDoesNotExistAndSingleCompatible() { final String agentClass = "default"; final AgentManifest defaultManifest = createAgentManifest("X.Y.Z", "minifi-java"); when(agentService.getAgentManifests(agentClass)).thenReturn(Collections.singletonList(defaultManifest)); final Optional<ELSpecification> specification = specificationProvider.getELSpecification(agentClass); assertTrue(specification.isPresent()); assertEquals("minifi-java-0.6.0", specification.get().getSpecificationKey()); }