16
16
import org .zowe .apiml .caching .model .KeyValue ;
17
17
import org .zowe .apiml .caching .service .StorageException ;
18
18
19
- import java .util .HashMap ;
20
- import java .util .Map ;
19
+ import java .util .concurrent . ConcurrentHashMap ;
20
+ import java .util .concurrent . ConcurrentMap ;
21
21
22
22
import static org .junit .jupiter .api .Assertions .*;
23
23
import static org .mockito .Mockito .*;
@@ -26,7 +26,7 @@ class InfinispanStorageTest {
26
26
27
27
public static final KeyValue TO_CREATE = new KeyValue ("key1" , "val1" );
28
28
public static final KeyValue TO_UPDATE = new KeyValue ("key1" , "val2" );
29
- Cache <String , Map < String , KeyValue > > cache ;
29
+ Cache <String , KeyValue > cache ;
30
30
InfinispanStorage storage ;
31
31
String serviceId1 = "service1" ;
32
32
@@ -39,86 +39,101 @@ void setup() {
39
39
@ Nested
40
40
class WhenEntryDoesntExist {
41
41
42
- Map < String , KeyValue > serviceStore ;
42
+ KeyValue keyValue ;
43
43
44
44
@ BeforeEach
45
45
void createEmptyStore () {
46
- serviceStore = new HashMap <>();
47
- }
48
-
49
- @ Test
50
- void whenCreate_thenCacheIsUpdated () {
51
- when (cache .computeIfAbsent (any (), any ())).thenReturn (serviceStore );
52
- storage .create (serviceId1 , TO_CREATE );
53
- verify (cache , times (1 )).put (serviceId1 , serviceStore );
46
+ keyValue = null ;
54
47
}
55
48
56
49
@ Test
57
50
void whenRead_thenExceptionIsThrown () {
58
51
String key = TO_CREATE .getKey ();
59
- when (cache .get (serviceId1 )).thenReturn (serviceStore );
52
+ when (cache .get (serviceId1 )).thenReturn (keyValue );
60
53
assertThrows (StorageException .class , () -> storage .read (serviceId1 , key ));
61
54
}
62
55
63
56
@ Test
64
- void whenDelete_thenExceptionIsThrown () {
57
+ void whenUpdate_thenExceptionIsThrown () {
58
+ KeyValue entry = new KeyValue ("key" , "value" );
59
+ when (cache .get (serviceId1 )).thenReturn (keyValue );
60
+ assertThrows (StorageException .class , () -> storage .update (serviceId1 , entry ));
61
+ }
65
62
66
- String key = TO_CREATE . getKey ();
67
- when ( cache . get ( serviceId1 )). thenReturn ( serviceStore );
68
- assertThrows ( StorageException . class , () -> storage . delete ( serviceId1 , key ) );
69
- verify ( cache , times ( 0 )). put (serviceId1 , serviceStore );
63
+ @ Test
64
+ void whenAddNew_returnNull () {
65
+ keyValue = new KeyValue ( "key" , "value" );
66
+ assertNull ( storage . create (serviceId1 , keyValue ) );
70
67
}
71
68
72
69
@ Test
73
- void whenUpdate_thenCacheIsUpdated () {
70
+ void whenDelete_thenExceptionIsThrown () {
74
71
75
- when ( cache . get ( serviceId1 )). thenReturn ( serviceStore );
76
- assertThrows ( StorageException . class , () -> storage . update ( serviceId1 , TO_UPDATE ) );
77
- verify ( cache , times ( 0 )). put (serviceId1 , serviceStore );
72
+ String key = TO_CREATE . getKey ( );
73
+ when ( cache . remove ( serviceId1 + key )). thenReturn ( null );
74
+ assertThrows ( StorageException . class , () -> storage . delete (serviceId1 , key ) );
78
75
}
76
+
79
77
}
80
78
81
79
82
80
@ Nested
83
81
class WhenEntryExists {
84
- Map < String , KeyValue > serviceStore ;
82
+ KeyValue keyValue ;
85
83
86
84
@ BeforeEach
87
85
void createStoreWithEntry () {
88
- serviceStore = new HashMap <>();
89
- serviceStore .put (TO_CREATE .getKey (), TO_CREATE );
86
+ keyValue = TO_CREATE ;
90
87
}
91
88
92
89
@ Test
93
- void whenCreate_thenExceptionIsThrown () {
94
- when (cache .computeIfAbsent (any (), any ())).thenReturn (serviceStore );
90
+ void exceptionIsThrown () {
91
+ when (cache .putIfAbsent (any (), any ())).thenReturn (keyValue );
95
92
assertThrows (StorageException .class , () -> storage .create (serviceId1 , TO_CREATE ));
96
93
}
97
94
98
95
@ Test
99
- void whenRead_thenEntryIsReturned () {
100
- when (cache .get (serviceId1 )) .thenReturn (serviceStore );
96
+ void entryIsReturned () {
97
+ when (cache .get (serviceId1 + TO_CREATE . getKey ())) .thenReturn (TO_CREATE );
101
98
KeyValue result = storage .read (serviceId1 , TO_CREATE .getKey ());
102
99
assertEquals (TO_CREATE .getValue (), result .getValue ());
103
100
}
104
101
105
102
@ Test
106
- void whenUpdate_thenCacheIsUpdated () {
103
+ void cacheIsUpdated () {
107
104
108
- when (cache .get (serviceId1 )) .thenReturn (serviceStore );
105
+ when (cache .put (serviceId1 + TO_UPDATE . getKey (), TO_UPDATE )) .thenReturn (TO_UPDATE );
109
106
storage .update (serviceId1 , TO_UPDATE );
110
- verify (cache , times (1 )).put (serviceId1 , serviceStore );
111
- assertEquals ("val2" , serviceStore . get ( TO_CREATE . getKey ()) .getValue ());
107
+ verify (cache , times (1 )).put (serviceId1 + TO_UPDATE . getKey (), TO_UPDATE );
108
+ assertEquals ("val2" , TO_UPDATE .getValue ());
112
109
}
113
110
114
111
@ Test
115
- void whenDelete_thenCacheIsUpdated () {
112
+ void itemIsDeleted () {
113
+ ConcurrentMap <String , KeyValue > cache = new ConcurrentHashMap <>();
114
+ InfinispanStorage storage = new InfinispanStorage (cache );
115
+ assertNull (storage .create (serviceId1 , TO_CREATE ));
116
+ assertEquals (TO_CREATE , storage .delete (serviceId1 , TO_CREATE .getKey ()));
117
+ }
116
118
117
- when (cache .get (serviceId1 )).thenReturn (serviceStore );
118
- KeyValue result = storage .delete (serviceId1 , TO_CREATE .getKey ());
119
- verify (cache , times (1 )).put (serviceId1 , serviceStore );
120
- assertEquals (TO_CREATE .getValue (), result .getValue ());
121
- assertNull (serviceStore .get (TO_CREATE .getKey ()));
119
+ @ Test
120
+ void returnAll () {
121
+ ConcurrentMap <String , KeyValue > cache = new ConcurrentHashMap <>();
122
+ InfinispanStorage storage = new InfinispanStorage (cache );
123
+ storage .create (serviceId1 , new KeyValue ("key" , "value" ));
124
+ storage .create (serviceId1 , new KeyValue ("key2" , "value2" ));
125
+ assertEquals (2 , storage .readForService (serviceId1 ).size ());
126
+ }
127
+
128
+ @ Test
129
+ void removeAll () {
130
+ ConcurrentMap <String , KeyValue > cache = new ConcurrentHashMap <>();
131
+ InfinispanStorage storage = new InfinispanStorage (cache );
132
+ storage .create (serviceId1 , new KeyValue ("key" , "value" ));
133
+ storage .create (serviceId1 , new KeyValue ("key2" , "value2" ));
134
+ assertEquals (2 , storage .readForService (serviceId1 ).size ());
135
+ storage .deleteForService (serviceId1 );
136
+ assertEquals (0 , storage .readForService (serviceId1 ).size ());
122
137
}
123
138
124
139
}
0 commit comments