View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index.reader;
20  
21  import java.util.Map;
22  import java.util.TreeSet;
23  import java.util.function.Function;
24  import java.util.stream.Collectors;
25  import java.util.stream.Stream;
26  import java.util.stream.StreamSupport;
27  
28  import org.apache.maven.index.reader.Record.Type;
29  
30  import static org.apache.maven.index.reader.Utils.allGroups;
31  import static org.apache.maven.index.reader.Utils.descriptor;
32  import static org.apache.maven.index.reader.Utils.rootGroup;
33  import static org.apache.maven.index.reader.Utils.rootGroups;
34  
35  /**
36   * Helpers to transform records from one to another representation, and, some helpers for publishing using Guava.
37   */
38  public final class TestUtils {
39      private TestUtils() {
40          // nothing
41      }
42  
43      private static final RecordCompactor RECORD_COMPACTOR = new RecordCompactor();
44  
45      private static final RecordExpander RECORD_EXPANDER = new RecordExpander();
46  
47      public static Function<Record, Map<String, String>> compactFunction = RECORD_COMPACTOR;
48  
49      public static Function<Map<String, String>, Record> expandFunction = RECORD_EXPANDER;
50  
51      /**
52       * Helper method, that "decorates" the stream of records to be written out with "special" Maven Indexer records, so
53       * all the caller is needed to provide {@link Iterable} or {@link Record}s <strong>to be</strong> on the index, with
54       * record type {@link Type#ARTIFACT_ADD}. This method will create the output as "proper" Maven Indexer record
55       * stream, by adding the {@link Type#DESCRIPTOR}, {@link Type#ROOT_GROUPS} and {@link Type#ALL_GROUPS} special
56       * records.
57       */
58      public static Iterable<Record> decorate(final Iterable<Record> iterable, final String repoId) {
59          final TreeSet<String> allGroupsSet = new TreeSet<>();
60          final TreeSet<String> rootGroupsSet = new TreeSet<>();
61          return Stream.concat(
62                          Stream.of(descriptor(repoId)),
63                          Stream.concat(
64                                  StreamSupport.stream(iterable.spliterator(), false),
65                                  Stream.concat(
66                                          Stream.of(allGroups(allGroupsSet)),
67                                          // placeholder, will be recreated at the end with proper content
68                                          Stream.of(rootGroups(rootGroupsSet))))
69                          // placeholder, will be recreated at the end with proper content
70                          )
71                  .map(rec -> {
72                      if (Type.DESCRIPTOR == rec.getType()) {
73                          return rec;
74                      } else if (Type.ALL_GROUPS == rec.getType()) {
75                          return allGroups(allGroupsSet);
76                      } else if (Type.ROOT_GROUPS == rec.getType()) {
77                          return rootGroups(rootGroupsSet);
78                      } else {
79                          final String groupId = rec.getString(Record.GROUP_ID);
80                          if (groupId != null) {
81                              allGroupsSet.add(groupId);
82                              rootGroupsSet.add(rootGroup(groupId));
83                          }
84                          return rec;
85                      }
86                  })
87                  .collect(Collectors.toList());
88      }
89  }