|
16 | 16 |
|
17 | 17 | import org.eclipse.edc.iam.decentralizedclaims.spi.scope.DcpScope; |
18 | 18 | import org.eclipse.edc.iam.decentralizedclaims.spi.scope.store.DcpScopeStore; |
| 19 | +import org.eclipse.edc.spi.query.QuerySpec; |
19 | 20 | import org.eclipse.edc.spi.result.StoreResult; |
20 | 21 | import org.eclipse.edc.transaction.spi.NoopTransactionContext; |
21 | 22 | import org.eclipse.edc.transaction.spi.TransactionContext; |
|
26 | 27 | import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; |
27 | 28 | import static org.mockito.Mockito.any; |
28 | 29 | import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.never; |
29 | 31 | import static org.mockito.Mockito.verify; |
30 | 32 | import static org.mockito.Mockito.when; |
31 | 33 |
|
@@ -71,6 +73,69 @@ void register_should_return_failure_when_store_fails() { |
71 | 73 | verify(store).save(scope); |
72 | 74 | } |
73 | 75 |
|
| 76 | + @Test |
| 77 | + void create_should_save_when_scope_does_not_exist() { |
| 78 | + var scope = DcpScope.Builder.newInstance().id("s1").value("v").profile("p").build(); |
| 79 | + when(store.query(any())).thenReturn(StoreResult.success(List.of())); |
| 80 | + when(store.save(scope)).thenReturn(StoreResult.success()); |
| 81 | + |
| 82 | + var impl = new DcpScopeRegistryImpl(transactionContext, store); |
| 83 | + var res = impl.create(scope); |
| 84 | + |
| 85 | + assertThat(res).isSucceeded(); |
| 86 | + verify(store).save(scope); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void create_should_return_conflict_when_scope_already_exists() { |
| 91 | + var scope = DcpScope.Builder.newInstance().id("s1").value("v").profile("p").build(); |
| 92 | + when(store.query(any())).thenReturn(StoreResult.success(List.of(scope))); |
| 93 | + |
| 94 | + var impl = new DcpScopeRegistryImpl(transactionContext, store); |
| 95 | + var res = impl.create(scope); |
| 96 | + |
| 97 | + assertThat(res).isFailed().detail().contains("already exists"); |
| 98 | + verify(store, never()).save(any()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void update_should_save_when_scope_exists() { |
| 103 | + var scope = DcpScope.Builder.newInstance().id("s1").value("v").profile("p").build(); |
| 104 | + when(store.query(any())).thenReturn(StoreResult.success(List.of(scope))); |
| 105 | + when(store.save(scope)).thenReturn(StoreResult.success()); |
| 106 | + |
| 107 | + var impl = new DcpScopeRegistryImpl(transactionContext, store); |
| 108 | + var res = impl.update(scope); |
| 109 | + |
| 110 | + assertThat(res).isSucceeded(); |
| 111 | + verify(store).save(scope); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void update_should_return_not_found_when_scope_does_not_exist() { |
| 116 | + var scope = DcpScope.Builder.newInstance().id("s1").value("v").profile("p").build(); |
| 117 | + when(store.query(any())).thenReturn(StoreResult.success(List.of())); |
| 118 | + |
| 119 | + var impl = new DcpScopeRegistryImpl(transactionContext, store); |
| 120 | + var res = impl.update(scope); |
| 121 | + |
| 122 | + assertThat(res).isFailed().detail().contains("does not exist"); |
| 123 | + verify(store, never()).save(any()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void query_should_return_list_from_store() { |
| 128 | + var s1 = DcpScope.Builder.newInstance().id("s1").value("v1").profile("p").build(); |
| 129 | + var expected = List.of(s1); |
| 130 | + when(store.query(any())).thenReturn(StoreResult.success(expected)); |
| 131 | + |
| 132 | + var impl = new DcpScopeRegistryImpl(transactionContext, store); |
| 133 | + var res = impl.query(QuerySpec.max()); |
| 134 | + |
| 135 | + assertThat(res).isSucceeded().isEqualTo(expected); |
| 136 | + verify(store).query(QuerySpec.max()); |
| 137 | + } |
| 138 | + |
74 | 139 | @Test |
75 | 140 | void remove_should_delegate_to_store_and_return_success() { |
76 | 141 | when(store.delete("id")).thenReturn(StoreResult.success()); |
|
0 commit comments