test_class.declarator: testBaseDir = new File("target/repos/PrivilegeXmlExporterTest")
test_class.modifier: final
test_class.original_string: final File testBaseDir = new File("target/repos/PrivilegeXmlExporterTest");
test_class.type: File
test_class.var_name: testBaseDir
test_class.file: webster/src/test/java/net/adamcin/oakpal/webster/PrivilegeXmlExporterTest.java
test_class.identifier: PrivilegeXmlExporterTest
test_case.body: @Test
      public void testWritePrivileges_defaults() throws Exception {
          final File tempDir = new File(testBaseDir, "testWritePrivileges_defaults");
          final File fromRepoDir = new File(tempDir, "fromRepo/segmentstore");
          final File toRepoDir = new File(tempDir, "toRepo/segmentstore");
          final File exportedXml = new File(tempDir, "exported.xml");
  
          TestUtil.prepareRepo(fromRepoDir, session -> {
              final URL aemPrivileges = getClass().getResource("/aem_privileges.xml");
              TestUtil.installPrivilegesFromURL(session, aemPrivileges);
          });
  
          if (exportedXml.exists()) {
              exportedXml.delete();
          }
  
          TestUtil.withReadOnlyFixture(fromRepoDir, session -> {
              final Workspace workspace = session.getWorkspace();
              final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager();
  
              assertNotNull("crx:replicate should be imported",
                      privilegeManager.getPrivilege("crx:replicate"));
              assertNotNull("cq:storeUGC should be imported",
                      privilegeManager.getPrivilege("cq:storeUGC"));
  
              PrivilegeXmlExporter.writePrivileges(() -> new OutputStreamWriter(
                              new FileOutputStream(exportedXml)),
                      session, null, true);
          });
  
          TestUtil.prepareRepo(toRepoDir, session -> {
              final URL exportedUrl = exportedXml.toURL();
              resultNothing1((URL url) -> TestUtil.installPrivilegesWithIndivResults(session, url)).apply(exportedUrl);
          });
  
          TestUtil.withReadOnlyFixture(toRepoDir, session -> {
              final Workspace workspace = session.getWorkspace();
              final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager();
  
              assertNotNull("crx:replicate should be imported",
                      privilegeManager.getPrivilege("crx:replicate"));
          });
      }
test_case.class_method_signature: PrivilegeXmlExporterTest.testWritePrivileges_defaults()
test_case.full_signature: @Test public void testWritePrivileges_defaults()
test_case.identifier: testWritePrivileges_defaults
test_case.invocations: prepareRepo getResource getClass installPrivilegesFromURL exists delete withReadOnlyFixture getWorkspace getPrivilegeManager assertNotNull getPrivilege assertNotNull getPrivilege writePrivileges prepareRepo toURL apply resultNothing1 installPrivilegesWithIndivResults withReadOnlyFixture getWorkspace getPrivilegeManager assertNotNull getPrivilege
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testWritePrivileges_defaults()
focal_class.file: webster/src/main/java/net/adamcin/oakpal/webster/PrivilegeXmlExporter.java
focal_class.identifier: PrivilegeXmlExporter
focal_class.class_method_signature: PrivilegeXmlExporter.PrivilegeXmlExporter()
focal_class.full_signature: private  PrivilegeXmlExporter()
focal_class.identifier: PrivilegeXmlExporter
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.signature:  PrivilegeXmlExporter()
focal_class.class_method_signature: PrivilegeXmlExporter.writePrivileges(final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
focal_class.full_signature: public static void writePrivileges(final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
focal_class.identifier: writePrivileges
focal_class.modifiers: public static
focal_class.parameters: (final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
focal_class.return: void
focal_class.signature: void writePrivileges(final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
focal_method.body: public static void writePrivileges(final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
              throws RepositoryException, IOException {
  
          final Workspace workspace = session.getWorkspace();
          if (!(workspace instanceof JackrabbitWorkspace)) {
              throw new RepositoryException("Workspace must be instance of JackrabbitWorkspace, but isn't. type: " +
                      workspace.getClass().getName());
          }
  
          // get a name mapper function
          final NamePathResolver resolver = new DefaultNamePathResolver(session);
          final Function<String, Name> mapper = Fun.tryOrDefault1(resolver::getQName, null);
          // first resolve the builtins to qualified Names
          final Set<Name> builtinPrivileges = JsonCnd.BUILTIN_PRIVILEGES.stream()
                  .map(mapper).filter(Objects::nonNull).collect(toSet());
          // create the builtinFilter for reuse
          final Predicate<Name> builtinFilter = includeBuiltins
                  ? name -> true
                  : ((Predicate<Name>) builtinPrivileges::contains).negate();
          // construct the desiredPrivileges set based on the names argument, filtering by the above builtinFilter
          final Set<Name> explicitNames = (onlyNames != null)
                  ? onlyNames.stream().map(mapper).filter(Objects::nonNull).filter(builtinFilter).collect(toSet())
                  : Collections.emptySet();
          // finally construct the privilegeFilter, which either includes only the desired
          final Predicate<Name> shouldInclude = !explicitNames.isEmpty()
                  ? explicitNames::contains
                  : builtinFilter;
  
          final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager();
          final List<PrivilegeDefinition> privilegeDefinitions = new ArrayList<>();
          for (Privilege privilege : privilegeManager.getRegisteredPrivileges()) {
              if (!shouldInclude.test(mapper.apply(privilege.getName()))) {
                  continue;
              }
              final PrivilegeDefinitionImpl privilegeDefinition =
                      new PrivilegeDefinitionImpl(
                              mapper.apply(privilege.getName()),
                              privilege.isAbstract(),
                              Stream.of(privilege.getDeclaredAggregatePrivileges())
                                      .map(Privilege::getName)
                                      .map(mapper)
                                      .filter(Objects::nonNull)
                                      .collect(toSet()));
              privilegeDefinitions.add(privilegeDefinition);
          }
  
          // identify only those namespaces referenced by included privilege names or their aggregate members
          final Set<String> uris = privilegeDefinitions.stream()
                  .flatMap(def -> Stream.concat(Stream.of(def.getName()), def.getDeclaredAggregateNames().stream()))
                  .map(Name::getNamespaceURI).collect(toSet());
  
          final Map<String, String> namespaces = uris.stream()
                  .collect(toMap(Fun.tryOrDefault1(session::getNamespacePrefix, null),
                          Function.identity()));
  
          PrivilegeDefinitionWriter pdWriter = new PrivilegeDefinitionWriter("text/xml");
          try (Writer writer = writerOpener.open()) {
              pdWriter.writeDefinitions(writer, privilegeDefinitions.toArray(new PrivilegeDefinition[0]), namespaces);
          }
      }
focal_method.class_method_signature: PrivilegeXmlExporter.writePrivileges(final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
focal_method.full_signature: public static void writePrivileges(final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
focal_method.identifier: writePrivileges
focal_method.invocations: getWorkspace getName getClass tryOrDefault1 collect filter map stream toSet negate collect filter filter map stream toSet emptySet isEmpty getPrivilegeManager getRegisteredPrivileges test apply getName apply getName isAbstract collect filter map map of getDeclaredAggregatePrivileges toSet add collect map flatMap stream concat of getName stream getDeclaredAggregateNames toSet collect stream toMap tryOrDefault1 identity open writeDefinitions toArray
focal_method.modifiers: public static
focal_method.parameters: (final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)
focal_method.return: void
focal_method.signature: void writePrivileges(final WriterOpener writerOpener,
                                         final Session session,
                                         final List<String> onlyNames,
                                         final boolean includeBuiltins)