View Javadoc
1   package org.apache.archiva.webtest.memory;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.metadata.model.ArtifactMetadata;
23  import org.apache.archiva.metadata.model.ProjectVersionMetadata;
24  import org.apache.archiva.metadata.model.ProjectVersionReference;
25  import org.apache.archiva.metadata.repository.MetadataResolver;
26  import org.apache.archiva.metadata.repository.RepositorySession;
27  
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.LinkedHashSet;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  
36  public class TestMetadataResolver
37      implements MetadataResolver
38  {
39      private Map<String, ProjectVersionMetadata> projectVersions = new HashMap<>();
40  
41      private Map<String, List<ArtifactMetadata>> artifacts = new HashMap<>();
42  
43      private Map<String, List<ProjectVersionReference>> references =
44          new HashMap<String, List<ProjectVersionReference>>();
45  
46      private Map<String, List<String>> namespaces = new HashMap<>();
47  
48      private Map<String, Collection<String>> projectsInNamespace = new HashMap<>();
49  
50      private Map<String, Collection<String>> versionsInProject = new HashMap<>();
51  
52      @Override
53      public ProjectVersionMetadata resolveProjectVersion( RepositorySession repositorySession, String repoId,
54                                                           String namespace, String projectId, String projectVersion )
55      {
56          return projectVersions.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
57      }
58  
59      @Override
60      public Collection<ProjectVersionReference> resolveProjectReferences( RepositorySession repositorySession,
61                                                                           String repoId, String namespace,
62                                                                           String projectId, String projectVersion )
63      {
64          Collection<ProjectVersionReference> projectVersionReferences =
65              references.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
66          return projectVersionReferences;
67      }
68  
69      @Override
70      public Collection<String> resolveRootNamespaces( RepositorySession repositorySession, String repoId )
71      {
72          return resolveNamespaces( repositorySession, repoId, null );
73      }
74  
75      @Override
76      public Collection<String> resolveNamespaces( RepositorySession repositorySession, String repoId,
77                                                   String baseNamespace )
78      {
79          Set<String> namespaces = new LinkedHashSet<String>();
80          int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
81          for ( String namespace : this.namespaces.get( repoId ) )
82          {
83              if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
84              {
85                  int i = namespace.indexOf( '.', fromIndex );
86                  if ( i >= 0 )
87                  {
88                      namespaces.add( namespace.substring( fromIndex, i ) );
89                  }
90                  else
91                  {
92                      namespaces.add( namespace.substring( fromIndex ) );
93                  }
94              }
95          }
96          return namespaces;
97      }
98  
99      @Override
100     public Collection<String> resolveProjects( RepositorySession repositorySession, String repoId, String namespace )
101     {
102         Collection<String> list = projectsInNamespace.get( namespace );
103         return list != null ? list : Collections.<String>emptyList();
104     }
105 
106     @Override
107     public Collection<String> resolveProjectVersions( RepositorySession repositorySession, String repoId,
108                                                       String namespace, String projectId )
109     {
110         Collection<String> list = versionsInProject.get( namespace + ":" + projectId );
111         return list != null ? list : Collections.<String>emptyList();
112     }
113 
114     @Override
115     public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession repositorySession, String repoId,
116                                                           String namespace, String projectId, String projectVersion )
117     {
118         List<ArtifactMetadata> artifacts =
119             this.artifacts.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
120         return ( artifacts != null ? artifacts : Collections.<ArtifactMetadata>emptyList() );
121     }
122 
123     public void setProjectVersion( String repoId, String namespace, String projectId,
124                                    ProjectVersionMetadata versionMetadata )
125     {
126         projectVersions.put( createMapKey( repoId, namespace, projectId, versionMetadata.getId() ), versionMetadata );
127 
128         Collection<String> projects = projectsInNamespace.get( namespace );
129         if ( projects == null )
130         {
131             projects = new LinkedHashSet<String>();
132             projectsInNamespace.put( namespace, projects );
133         }
134         projects.add( projectId );
135 
136         String key = namespace + ":" + projectId;
137         Collection<String> versions = versionsInProject.get( key );
138         if ( versions == null )
139         {
140             versions = new LinkedHashSet<String>();
141             versionsInProject.put( key, versions );
142         }
143         versions.add( versionMetadata.getId() );
144     }
145 
146     public void setArtifacts( String repoId, String namespace, String projectId, String projectVersion,
147                               List<ArtifactMetadata> artifacts )
148     {
149         this.artifacts.put( createMapKey( repoId, namespace, projectId, projectVersion ), artifacts );
150     }
151 
152     private String createMapKey( String repoId, String namespace, String projectId, String projectVersion )
153     {
154         return repoId + ":" + namespace + ":" + projectId + ":" + projectVersion;
155     }
156 
157     public void setProjectReferences( String repoId, String namespace, String projectId, String projectVersion,
158                                       List<ProjectVersionReference> references )
159     {
160         this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references );
161     }
162 
163     public void setNamespaces( String repoId, List<String> namespaces )
164     {
165         this.namespaces.put( repoId, namespaces );
166     }
167 }