DependencyBundlesProcessor { public LinkedList<File> process(final List<File> bundles, String bundleFolderPath) { Bundle bundleObject = entityBundleLoader.load(bundles, BundleLoadingOperation.VALIDATE, ENCAPSULATED_ASSERTION_TYPE, EntityTypes.POLICY_TYPE, FOLDER_TYPE); LinkedList<File> processedBundles = new LinkedList<>(); for (File bundle : bundles) { Document document = parseBundleFile(bundle); Element bundleElement = document.getDocumentElement(); NodeList items = bundleElement.getElementsByTagName(ITEM); processEncasses(bundleObject, items); File processedBundle = writeProcessedBundle(bundle, bundleElement, bundleFolderPath); processedBundles.add(processedBundle); } return processedBundles; } @Inject  DependencyBundlesProcessor(EntityBundleLoader entityBundleLoader, DocumentTools documentTools, DocumentFileUtils documentFileUtils); LinkedList<File> process(final List<File> bundles, String bundleFolderPath);  }
@Test void testAttachingEncasses() throws IOException, DocumentParseException { File originFolder = new File(rootProjectDir.getRoot(), "original"); originFolder.mkdirs(); File destinationFolder = new File(rootProjectDir.getRoot(), "processed"); destinationFolder.mkdirs(); byte[] bundle1Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_1_BUNDLE)); byte[] bundle2Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_2_BUNDLE)); File file1 = new File(originFolder, TEST_1_BUNDLE); file1.createNewFile(); File file2 = new File(originFolder, TEST_2_BUNDLE); file2.createNewFile(); Files.write(file1.toPath(), bundle1Contents); Files.write(file2.toPath(), bundle2Contents); LinkedList<File> processed = processor.process(Stream.of(new File(originFolder, TEST_1_BUNDLE), new File(originFolder, TEST_2_BUNDLE)).collect(toCollection(LinkedList::new)), destinationFolder.toString()); assertNotNull(processed); assertEquals(2, processed.size()); File bundle1 = processed.get(0); assertEquals(destinationFolder.toString() + File.separator + TEST_1_BUNDLE, bundle1.toString()); File bundle2 = processed.get(1); assertEquals(destinationFolder.toString() + File.separator + TEST_2_BUNDLE, bundle2.toString()); Document bundle2Doc = DocumentTools.INSTANCE.parse(bundle2); NodeList resources = bundle2Doc.getDocumentElement().getElementsByTagName(RESOURCE); assertNotNull(resources); assertEquals(3, resources.getLength()); Element resourceElement = StreamSupport.stream(nodeList(resources).spliterator(), false).map(n -> (Element) n).filter(e -> PolicyEntityBuilder.POLICY.equals(e.getAttribute(ATTRIBUTE_TYPE))).findFirst().orElse(null); assertNotNull(resourceElement); String policyXML = resourceElement.getTextContent(); assertNotNull(policyXML); Element policyElement = DocumentTools.INSTANCE.parse(policyXML).getDocumentElement(); assertNotNull(policyElement); NodeList guids = policyElement.getElementsByTagName(ENCAPSULATED_ASSERTION_CONFIG_GUID); assertNotNull(guids); assertEquals(1, guids.getLength()); String guid = guids.item(0).getAttributes().getNamedItem(STRING_VALUE).getTextContent(); assertEquals("283e93c6-9cf6-46f1-a34a-cf333bf4f1c3", guid); }
@Test void testAttachingEncassesInServicePolicy() throws IOException, DocumentParseException { File originFolder = new File(rootProjectDir.getRoot(), "original"); originFolder.mkdirs(); File destinationFolder = new File(rootProjectDir.getRoot(), "processed"); destinationFolder.mkdirs(); byte[] bundle1Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_1_BUNDLE)); byte[] bundle4Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_4_BUNDLE)); File file1 = new File(originFolder, TEST_1_BUNDLE); file1.createNewFile(); File file4 = new File(originFolder, TEST_4_BUNDLE); file4.createNewFile(); Files.write(file1.toPath(), bundle1Contents); Files.write(file4.toPath(), bundle4Contents); LinkedList<File> processed = processor.process(Stream.of(new File(originFolder, TEST_1_BUNDLE), new File(originFolder, TEST_4_BUNDLE)).collect(toCollection(LinkedList::new)), destinationFolder.toString()); assertNotNull(processed); assertEquals(2, processed.size()); File bundle1 = processed.get(0); assertEquals(destinationFolder.toString() + File.separator + TEST_1_BUNDLE, bundle1.toString()); File bundle4 = processed.get(1); assertEquals(destinationFolder.toString() + File.separator + TEST_4_BUNDLE, bundle4.toString()); Document bundle4Doc = DocumentTools.INSTANCE.parse(bundle4); NodeList resources = bundle4Doc.getDocumentElement().getElementsByTagName(RESOURCE); assertNotNull(resources); assertEquals(3, resources.getLength()); Element resourceElement = StreamSupport.stream(nodeList(resources).spliterator(), false).map(n -> (Element) n).filter(e -> PolicyEntityBuilder.POLICY.equals(e.getAttribute(ATTRIBUTE_TYPE))).findFirst().orElse(null); assertNotNull(resourceElement); String policyXML = resourceElement.getTextContent(); assertNotNull(policyXML); Element policyElement = DocumentTools.INSTANCE.parse(policyXML).getDocumentElement(); assertNotNull(policyElement); NodeList guids = policyElement.getElementsByTagName(ENCAPSULATED_ASSERTION_CONFIG_GUID); assertNotNull(guids); assertEquals(1, guids.getLength()); String guid = guids.item(0).getAttributes().getNamedItem(STRING_VALUE).getTextContent(); assertEquals("283e93c6-9cf6-46f1-a34a-cf333bf4f1c3", guid); }
@Test void testAttachingEncasses_notFound() throws IOException, DocumentParseException { File originFolder = new File(rootProjectDir.getRoot(), "original"); originFolder.mkdirs(); File destinationFolder = new File(rootProjectDir.getRoot(), "processed"); destinationFolder.mkdirs(); byte[] bundle2Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_2_BUNDLE)); byte[] bundle3Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_3_BUNDLE)); File file2 = new File(originFolder, TEST_2_BUNDLE); file2.createNewFile(); File file3 = new File(originFolder, TEST_3_BUNDLE); file3.createNewFile(); Files.write(file2.toPath(), bundle2Contents); Files.write(file3.toPath(), bundle3Contents); LinkedList<File> processed = processor.process(Stream.of(new File(originFolder, TEST_2_BUNDLE), new File(originFolder, TEST_3_BUNDLE)).collect(toCollection(LinkedList::new)), destinationFolder.toString()); assertNotNull(processed); assertEquals(2, processed.size()); File bundle3 = processed.get(1); assertEquals(destinationFolder.toString() + File.separator + TEST_3_BUNDLE, bundle3.toString()); File bundle2 = processed.get(0); assertEquals(destinationFolder.toString() + File.separator + TEST_2_BUNDLE, bundle2.toString()); Document bundle2Doc = DocumentTools.INSTANCE.parse(bundle2); NodeList resources = bundle2Doc.getDocumentElement().getElementsByTagName(RESOURCE); assertNotNull(resources); assertEquals(3, resources.getLength()); Element resourceElement = StreamSupport.stream(nodeList(resources).spliterator(), false).map(n -> (Element) n).filter(e -> PolicyEntityBuilder.POLICY.equals(e.getAttribute(ATTRIBUTE_TYPE))).findFirst().orElse(null); assertNotNull(resourceElement); String policyXML = resourceElement.getTextContent(); assertNotNull(policyXML); Element policyElement = DocumentTools.INSTANCE.parse(policyXML).getDocumentElement(); assertNotNull(policyElement); NodeList guids = policyElement.getElementsByTagName(ENCAPSULATED_ASSERTION_CONFIG_GUID); assertNotNull(guids); assertEquals(1, guids.getLength()); String guid = guids.item(0).getAttributes().getNamedItem(STRING_VALUE).getTextContent(); assertEquals(ZERO_GUID, guid); }
@Test void testErrorOnDuplicatePolicies() throws IOException { File originFolder = new File(rootProjectDir.getRoot(), "original"); originFolder.mkdirs(); File destinationFolder = new File(rootProjectDir.getRoot(), "processed"); destinationFolder.mkdirs(); byte[] bundle2Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_2_BUNDLE)); byte[] bundle3Contents = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource(TEST_5_BUNDLE)); File file2 = new File(originFolder, TEST_2_BUNDLE); file2.createNewFile(); File file3 = new File(originFolder, TEST_5_BUNDLE); file3.createNewFile(); Files.write(file2.toPath(), bundle2Contents); Files.write(file3.toPath(), bundle3Contents); assertThrows(BundleLoadException.class, () -> processor.process(Stream.of(new File(originFolder, TEST_2_BUNDLE), new File(originFolder, TEST_3_BUNDLE)).collect(toCollection(LinkedList::new)), destinationFolder.toString())); }