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.io.IOException;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.stream.Collectors;
27  import java.util.stream.StreamSupport;
28  
29  import org.apache.maven.index.reader.Record.EntryKey;
30  import org.apache.maven.index.reader.Record.Type;
31  import org.junit.Test;
32  
33  import static org.apache.maven.index.reader.TestUtils.compactFunction;
34  import static org.apache.maven.index.reader.TestUtils.decorate;
35  import static org.hamcrest.CoreMatchers.nullValue;
36  import static org.hamcrest.MatcherAssert.assertThat;
37  import static org.hamcrest.core.IsEqual.equalTo;
38  
39  /**
40   * UT for {@link RecordCompactor} and {@link RecordExpander}.
41   */
42  public class TransformTest extends TestSupport {
43      @Test
44      public void decorateAndTransform() throws IOException {
45          final String indexId = "test";
46          final Record r1 = new Record(Type.ARTIFACT_ADD, artifactMap("org.apache"));
47          final Record r2 = new Record(Type.ARTIFACT_ADD, artifactMap("org.foo"));
48          final Record r3 = new Record(Type.ARTIFACT_ADD, artifactMap("com.bar"));
49  
50          Iterable<Map<String, String>> iterable = StreamSupport.stream(
51                          decorate(Arrays.asList(r1, r2, r3), indexId).spliterator(), false)
52                  .map(compactFunction)
53                  .collect(Collectors.toList());
54  
55          ;
56          try (WritableResourceHandler writableResourceHandler = createWritableResourceHandler()) {
57              try (IndexWriter indexWriter = new IndexWriter(writableResourceHandler, indexId, false)) {
58                  indexWriter.writeChunk(iterable.iterator());
59                  indexWriter.close();
60              }
61  
62              try (IndexReader indexReader = new IndexReader(null, writableResourceHandler)) {
63                  assertThat(indexReader.getChunkNames(), equalTo(List.of("nexus-maven-repository-index.gz")));
64                  ChunkReader chunkReader = indexReader.iterator().next();
65                  final Map<Type, List<Record>> recordTypes = loadRecordsByType(chunkReader);
66                  assertThat(recordTypes.get(Type.DESCRIPTOR).size(), equalTo(1));
67                  assertThat(recordTypes.get(Type.ROOT_GROUPS).size(), equalTo(1));
68                  assertThat(recordTypes.get(Type.ALL_GROUPS).size(), equalTo(1));
69                  assertThat(recordTypes.get(Type.ARTIFACT_ADD).size(), equalTo(3));
70                  assertThat(recordTypes.get(Type.ARTIFACT_REMOVE), nullValue());
71  
72                  assertThat(
73                          recordTypes.get(Type.ROOT_GROUPS).get(0).get(Record.ROOT_GROUPS),
74                          equalTo(new String[] {"com", "org"}));
75                  assertThat(
76                          recordTypes.get(Type.ALL_GROUPS).get(0).get(Record.ALL_GROUPS),
77                          equalTo(new String[] {"com.bar", "org.apache", "org.foo"}));
78              }
79          }
80      }
81  
82      private Map<EntryKey, Object> artifactMap(final String groupId) {
83          final HashMap<EntryKey, Object> result = new HashMap<>();
84          result.put(Record.GROUP_ID, groupId);
85          result.put(Record.ARTIFACT_ID, "artifact");
86          result.put(Record.VERSION, "1.0");
87          result.put(Record.PACKAGING, "jar");
88          result.put(Record.FILE_MODIFIED, System.currentTimeMillis());
89          result.put(Record.FILE_SIZE, 123L);
90          result.put(Record.FILE_EXTENSION, "jar");
91          result.put(Record.HAS_SOURCES, Boolean.FALSE);
92          result.put(Record.HAS_JAVADOC, Boolean.FALSE);
93          result.put(Record.HAS_SIGNATURE, Boolean.FALSE);
94          return result;
95      }
96  }