|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.nifi.manifest; |
| 18 | + |
| 19 | +import org.apache.nifi.bundle.Bundle; |
| 20 | +import org.apache.nifi.bundle.BundleCoordinate; |
| 21 | +import org.apache.nifi.bundle.BundleDetails; |
| 22 | +import org.apache.nifi.c2.protocol.component.api.ComponentManifest; |
| 23 | +import org.apache.nifi.c2.protocol.component.api.ControllerServiceDefinition; |
| 24 | +import org.apache.nifi.c2.protocol.component.api.ProcessorDefinition; |
| 25 | +import org.apache.nifi.c2.protocol.component.api.RuntimeManifest; |
| 26 | +import org.apache.nifi.extension.manifest.parser.ExtensionManifestParser; |
| 27 | +import org.apache.nifi.extension.manifest.parser.jaxb.JAXBExtensionManifestParser; |
| 28 | +import org.apache.nifi.nar.ExtensionManager; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.HashSet; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | +import static org.mockito.Mockito.when; |
| 41 | + |
| 42 | +public class StandardRuntimeManifestServiceTest { |
| 43 | + |
| 44 | + private Bundle frameworkBundle; |
| 45 | + private Bundle testComponentsBundle; |
| 46 | + private ExtensionManager extensionManager; |
| 47 | + private ExtensionManifestParser extensionManifestParser; |
| 48 | + private RuntimeManifestService runtimeManifestService; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + public void setup() { |
| 52 | + final BundleDetails frameworkBundleDetails = new BundleDetails.Builder() |
| 53 | + .coordinate(new BundleCoordinate("org.apache.nifi", "nifi-framework-nar", "1.16.0")) |
| 54 | + .buildJdk("1.8.0") |
| 55 | + .buildRevision("revision1") |
| 56 | + .buildTimestamp("2022-03-07T08:54:46Z") |
| 57 | + .workingDir(new File("src/test/resources/TestRuntimeManifest/nifi-framework-nar")) |
| 58 | + .build(); |
| 59 | + |
| 60 | + frameworkBundle = new Bundle(frameworkBundleDetails, this.getClass().getClassLoader()); |
| 61 | + |
| 62 | + final BundleDetails testComponentsBundleDetails = new BundleDetails.Builder() |
| 63 | + .coordinate(new BundleCoordinate("org.apache.nifi", "nifi-test-components-nar", "1.16.0")) |
| 64 | + .buildJdk("1.8.0") |
| 65 | + .buildRevision("revision1") |
| 66 | + .buildTimestamp("2022-03-07T08:54:46Z") |
| 67 | + .workingDir(new File("src/test/resources/TestRuntimeManifest/nifi-test-components-nar")) |
| 68 | + .build(); |
| 69 | + |
| 70 | + testComponentsBundle = new Bundle(testComponentsBundleDetails, this.getClass().getClassLoader()); |
| 71 | + |
| 72 | + extensionManager = mock(ExtensionManager.class); |
| 73 | + extensionManifestParser = new JAXBExtensionManifestParser(); |
| 74 | + runtimeManifestService = new TestableRuntimeManifestService(extensionManager, extensionManifestParser, frameworkBundle); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testGetRuntimeManifest() { |
| 79 | + when(extensionManager.getAllBundles()).thenReturn(new HashSet<>(Arrays.asList(testComponentsBundle))); |
| 80 | + |
| 81 | + final RuntimeManifest runtimeManifest = runtimeManifestService.getManifest(); |
| 82 | + assertNotNull(runtimeManifest); |
| 83 | + assertEquals(frameworkBundle.getBundleDetails().getCoordinate().getVersion(), runtimeManifest.getVersion()); |
| 84 | + |
| 85 | + assertEquals(frameworkBundle.getBundleDetails().getCoordinate().getVersion(), runtimeManifest.getBuildInfo().getVersion()); |
| 86 | + assertEquals(frameworkBundle.getBundleDetails().getBuildRevision(), runtimeManifest.getBuildInfo().getRevision()); |
| 87 | + assertEquals(frameworkBundle.getBundleDetails().getBuildJdk(), runtimeManifest.getBuildInfo().getCompiler()); |
| 88 | + |
| 89 | + final List<org.apache.nifi.c2.protocol.component.api.Bundle> bundles = runtimeManifest.getBundles(); |
| 90 | + assertNotNull(bundles); |
| 91 | + assertEquals(1, bundles.size()); |
| 92 | + |
| 93 | + final org.apache.nifi.c2.protocol.component.api.Bundle testComponentsManifestBundle = bundles.get(0); |
| 94 | + assertEquals(testComponentsBundle.getBundleDetails().getCoordinate().getGroup(), testComponentsManifestBundle.getGroup()); |
| 95 | + assertEquals(testComponentsBundle.getBundleDetails().getCoordinate().getId(), testComponentsManifestBundle.getArtifact()); |
| 96 | + assertEquals(testComponentsBundle.getBundleDetails().getCoordinate().getVersion(), testComponentsManifestBundle.getVersion()); |
| 97 | + |
| 98 | + final ComponentManifest testComponentsManifest = testComponentsManifestBundle.getComponentManifest(); |
| 99 | + assertNotNull(testComponentsManifest); |
| 100 | + |
| 101 | + final List<ProcessorDefinition> processorDefinitions = testComponentsManifest.getProcessors(); |
| 102 | + assertEquals(3, processorDefinitions.size()); |
| 103 | + |
| 104 | + final List<ControllerServiceDefinition> controllerServiceDefinitions = testComponentsManifest.getControllerServices(); |
| 105 | + assertEquals(1, controllerServiceDefinitions.size()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Override getFrameworkBundle to provide a mocked Bundle. |
| 110 | + */ |
| 111 | + private static class TestableRuntimeManifestService extends StandardRuntimeManifestService { |
| 112 | + |
| 113 | + private Bundle frameworkBundle; |
| 114 | + |
| 115 | + public TestableRuntimeManifestService(final ExtensionManager extensionManager, final ExtensionManifestParser extensionManifestParser, final Bundle frameworkBundle) { |
| 116 | + super(extensionManager, extensionManifestParser); |
| 117 | + this.frameworkBundle = frameworkBundle; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + Bundle getFrameworkBundle() { |
| 122 | + return frameworkBundle; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments