Skip to content

Commit 41f3d6a

Browse files
committed
fix up tests
1 parent c8370e7 commit 41f3d6a

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

sdk/src/main/java/io/opentdf/platform/sdk/KASKeyCache.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.time.temporal.ChronoUnit;
88
import java.util.HashMap;
99
import java.util.Map;
10+
import java.util.Objects;
1011

1112
/**
1213
* Class representing a cache for KAS (Key Access Server) information.
@@ -31,7 +32,7 @@ public Config.KASInfo get(String url, String algorithm, String kid) {
3132
TimeStampedKASInfo cachedValue = cache.get(cacheKey);
3233

3334
if (cachedValue == null) {
34-
log.debug("didn't find kasinfo for url = [{}], algorithm = [{}]", url, algorithm);
35+
log.debug("didn't find kasinfo for key= [{}]", cacheKey);
3536
return null;
3637
}
3738

@@ -93,24 +94,26 @@ public KASKeyRequest(String url, String algorithm, String kid) {
9394
this.kid = kid;
9495
}
9596

96-
// Override equals and hashCode to ensure proper functioning of the HashMap
9797
@Override
9898
public boolean equals(Object o) {
99-
if (this == o) return true;
100-
if (o == null || !(o instanceof KASKeyRequest)) return false;
99+
if (o == null || getClass() != o.getClass()) return false;
101100
KASKeyRequest that = (KASKeyRequest) o;
102-
if (algorithm == null){
103-
return url.equals(that.url);
104-
}
105-
return url.equals(that.url) && algorithm.equals(that.algorithm);
101+
return Objects.equals(url, that.url) && Objects.equals(algorithm, that.algorithm) && Objects.equals(kid, that.kid);
106102
}
107103

108104
@Override
109105
public int hashCode() {
110-
int result = 31 * url.hashCode();
111-
if (algorithm != null) {
112-
result = result + algorithm.hashCode();
113-
}
114-
return result;
106+
return Objects.hash(url, algorithm, kid);
107+
}
108+
109+
@Override
110+
public String toString() {
111+
return "KASKeyRequest{" +
112+
"url='" + url + '\'' +
113+
", algorithm='" + algorithm + '\'' +
114+
", kid='" + kid + '\'' +
115+
'}';
115116
}
117+
118+
// Override equals and hashCode to ensure proper functioning of the HashMap
116119
}

sdk/src/test/java/io/opentdf/platform/sdk/KASKeyCacheTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void testStoreAndGet_AfterTimeLimit() {
5151
kasKeyCache.store(kasInfo1);
5252

5353
// Simulate time passing by modifying the timestamp directly
54-
KASKeyRequest cacheKey = new KASKeyRequest("https://example.com/kas1", "rsa:2048", "kid2");
54+
KASKeyRequest cacheKey = new KASKeyRequest("https://example.com/kas1", "rsa:2048", "kid1");
5555
TimeStampedKASInfo timeStampedKASInfo = new TimeStampedKASInfo(kasInfo1, LocalDateTime.now().minus(6, ChronoUnit.MINUTES));
5656
kasKeyCache.cache.put(cacheKey, timeStampedKASInfo);
5757

@@ -62,6 +62,18 @@ void testStoreAndGet_AfterTimeLimit() {
6262
assertNull(result);
6363
}
6464

65+
@Test
66+
void testStoreAndGet_DifferentKIDs() {
67+
// Store an item in the cache
68+
kasKeyCache.store(kasInfo1);
69+
70+
// Attempt to retrieve the item with a different KID
71+
Config.KASInfo result = kasKeyCache.get(kasInfo1.URL, kasInfo1.Algorithm, kasInfo1.KID + "different");
72+
73+
// Ensure the item was not retrieved (it should have expired)
74+
assertNull(result);
75+
}
76+
6577
@Test
6678
void testStoreAndGet_WithNullAlgorithm() {
6779
// Store an item in the cache with a null algorithm
@@ -72,7 +84,7 @@ void testStoreAndGet_WithNullAlgorithm() {
7284
kasKeyCache.store(kasInfo1);
7385

7486
// Retrieve the item with a null algorithm
75-
Config.KASInfo result = kasKeyCache.get("https://example.com/kas1", null, null);
87+
Config.KASInfo result = kasKeyCache.get("https://example.com/kas1", null, "kid1");
7688

7789
// Ensure the item was correctly retrieved
7890
assertNotNull(result);

0 commit comments

Comments
 (0)