Skip to content

Commit

Permalink
Merge pull request #35 from ligangty/o11y
Browse files Browse the repository at this point in the history
Add two unit tests
  • Loading branch information
ligangty authored Jul 19, 2024
2 parents bca82ea + 7b8d4c4 commit e9ca4ac
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 10 deletions.
5 changes: 5 additions & 0 deletions core-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.commonjava.util</groupId>
<artifactId>http-testserver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2024 Red Hat, Inc.
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.client.core;

import org.apache.commons.io.IOUtils;
import org.commonjava.indy.client.core.module.IndyStoreQueryClientModule;
import org.commonjava.indy.client.core.module.IndyStoresClientModule;
import org.commonjava.indy.client.core.util.UrlUtils;
import org.commonjava.indy.model.core.Group;
import org.commonjava.indy.model.core.HostedRepository;
import org.commonjava.indy.model.core.RemoteRepository;
import org.commonjava.indy.model.core.StoreKey;
import org.commonjava.indy.model.core.StoreType;
import org.commonjava.indy.model.core.io.IndyObjectMapper;
import org.commonjava.test.http.expect.ExpectationServer;
import org.commonjava.util.jhttpc.model.SiteConfig;
import org.commonjava.util.jhttpc.model.SiteConfigBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;

import static org.commonjava.indy.client.core.util.UrlUtils.normalizePath;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;

public abstract class AbstractIndyClientTest
{
private static final String BASE_STORE_PATH = "/api/admin/stores";

@Rule
public ExpectationServer server = new ExpectationServer();

private final IndyObjectMapper mapper = new IndyObjectMapper( false );

private Indy client;

@Before
public void setUp()
{
final String serviceUrl = UrlUtils.buildUrl( server.getBaseUri(), "api" );

SiteConfig config = new SiteConfigBuilder( "indy", serviceUrl ).withRequestTimeoutSeconds( 30 )
.withMetricEnabled( false )
.build();
Collection<IndyClientModule> modules =
Arrays.asList( new IndyStoresClientModule(), new IndyStoreQueryClientModule() );

client = getClient( config, modules );
}

protected abstract Indy getClient(SiteConfig siteConfig, Collection<IndyClientModule> modules);

@Test
public void testGetStores()
throws Exception
{
String path = normalizePath( BASE_STORE_PATH, "maven/remote/central" );
server.expect( path, 200, readResource( "repo-service/remote-central.json" ) );
StoreKey key = StoreKey.fromString( "maven:remote:central" );
RemoteRepository remote = client.module( IndyStoresClientModule.class ).load( key, RemoteRepository.class );
assertNotNull( remote );
assertThat( remote.getKey(), equalTo( key ) );
assertThat( remote.getType(), equalTo( StoreType.remote ) );

path = normalizePath( BASE_STORE_PATH, "maven/hosted/local-deployments" );
server.expect( path, 200, readResource( "repo-service/hosted-localdeploy.json" ) );
key = StoreKey.fromString( "maven:hosted:local-deployments" );
HostedRepository hosted = client.module( IndyStoresClientModule.class ).load( key, HostedRepository.class );
assertNotNull( hosted );
assertThat( hosted.getKey(), equalTo( key ) );
assertThat( hosted.getType(), equalTo( StoreType.hosted ) );

path = normalizePath( BASE_STORE_PATH, "maven/group/static" );
server.expect( path, 200, readResource( "repo-service/group-static.json" ) );
key = StoreKey.fromString( "maven:group:static" );
Group group = client.module( IndyStoresClientModule.class ).load( key, Group.class );
assertNotNull( group );
assertThat( group.getKey(), equalTo( key ) );
assertThat( group.getType(), equalTo( StoreType.group ) );
}

static String readResource( final String resourcePath )
throws IOException
{
URL url = Thread.currentThread().getContextClassLoader().getResource( resourcePath );
if ( url != null )
{
return IOUtils.toString( url.openStream(), Charset.defaultCharset() );
}
throw new IOException( String.format( "File not exists: %s", resourcePath ) );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.client.core;

import org.commonjava.indy.client.core.o11y.trace.ClientTracerConfiguration;
import org.commonjava.indy.model.core.io.IndyObjectMapper;
import org.commonjava.util.jhttpc.auth.MemoryPasswordManager;
import org.commonjava.util.jhttpc.model.SiteConfig;

import java.util.Collection;
import java.util.Collections;

public class IndyClientWithTraceTest
extends AbstractIndyClientTest
{

@Override
protected Indy getClient( SiteConfig siteConfig, Collection<IndyClientModule> modules )
{
try
{

final Indy.Builder builder = Indy.builder()
.setLocation( siteConfig )
.setObjectMapper( new IndyObjectMapper( Collections.emptySet() ) )
.setMdcCopyMappings( Collections.emptyMap() )
.setModules( modules.toArray( new IndyClientModule[0] ) );

ClientTracerConfiguration clientTracerConfig = new ClientTracerConfiguration();
clientTracerConfig.setEnabled( true );
clientTracerConfig.setConsoleTransport( true );
builder.setTraceConfiguration( clientTracerConfig );
builder.setPasswordManager( new MemoryPasswordManager() );

return builder.build();
}
catch ( IndyClientException e )
{
throw new RuntimeException( e );
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-client)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.client.core;

import org.commonjava.indy.model.core.io.IndyObjectMapper;
import org.commonjava.util.jhttpc.auth.MemoryPasswordManager;
import org.commonjava.util.jhttpc.model.SiteConfig;

import java.util.Collection;
import java.util.Collections;

public class IndyClientWithoutTraceTest
extends AbstractIndyClientTest
{
@Override
protected Indy getClient( SiteConfig siteConfig, Collection<IndyClientModule> modules )
{
try
{

final Indy.Builder builder = Indy.builder()
.setLocation( siteConfig )
.setObjectMapper( new IndyObjectMapper( Collections.emptySet() ) )
.setMdcCopyMappings( Collections.emptyMap() )
.setModules( modules.toArray( new IndyClientModule[0] ) );
builder.setPasswordManager( new MemoryPasswordManager() );
return builder.build();
}
catch ( IndyClientException e )
{
throw new RuntimeException( e );
}
}

}
17 changes: 17 additions & 0 deletions core-java/src/test/resources/repo-service/group-static.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type" : "group",
"key" : "maven:group:static",
"description" : "This group provides faster access to static data, that are stored in Indy.",
"metadata" : {
"changelog" : ""
},
"disabled" : false,
"constituents" : [ "maven:group:builds-untested+shared-imports" ],
"name" : "static",
"packageType" : "maven",
"disable_timeout" : -1,
"path_style" : "plain",
"authoritative_index" : false,
"create_time" : "2019-12-12 21:06:58 +0000",
"prepend_constituent" : false
}
15 changes: 15 additions & 0 deletions core-java/src/test/resources/repo-service/hosted-localdeploy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type" : "hosted",
"key" : "maven:hosted:local-deployments",
"disabled" : false,
"snapshotTimeoutSeconds" : 86400,
"readonly" : false,
"name" : "local-deployments",
"packageType" : "maven",
"disable_timeout" : 0,
"path_style" : "plain",
"authoritative_index" : false,
"create_time" : "2022-06-29 02:08:33 +0000",
"allow_snapshots" : true,
"allow_releases" : true
}
27 changes: 27 additions & 0 deletions core-java/src/test/resources/repo-service/remote-central.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type" : "remote",
"key" : "maven:remote:central",
"disabled" : false,
"host" : "repo.maven.apache.org",
"port" : 443,
"name" : "central",
"packageType" : "maven",
"disable_timeout" : 0,
"path_style" : "plain",
"authoritative_index" : false,
"create_time" : "2022-06-29 02:08:33 +0000",
"allow_snapshots" : false,
"allow_releases" : true,
"url" : "https://repo.maven.apache.org/maven2/",
"timeout_seconds" : 0,
"max_connections" : 30,
"ignore_hostname_verification" : false,
"nfc_timeout_seconds" : 0,
"is_passthrough" : false,
"cache_timeout_seconds" : 86400,
"metadata_timeout_seconds" : 0,
"proxy_port" : 0,
"prefetch_priority" : 0,
"prefetch_rescan" : false,
"prefetch_listing_type" : "html"
}

0 comments on commit e9ca4ac

Please sign in to comment.