View Javadoc
1   package org.apache.archiva.rest.services;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
22  import org.apache.archiva.rest.api.services.RepositoriesService;
23  import org.assertj.core.api.Assertions;
24  import org.junit.Ignore;
25  import org.junit.Test;
26  
27  import javax.ws.rs.InternalServerErrorException;
28  import java.io.File;
29  
30  /**
31   * @author Olivier Lamy
32   */
33  public class CopyArtifactTest
34      extends AbstractArchivaRestTest
35  {
36  
37  
38      @Test
39      public void copyToAnEmptyRepo()
40          throws Exception
41      {
42          try
43          {
44              initSourceTargetRepo();
45  
46              // START SNIPPET: copy-artifact
47              // configure the artifact you want to copy
48              // if package ommited default will be jar
49              ArtifactTransferRequest artifactTransferRequest = new ArtifactTransferRequest();
50              artifactTransferRequest.setGroupId( "org.apache.karaf.features" );
51              artifactTransferRequest.setArtifactId( "org.apache.karaf.features.core" );
52              artifactTransferRequest.setVersion( "2.2.2" );
53              artifactTransferRequest.setRepositoryId( SOURCE_REPO_ID );
54              artifactTransferRequest.setTargetRepositoryId( TARGET_REPO_ID );
55              // retrieve the service
56              RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
57              // copy the artifact
58              Boolean res = repositoriesService.copyArtifact( artifactTransferRequest );
59              // END SNIPPET: copy-artifact
60              assertTrue( res );
61  
62              String targetRepoPath = getManagedRepositoriesService( authorizationHeader ).getManagedRepository(
63                  TARGET_REPO_ID ).getLocation();
64  
65              File artifact = new File( targetRepoPath,
66                                        "/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
67              assertTrue( artifact.exists() );
68              File pom = new File( targetRepoPath,
69                                   "/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.pom" );
70  
71              assertTrue( "not exists " + pom.getPath(), pom.exists() );
72              // TODO find a way to force metadata generation and test it !!
73          }
74          finally
75          {
76              cleanRepos();
77          }
78      }
79  
80      @Test( expected = InternalServerErrorException.class )
81      public void copyNonExistingArtifact()
82          throws Throwable
83      {
84          try
85          {
86              initSourceTargetRepo();
87  
88              ArtifactTransferRequest artifactTransferRequest = new ArtifactTransferRequest();
89              artifactTransferRequest.setGroupId( "org.apache.karaf.features" );
90              artifactTransferRequest.setArtifactId( "org.apache.karaf.features.core" );
91              artifactTransferRequest.setVersion( "3.0.6552" );
92              artifactTransferRequest.setRepositoryId( SOURCE_REPO_ID );
93              artifactTransferRequest.setTargetRepositoryId( TARGET_REPO_ID );
94              RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
95  
96              repositoriesService.copyArtifact( artifactTransferRequest );
97          }
98          catch ( InternalServerErrorException e )
99          {
100             // FIXME this doesn't work anymore with cxf 3.x????
101             //Assertions.assertThat( e.getResponse().getStatusInfo().getReasonPhrase() ) //
102             //    .contains( "cannot find artifact" );
103 
104             // previous test with cxf 2.x
105             //assertTrue( e.getMessage() + " do not contains ''",
106             //            StringUtils.contains( e.getMessage(), "cannot find artifact" ) );
107             throw e;
108         }
109         finally
110         {
111             cleanRepos();
112         }
113 
114     }
115 
116     @Ignore
117     public void copyToAnExistingRepo()
118         throws Exception
119     {
120         initSourceTargetRepo();
121         cleanRepos();
122     }
123 }