SimpleApiRepositoryAdapter implements ApiRepositoryAdapter { @Override public AbstractApiRepository adapt(final Repository repository) { boolean online = repository.getConfiguration().isOnline(); String name = repository.getName(); String format = repository.getFormat().toString(); String url = repository.getUrl(); switch (repository.getType().toString()) { case GroupType.NAME: return new SimpleApiGroupRepository(name, format, url, online, getStorageAttributes(repository), getGroupAttributes(repository)); case HostedType.NAME: return new SimpleApiHostedRepository(name, format, url, online, getHostedStorageAttributes(repository), getCleanupPolicyAttributes(repository)); case ProxyType.NAME: return new SimpleApiProxyRepository(name, format, url, online, getStorageAttributes(repository), getCleanupPolicyAttributes(repository), getProxyAttributes(repository), getNegativeCacheAttributes(repository), getHttpClientAttributes(repository), getRoutingRuleName(repository)); default: return null; } } @Inject  SimpleApiRepositoryAdapter(final RoutingRuleStore routingRuleStore); @Override AbstractApiRepository adapt(final Repository repository);  }
@Test public void testAdapt_groupRepository() throws Exception { Repository repository = createRepository(new GroupType()); repository.getConfiguration().attributes("group").set("memberNames", Arrays.asList("a", "b")); SimpleApiGroupRepository groupRepository = (SimpleApiGroupRepository) underTest.adapt(repository); assertRepository(groupRepository, "group", true); assertThat(groupRepository.getGroup().getMemberNames(), contains("a", "b")); assertThat(groupRepository.getStorage().getStrictContentTypeValidation(), is(true)); setStorageAttributes(repository, "default", false, null); groupRepository = (SimpleApiGroupRepository) underTest.adapt(repository); assertThat(groupRepository.getStorage().getBlobStoreName(), is("default")); assertThat(groupRepository.getStorage().getStrictContentTypeValidation(), is(false)); }
@Test public void testAdapt_hostedRepository() throws Exception { Repository repository = createRepository(new HostedType()); SimpleApiHostedRepository hostedRepository = (SimpleApiHostedRepository) underTest.adapt(repository); assertRepository(hostedRepository, "hosted", true); assertThat(hostedRepository.getStorage().getStrictContentTypeValidation(), is(true)); assertThat(hostedRepository.getStorage().getWritePolicy(), is("ALLOW")); setStorageAttributes(repository, "default", false, "DENY"); hostedRepository = (SimpleApiHostedRepository) underTest.adapt(repository); assertThat(hostedRepository.getStorage().getBlobStoreName(), is("default")); assertThat(hostedRepository.getStorage().getStrictContentTypeValidation(), is(false)); assertThat(hostedRepository.getStorage().getWritePolicy(), is("DENY")); }
@Test public void testAdapt_proxyRepository() throws Exception { Repository repository = createRepository(new ProxyType()); NestedAttributesMap proxy = repository.getConfiguration().attributes("proxy"); proxy.set("remoteUrl", "https: SimpleApiProxyRepository proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertRepository(proxyRepository, "proxy", true); assertThat(proxyRepository.getProxy().getContentMaxAge(), is(1440)); assertThat(proxyRepository.getProxy().getMetadataMaxAge(), is(1440)); assertThat(proxyRepository.getProxy().getRemoteUrl(), is("https: assertThat(proxyRepository.getHttpClient().getAutoBlock(), is(false)); assertThat(proxyRepository.getHttpClient().getBlocked(), is(false)); proxy.set("contentMaxAge", 1000.0); proxy.set("metadataMaxAge", 1000.0); NestedAttributesMap httpclient = repository.getConfiguration().attributes("httpclient"); httpclient.set("autoBlock", true); httpclient.set("blocked", true); proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getProxy().getContentMaxAge(), is(1000)); assertThat(proxyRepository.getProxy().getMetadataMaxAge(), is(1000)); assertThat(proxyRepository.getHttpClient().getAutoBlock(), is(true)); assertThat(proxyRepository.getHttpClient().getBlocked(), is(true)); }
@Test public void testAdapt_proxyRepository_negativeCache() throws Exception { Repository repository = createRepository(new ProxyType()); SimpleApiProxyRepository proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getNegativeCache().getEnabled(), is(true)); assertThat(proxyRepository.getNegativeCache().getTimeToLive(), is(Time.hours(24).toMinutesI())); NestedAttributesMap negativeCache = repository.getConfiguration().attributes("negativeCache"); negativeCache.set("enabled", false); negativeCache.set("timeToLive", 23.0); proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getNegativeCache().getEnabled(), is(false)); assertThat(proxyRepository.getNegativeCache().getTimeToLive(), is(23)); }
@Test public void testAdapt_proxyRepositoryStorageAttributes() throws Exception { Repository repository = createRepository(new ProxyType()); setStorageAttributes(repository, "default", false, null); SimpleApiProxyRepository proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getStorage().getBlobStoreName(), is("default")); assertThat(proxyRepository.getStorage().getStrictContentTypeValidation(), is(false)); }
@Test public void testAdapt_proxyRepositoryAuth() throws Exception { Repository repository = createRepository(new ProxyType()); SimpleApiProxyRepository proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getHttpClient().getAuthentication(), nullValue()); NestedAttributesMap httpclient = repository.getConfiguration().attributes("httpclient").child("authentication"); httpclient.set("type", "username"); httpclient.set("username", "jsmith"); httpclient.set("password", "p4ssw0rd"); proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getHttpClient().getAuthentication().getType(), is("username")); assertThat(proxyRepository.getHttpClient().getAuthentication().getUsername(), is("jsmith")); assertThat(proxyRepository.getHttpClient().getAuthentication().getPassword(), nullValue()); assertThat(proxyRepository.getHttpClient().getAuthentication().getNtlmDomain(), nullValue()); assertThat(proxyRepository.getHttpClient().getAuthentication().getNtlmHost(), nullValue()); httpclient.set("type", "ntlm"); httpclient.set("username", "jsmith"); httpclient.set("password", "p4ssw0rd"); httpclient.set("ntlmDomain", "sona"); httpclient.set("ntlmHost", "sonatype.com"); proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getHttpClient().getAuthentication().getType(), is("ntlm")); assertThat(proxyRepository.getHttpClient().getAuthentication().getUsername(), is("jsmith")); assertThat(proxyRepository.getHttpClient().getAuthentication().getPassword(), nullValue()); assertThat(proxyRepository.getHttpClient().getAuthentication().getNtlmDomain(), is("sona")); assertThat(proxyRepository.getHttpClient().getAuthentication().getNtlmHost(), is("sonatype.com")); }
@Test public void testAdapt_proxyRepositoryConnection() throws Exception { Repository repository = createRepository(new ProxyType()); SimpleApiProxyRepository proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertThat(proxyRepository.getHttpClient().getConnection(), nullValue()); NestedAttributesMap connection = repository.getConfiguration().attributes("httpclient").child("connection"); proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertConnection(proxyRepository.getHttpClient().getConnection(), false, false, null, null, null); connection.set("enableCircularRedirects", true); connection.set("enableCookies", true); connection.set("retries", 9.0); connection.set("timeout", 7.0); connection.set("userAgentSuffix", "hi-yall"); proxyRepository = (SimpleApiProxyRepository) underTest.adapt(repository); assertConnection(proxyRepository.getHttpClient().getConnection(), true, true, 9, 7, "hi-yall"); }