Skip to content

Commit 59aecc3

Browse files
committed
Add two unit tests
1 parent bca82ea commit 59aecc3

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed

core-java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
<groupId>org.apache.commons</groupId>
6969
<artifactId>commons-lang3</artifactId>
7070
</dependency>
71+
<dependency>
72+
<groupId>org.commonjava.util</groupId>
73+
<artifactId>http-testserver</artifactId>
74+
<scope>test</scope>
75+
</dependency>
7176
</dependencies>
7277

7378
</project>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.client.core;
17+
18+
import org.apache.commons.io.IOUtils;
19+
import org.commonjava.indy.client.core.module.IndyStoreQueryClientModule;
20+
import org.commonjava.indy.client.core.module.IndyStoresClientModule;
21+
import org.commonjava.indy.client.core.util.UrlUtils;
22+
import org.commonjava.indy.model.core.Group;
23+
import org.commonjava.indy.model.core.HostedRepository;
24+
import org.commonjava.indy.model.core.RemoteRepository;
25+
import org.commonjava.indy.model.core.StoreKey;
26+
import org.commonjava.indy.model.core.StoreType;
27+
import org.commonjava.indy.model.core.io.IndyObjectMapper;
28+
import org.commonjava.test.http.expect.ExpectationServer;
29+
import org.commonjava.util.jhttpc.model.SiteConfig;
30+
import org.commonjava.util.jhttpc.model.SiteConfigBuilder;
31+
import org.junit.Before;
32+
import org.junit.Rule;
33+
import org.junit.Test;
34+
35+
import java.io.IOException;
36+
import java.net.URL;
37+
import java.nio.charset.Charset;
38+
import java.util.Arrays;
39+
import java.util.Collection;
40+
41+
import static org.commonjava.indy.client.core.util.UrlUtils.normalizePath;
42+
import static org.hamcrest.CoreMatchers.equalTo;
43+
import static org.hamcrest.MatcherAssert.assertThat;
44+
import static org.junit.Assert.assertNotNull;
45+
46+
public abstract class AbstractIndyClientTest
47+
{
48+
private static final String BASE_STORE_PATH = "/api/admin/stores";
49+
50+
@Rule
51+
public ExpectationServer server = new ExpectationServer();
52+
53+
private final IndyObjectMapper mapper = new IndyObjectMapper( false );
54+
55+
private Indy client;
56+
57+
@Before
58+
public void setUp()
59+
{
60+
final String serviceUrl = UrlUtils.buildUrl( server.getBaseUri(), "api" );
61+
62+
SiteConfig config = new SiteConfigBuilder( "indy", serviceUrl ).withRequestTimeoutSeconds( 30 )
63+
.withMetricEnabled( false )
64+
.build();
65+
Collection<IndyClientModule> modules =
66+
Arrays.asList( new IndyStoresClientModule(), new IndyStoreQueryClientModule() );
67+
68+
client = getClient( config, modules );
69+
}
70+
71+
protected abstract Indy getClient(SiteConfig siteConfig, Collection<IndyClientModule> modules);
72+
73+
@Test
74+
public void testGetStores()
75+
throws Exception
76+
{
77+
String path = normalizePath( BASE_STORE_PATH, "maven/remote/central" );
78+
server.expect( path, 200, readResource( "repo-service/remote-central.json" ) );
79+
StoreKey key = StoreKey.fromString( "maven:remote:central" );
80+
RemoteRepository remote = client.module( IndyStoresClientModule.class ).load( key, RemoteRepository.class );
81+
assertNotNull( remote );
82+
assertThat( remote.getKey(), equalTo( key ) );
83+
assertThat( remote.getType(), equalTo( StoreType.remote ) );
84+
85+
path = normalizePath( BASE_STORE_PATH, "maven/hosted/local-deployments" );
86+
server.expect( path, 200, readResource( "repo-service/hosted-localdeploy.json" ) );
87+
key = StoreKey.fromString( "maven:hosted:local-deployments" );
88+
HostedRepository hosted = client.module( IndyStoresClientModule.class ).load( key, HostedRepository.class );
89+
assertNotNull( hosted );
90+
assertThat( hosted.getKey(), equalTo( key ) );
91+
assertThat( hosted.getType(), equalTo( StoreType.hosted ) );
92+
93+
path = normalizePath( BASE_STORE_PATH, "maven/group/static" );
94+
server.expect( path, 200, readResource( "repo-service/group-static.json" ) );
95+
key = StoreKey.fromString( "maven:group:static" );
96+
Group group = client.module( IndyStoresClientModule.class ).load( key, Group.class );
97+
assertNotNull( group );
98+
assertThat( group.getKey(), equalTo( key ) );
99+
assertThat( group.getType(), equalTo( StoreType.group ) );
100+
}
101+
102+
static String readResource( final String resourcePath )
103+
throws IOException
104+
{
105+
URL url = Thread.currentThread().getContextClassLoader().getResource( resourcePath );
106+
if ( url != null )
107+
{
108+
return IOUtils.toString( url.openStream(), Charset.defaultCharset() );
109+
}
110+
throw new IOException( String.format( "File not exists: %s", resourcePath ) );
111+
}
112+
113+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.client.core;
17+
18+
import org.commonjava.indy.client.core.o11y.trace.ClientTracerConfiguration;
19+
import org.commonjava.indy.model.core.io.IndyObjectMapper;
20+
import org.commonjava.util.jhttpc.auth.MemoryPasswordManager;
21+
import org.commonjava.util.jhttpc.model.SiteConfig;
22+
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
26+
public class IndyClientWithTraceTest
27+
extends AbstractIndyClientTest
28+
{
29+
30+
@Override
31+
protected Indy getClient( SiteConfig siteConfig, Collection<IndyClientModule> modules )
32+
{
33+
try
34+
{
35+
36+
final Indy.Builder builder = Indy.builder()
37+
.setLocation( siteConfig )
38+
.setObjectMapper( new IndyObjectMapper( Collections.emptySet() ) )
39+
.setMdcCopyMappings( Collections.emptyMap() )
40+
.setModules( modules.toArray( new IndyClientModule[0] ) );
41+
42+
ClientTracerConfiguration clientTracerConfig = new ClientTracerConfiguration();
43+
clientTracerConfig.setEnabled( true );
44+
clientTracerConfig.setConsoleTransport( true );
45+
builder.setTraceConfiguration( clientTracerConfig );
46+
builder.setPasswordManager( new MemoryPasswordManager() );
47+
48+
return builder.build();
49+
}
50+
catch ( IndyClientException e )
51+
{
52+
throw new RuntimeException( e );
53+
}
54+
}
55+
56+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.client.core;
17+
18+
import org.commonjava.indy.model.core.io.IndyObjectMapper;
19+
import org.commonjava.util.jhttpc.auth.MemoryPasswordManager;
20+
import org.commonjava.util.jhttpc.model.SiteConfig;
21+
22+
import java.util.Collection;
23+
import java.util.Collections;
24+
25+
public class IndyClientWithoutTraceTest
26+
extends AbstractIndyClientTest
27+
{
28+
@Override
29+
protected Indy getClient( SiteConfig siteConfig, Collection<IndyClientModule> modules )
30+
{
31+
try
32+
{
33+
34+
final Indy.Builder builder = Indy.builder()
35+
.setLocation( siteConfig )
36+
.setObjectMapper( new IndyObjectMapper( Collections.emptySet() ) )
37+
.setMdcCopyMappings( Collections.emptyMap() )
38+
.setModules( modules.toArray( new IndyClientModule[0] ) );
39+
builder.setPasswordManager( new MemoryPasswordManager() );
40+
return builder.build();
41+
}
42+
catch ( IndyClientException e )
43+
{
44+
throw new RuntimeException( e );
45+
}
46+
}
47+
48+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"type" : "group",
3+
"key" : "maven:group:static",
4+
"description" : "This group provides faster access to static data, that are stored in Indy.",
5+
"metadata" : {
6+
"changelog" : ""
7+
},
8+
"disabled" : false,
9+
"constituents" : [ "maven:group:builds-untested+shared-imports" ],
10+
"name" : "static",
11+
"packageType" : "maven",
12+
"disable_timeout" : -1,
13+
"path_style" : "plain",
14+
"authoritative_index" : false,
15+
"create_time" : "2019-12-12 21:06:58 +0000",
16+
"prepend_constituent" : false
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type" : "hosted",
3+
"key" : "maven:hosted:local-deployments",
4+
"disabled" : false,
5+
"snapshotTimeoutSeconds" : 86400,
6+
"readonly" : false,
7+
"name" : "local-deployments",
8+
"packageType" : "maven",
9+
"disable_timeout" : 0,
10+
"path_style" : "plain",
11+
"authoritative_index" : false,
12+
"create_time" : "2022-06-29 02:08:33 +0000",
13+
"allow_snapshots" : true,
14+
"allow_releases" : true
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"type" : "remote",
3+
"key" : "maven:remote:central",
4+
"disabled" : false,
5+
"host" : "repo.maven.apache.org",
6+
"port" : 443,
7+
"name" : "central",
8+
"packageType" : "maven",
9+
"disable_timeout" : 0,
10+
"path_style" : "plain",
11+
"authoritative_index" : false,
12+
"create_time" : "2022-06-29 02:08:33 +0000",
13+
"allow_snapshots" : false,
14+
"allow_releases" : true,
15+
"url" : "https://repo.maven.apache.org/maven2/",
16+
"timeout_seconds" : 0,
17+
"max_connections" : 30,
18+
"ignore_hostname_verification" : false,
19+
"nfc_timeout_seconds" : 0,
20+
"is_passthrough" : false,
21+
"cache_timeout_seconds" : 86400,
22+
"metadata_timeout_seconds" : 0,
23+
"proxy_port" : 0,
24+
"prefetch_priority" : 0,
25+
"prefetch_rescan" : false,
26+
"prefetch_listing_type" : "html"
27+
}

0 commit comments

Comments
 (0)