BundleFileBuilder { public void buildBundle(File rootDir, File outputDir, List<DependentBundle> dependencies, ProjectInfo projectInfo) { final DocumentBuilder documentBuilder = documentTools.getDocumentBuilder(); final Document document = documentBuilder.newDocument(); final Bundle bundle = new Bundle(projectInfo); if (rootDir != null) { final Collection<EntityLoader> entityLoaders = entityLoaderRegistry.getEntityLoaders(); entityLoaders.parallelStream().forEach(e -> e.load(bundle, rootDir)); FolderLoaderUtils.createFolders(bundle, rootDir, bundle.getServices()); final Set<Bundle> dependencyBundles = new HashSet<>(); for (DependentBundle dependentBundle: dependencies) { List<File> metadataFiles = new ArrayList<>(); final File dependencyFile = dependentBundle.getDependencyFile(); if (!dependencyFile.exists()) { File bundleDirectory = dependencyFile.getParentFile(); if (bundleDirectory != null && bundleDirectory.isDirectory()) { File[] files = bundleDirectory.listFiles(); metadataFiles = Stream.of(files).filter(file -> file.getName().endsWith(JsonFileUtils.METADATA_FILE_NAME_SUFFIX)).collect(Collectors.toList()); } } if (!metadataFiles.isEmpty()) { metadataFiles.forEach(file -> { Bundle bundleDependency = cache.getBundleFromMetadataFile(file); if (bundleDependency != null) { dependencyBundles.add(bundleDependency); } }); } else if (dependencyFile.getName().endsWith(JsonFileUtils.METADATA_FILE_NAME_SUFFIX)) { Bundle bundleDependency = cache.getBundleFromMetadataFile(dependencyFile); if (bundleDependency != null) { bundleDependency.setDependentBundleFrom(dependentBundle); dependencyBundles.add(bundleDependency); } } else if (dependencyFile.getName().endsWith(BUNDLE_EXTENSION)) { Bundle bundleDependency = cache.getBundleFromFile(dependencyFile); bundleDependency.setDependentBundleFrom(dependentBundle); dependencyBundles.add(bundleDependency); } } bundle.setDependencies(dependencyBundles); if (!dependencyBundles.isEmpty()) { logOverriddenEntities(bundle, dependencyBundles, Service.class); logOverriddenEntities(bundle, dependencyBundles, Policy.class); } } final Map<String, BundleArtifacts> bundleElementMap = bundleEntityBuilder.build(bundle, EntityBuilder.BundleType.DEPLOYMENT, document, projectInfo); bundleElementMap.forEach((k, v) -> writeBundleArtifacts(k, v, outputDir)); } @Inject  BundleFileBuilder(final DocumentTools documentTools,
                             final DocumentFileUtils documentFileUtils,
                             final JsonFileUtils jsonFileUtils,
                             final EntityLoaderRegistry entityLoaderRegistry,
                             final BundleEntityBuilder bundleEntityBuilder,
                             final BundleCache cache); void buildBundle(File rootDir, File outputDir, List<DependentBundle> dependencies, ProjectInfo projectInfo);  }
@Test void buildBundleNoSource() { BundleFileBuilder bundleFileBuilder = new BundleFileBuilder(documentTools, documentFileUtils, jsonFileUtils, entityLoaderRegistry, bundleEntityBuilder, bundleCache); bundleFileBuilder.buildBundle(null, new File("output"), Collections.emptyList(), projectInfo); verify(bundleEntityBuilder).build(argThat(bundle -> bundle.getPolicies().isEmpty()), eq(EntityBuilder.BundleType.DEPLOYMENT), any(), eq(projectInfo)); }
@Test void buildBundleOnePolicy() { Policy policy = new Policy(); policy.setName("from-file"); when(entityLoaderRegistry.getEntityLoaders()).thenReturn(Collections.singleton(new TestPolicyLoader(policy))); BundleFileBuilder bundleFileBuilder = new BundleFileBuilder(documentTools, documentFileUtils, jsonFileUtils, entityLoaderRegistry, bundleEntityBuilder, bundleCache); bundleFileBuilder.buildBundle(new File("input"), new File("output"),Collections.emptyList(), projectInfo); verify(bundleEntityBuilder).build(argThat(bundle -> bundle.getPolicies().containsKey(policy.getName()) && bundle.getPolicies().containsValue(policy)), eq(EntityBuilder.BundleType.DEPLOYMENT), any(), eq(projectInfo)); }
@Test void buildBundleWithDependency() { Policy policy = new Policy(); policy.setName("from-file"); when(entityLoaderRegistry.getEntityLoaders()).thenReturn(Collections.singleton(new TestPolicyLoader(policy))); List<DependentBundle> dummyList = new ArrayList<>(); dummyList.add(new DependentBundle(new File("test.bundle"))); Bundle dependencyBundle = new Bundle(); when(bundleCache.getBundleFromFile(any(File.class))).thenReturn(dependencyBundle); BundleFileBuilder bundleFileBuilder = Mockito.spy(new BundleFileBuilder(documentTools, documentFileUtils, jsonFileUtils, entityLoaderRegistry, bundleEntityBuilder, bundleCache)); bundleFileBuilder.buildBundle(new File("input"), new File("output"), dummyList, projectInfo); Assert.assertNotNull(dependencyBundle.getDependentBundleFrom()); }