|
| 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 | +} |
0 commit comments