Skip to content

Commit

Permalink
Add some unit tests (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
newtork authored Nov 15, 2023
1 parent b0526d1 commit 8806a4f
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2023 SAP SE or an SAP affiliate company. All rights reserved.
*/

package com.sap.cloud.sdk.cloudplatform.connectivity;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

import java.time.Duration;

import org.junit.After;
import org.junit.Test;

import com.github.benmanes.caffeine.cache.Cache;
import com.sap.cloud.sdk.cloudplatform.cache.CacheKey;
import com.sap.cloud.sdk.cloudplatform.resilience.CacheExpirationStrategy;

import io.vavr.control.Option;

public class DestinationServiceCacheTest
{
@After
public void resetCache()
{
DestinationService.Cache.reset();
}

@Test
public void testDisable()
{
assertThat(DestinationService.Cache.isEnabled()).isTrue();

// sut
DestinationService.Cache.disable();
assertThat(DestinationService.Cache.isEnabled()).isFalse();
assertThatCode(DestinationService.Cache::enableChangeDetection).isInstanceOf(IllegalStateException.class);
assertThatCode(DestinationService.Cache::instanceSingle).isInstanceOf(IllegalStateException.class);
assertThatCode(DestinationService.Cache::instanceAll).isInstanceOf(IllegalStateException.class);
}

@Test
public void testReset()
{
final Cache<CacheKey, ?> cacheSingle = DestinationService.Cache.instanceSingle();
final Cache<CacheKey, ?> cacheAll = DestinationService.Cache.instanceAll();

// sut
DestinationService.Cache.reset();
assertThat(cacheSingle).isNotSameAs(DestinationService.Cache.instanceSingle());
assertThat(cacheAll).isNotSameAs(DestinationService.Cache.instanceAll());
}

@Test
public void testChangeDetection()
{
final Cache<CacheKey, ?> cacheSingle = DestinationService.Cache.instanceSingle();
final Cache<CacheKey, ?> cacheAll = DestinationService.Cache.instanceAll();
assertThat(DestinationService.Cache.isChangeDetectionEnabled()).isFalse();

// sut
DestinationService.Cache.enableChangeDetection();
assertThat(DestinationService.Cache.isChangeDetectionEnabled()).isTrue();
assertThat(cacheSingle).isNotSameAs(DestinationService.Cache.instanceSingle());
assertThat(cacheAll).isNotSameAs(DestinationService.Cache.instanceAll());
}

@Test
public void testExpiration()
{
final Option<Duration> oldDuration = DestinationService.Cache.getExpirationDuration();
final Duration newDuration = Duration.ofSeconds(1);
Cache<CacheKey, ?> cacheSingle = DestinationService.Cache.instanceSingle();
Cache<CacheKey, ?> cacheAll = DestinationService.Cache.instanceAll();

// sut
DestinationService.Cache.setExpiration(newDuration, CacheExpirationStrategy.WHEN_CREATED);
assertThat(DestinationService.Cache.getExpirationDuration()).isNotEqualTo(oldDuration);
assertThat(DestinationService.Cache.getExpirationDuration()).containsExactly(newDuration);
assertThat(cacheSingle).isNotSameAs(cacheSingle = DestinationService.Cache.instanceSingle());
assertThat(cacheAll).isNotSameAs(cacheAll = DestinationService.Cache.instanceAll());

// sut
DestinationService.Cache.disableExpiration();
assertThat(DestinationService.Cache.getExpirationDuration()).isEmpty();
assertThat(cacheSingle).isNotSameAs(DestinationService.Cache.instanceSingle());
assertThat(cacheAll).isNotSameAs(DestinationService.Cache.instanceAll());
}

@Test
public void testSize()
{
Cache<CacheKey, ?> cacheSingle = DestinationService.Cache.instanceSingle();
final Cache<CacheKey, ?> cacheAll = DestinationService.Cache.instanceAll();

// sut
DestinationService.Cache.setSizeLimit(100);
assertThat(cacheSingle).isNotSameAs(cacheSingle = DestinationService.Cache.instanceSingle());
assertThat(cacheAll).isSameAs(DestinationService.Cache.instanceAll());

// sut
DestinationService.Cache.disableSizeLimit();
assertThat(cacheSingle).isNotSameAs(DestinationService.Cache.instanceSingle());
assertThat(cacheAll).isSameAs(DestinationService.Cache.instanceAll());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import com.sap.cloud.environment.servicebinding.api.ServiceIdentifier;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
import com.sap.cloud.security.config.ClientCertificate;
import com.sap.cloud.security.config.CredentialType;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -107,6 +108,45 @@ public void testClientSecretIsTheDefault()
assertThat(sut.getCredentialType()).isEqualTo(CredentialType.BINDING_SECRET);
}

@Test
public void testCredentialTypeInstanceSecret()
{
final ServiceBinding binding =
new ServiceBindingBuilder(ServiceIdentifier.of("testInstanceSecret"))
.with("credentials.uaa.credential-type", "instance-secret")
.build();
final ServiceBindingDestinationOptions options = ServiceBindingDestinationOptions.forService(binding).build();

sut = new DefaultOAuth2PropertySupplier(options);

assertThat(sut.getCredentialType()).isEqualTo(CredentialType.INSTANCE_SECRET);
assertThatCode(sut::getClientIdentity)
.isInstanceOf(DestinationAccessException.class)
.hasMessage("Failed to resolve property [uaa][clientid] from service binding.");
}

@Test
public void testCredentialTypeX509()
{
final ServiceBinding binding =
new ServiceBindingBuilder(ServiceIdentifier.of("testX509"))
.with("credentials.uaa.credential-type", "x509")
.with("credentials.uaa.clientid", "id")
.with("credentials.uaa.certificate", "cert")
.with("credentials.uaa.key", "key")
.build();
final ServiceBindingDestinationOptions options = ServiceBindingDestinationOptions.forService(binding).build();

sut = new DefaultOAuth2PropertySupplier(options);

assertThat(sut.getCredentialType()).isEqualTo(CredentialType.X509);
assertThat(sut.getClientIdentity()).isInstanceOfSatisfying(ClientCertificate.class, cc -> {
assertThat(cc.getId()).isEqualTo("id");
assertThat(cc.getCertificate()).isEqualTo("cert");
assertThat(cc.getKey()).isEqualTo("key");
});
}

@RequiredArgsConstructor
private static final class ServiceBindingBuilder
{
Expand Down

0 comments on commit 8806a4f

Please sign in to comment.