5
5
package org .opensmartgridplatform .adapter .protocol .dlms .application .services ;
6
6
7
7
import static org .assertj .core .api .Assertions .assertThat ;
8
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
8
9
import static org .mockito .ArgumentMatchers .any ;
9
10
import static org .mockito .ArgumentMatchers .same ;
10
11
import static org .mockito .Mockito .times ;
11
12
import static org .mockito .Mockito .verify ;
12
13
import static org .mockito .Mockito .when ;
13
14
14
- import java .util .Arrays ;
15
- import java .util .HashMap ;
15
+ import java .util .EnumMap ;
16
16
import java .util .List ;
17
17
import java .util .Map ;
18
18
import org .apache .commons .codec .binary .Hex ;
24
24
import org .mockito .junit .jupiter .MockitoExtension ;
25
25
import org .opensmartgridplatform .adapter .protocol .dlms .application .wsclient .SecretManagementClient ;
26
26
import org .opensmartgridplatform .adapter .protocol .dlms .domain .entities .SecurityKeyType ;
27
- import org .opensmartgridplatform .adapter .protocol .dlms .exceptions .ProtocolAdapterException ;
27
+ import org .opensmartgridplatform .adapter .protocol .dlms .exceptions .StoreNewKeyException ;
28
28
import org .opensmartgridplatform .shared .infra .jms .MessageMetadata ;
29
29
import org .opensmartgridplatform .shared .security .RsaEncrypter ;
30
30
import org .opensmartgridplatform .ws .schema .core .secret .management .ActivateSecretsRequest ;
41
41
import org .opensmartgridplatform .ws .schema .core .secret .management .TypedSecrets ;
42
42
43
43
@ ExtendWith (MockitoExtension .class )
44
- public class SecretManagementServiceTest {
44
+ class SecretManagementServiceTest {
45
45
private static final String DEVICE_IDENTIFICATION = "E000123456789" ;
46
46
private static final SecurityKeyType KEY_TYPE = SecurityKeyType .E_METER_ENCRYPTION ;
47
47
private static final byte [] UNENCRYPTED_SECRET = {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
@@ -72,9 +72,8 @@ public void init() {
72
72
}
73
73
74
74
@ Test
75
- public void testGetKeys () {
76
- // SETUP
77
- final List <SecurityKeyType > keyTypes = Arrays .asList (KEY_TYPE );
75
+ void testGetKeys () {
76
+ final List <SecurityKeyType > keyTypes = List .of (KEY_TYPE );
78
77
final GetSecretsResponse response = new GetSecretsResponse ();
79
78
response .setResult (OsgpResultType .OK );
80
79
response .setTypedSecrets (new TypedSecrets ());
@@ -83,35 +82,51 @@ public void testGetKeys() {
83
82
same (messageMetadata ), any (GetSecretsRequest .class )))
84
83
.thenReturn (response );
85
84
when (this .decrypterForProtocolAdapterDlms .decrypt (SOAP_SECRET )).thenReturn (UNENCRYPTED_SECRET );
86
- // EXECUTE
85
+
87
86
final Map <SecurityKeyType , byte []> result =
88
87
this .secretManagementTestService .getKeys (messageMetadata , DEVICE_IDENTIFICATION , keyTypes );
89
- // ASSERT
90
- assertThat (result ).isNotNull ();
91
- assertThat (result .size ()).isEqualTo (1 );
88
+
89
+ assertThat (result ).isNotNull ().hasSize (1 );
92
90
assertThat (result .keySet ().iterator ().next ()).isEqualTo (KEY_TYPE );
93
91
assertThat (result .values ().iterator ().next ()).isEqualTo (UNENCRYPTED_SECRET );
94
92
}
95
93
96
94
@ Test
97
- public void testStoreNewKeys () {
98
- final Map <SecurityKeyType , byte []> keys = new HashMap <>();
95
+ void testStoreNewKeys () {
96
+ final Map <SecurityKeyType , byte []> keys = new EnumMap <>(SecurityKeyType . class );
99
97
keys .put (KEY_TYPE , UNENCRYPTED_SECRET );
100
98
final StoreSecretsResponse response = new StoreSecretsResponse ();
101
99
response .setResult (OsgpResultType .OK );
102
100
when (this .encrypterForSecretManagement .encrypt (UNENCRYPTED_SECRET )).thenReturn (SOAP_SECRET );
103
101
when (this .secretManagementClient .storeSecretsRequest (same (messageMetadata ), any ()))
104
102
.thenReturn (response );
105
- // EXECUTE
103
+
106
104
this .secretManagementTestService .storeNewKeys (messageMetadata , DEVICE_IDENTIFICATION , keys );
107
- // ASSERT
105
+
108
106
verify (this .secretManagementClient , times (1 ))
109
107
.storeSecretsRequest (same (messageMetadata ), any (StoreSecretsRequest .class ));
110
108
}
111
109
112
110
@ Test
113
- public void testActivateKeys () throws ProtocolAdapterException {
114
- final List <SecurityKeyType > keyTypes = Arrays .asList (KEY_TYPE );
111
+ void testStoreNewKeysThrowsException () {
112
+ final Map <SecurityKeyType , byte []> keys = new EnumMap <>(SecurityKeyType .class );
113
+ keys .put (KEY_TYPE , UNENCRYPTED_SECRET );
114
+
115
+ when (this .encrypterForSecretManagement .encrypt (UNENCRYPTED_SECRET )).thenReturn (SOAP_SECRET );
116
+ when (this .secretManagementClient .storeSecretsRequest (same (messageMetadata ), any ()))
117
+ .thenThrow (new RuntimeException ("Simulated exception" ));
118
+
119
+ assertThatThrownBy (
120
+ () -> {
121
+ this .secretManagementTestService .storeNewKeys (
122
+ messageMetadata , DEVICE_IDENTIFICATION , keys );
123
+ })
124
+ .isInstanceOf (StoreNewKeyException .class );
125
+ }
126
+
127
+ @ Test
128
+ void testActivateKeys () {
129
+ final List <SecurityKeyType > keyTypes = List .of (KEY_TYPE );
115
130
final ArgumentCaptor <ActivateSecretsRequest > activateSecretsCaptor =
116
131
ArgumentCaptor .forClass (ActivateSecretsRequest .class );
117
132
// EXECUTE
@@ -127,25 +142,25 @@ public void testActivateKeys() throws ProtocolAdapterException {
127
142
}
128
143
129
144
@ Test
130
- public void testGenerateAndStoreKeys () {
131
- final List <SecurityKeyType > keyTypes = Arrays . asList (KEY_TYPE );
145
+ void testGenerateAndStoreKeys () {
146
+ final List <SecurityKeyType > keyTypes = List . of (KEY_TYPE );
132
147
final GenerateAndStoreSecretsResponse response = new GenerateAndStoreSecretsResponse ();
133
148
response .setResult (OsgpResultType .OK );
134
149
response .setTypedSecrets (new TypedSecrets ());
135
150
response .getTypedSecrets ().getTypedSecret ().add (TYPED_SECRET );
136
151
when (this .secretManagementClient .generateAndStoreSecrets (same (messageMetadata ), any ()))
137
152
.thenReturn (response );
138
153
when (this .decrypterForProtocolAdapterDlms .decrypt (SOAP_SECRET )).thenReturn (UNENCRYPTED_SECRET );
139
- // EXECUTE
154
+
140
155
final Map <SecurityKeyType , byte []> keys =
141
156
this .secretManagementTestService .generate128BitsKeysAndStoreAsNewKeys (
142
157
messageMetadata , DEVICE_IDENTIFICATION , keyTypes );
143
- // ASSERT
144
- assertThat (keys . get ( KEY_TYPE )). isEqualTo ( UNENCRYPTED_SECRET );
158
+
159
+ assertThat (keys ). containsEntry ( KEY_TYPE , UNENCRYPTED_SECRET );
145
160
}
146
161
147
162
@ Test
148
- public void testHasNewSecretAuthenticationKey () {
163
+ void testHasNewSecretAuthenticationKey () {
149
164
final HasNewSecretResponse responseTrue = new HasNewSecretResponse ();
150
165
responseTrue .setHasNewSecret (true );
151
166
final HasNewSecretResponse responseFalse = new HasNewSecretResponse ();
@@ -163,15 +178,14 @@ public void testHasNewSecretAuthenticationKey() {
163
178
}
164
179
});
165
180
166
- // EXECUTE
167
181
final boolean result =
168
182
this .secretManagementTestService .hasNewSecret (messageMetadata , DEVICE_IDENTIFICATION );
169
- // ASSERT
183
+
170
184
assertThat (result ).isTrue ();
171
185
}
172
186
173
187
@ Test
174
- public void testHasNewSecretEncryptionKey () {
188
+ void testHasNewSecretEncryptionKey () {
175
189
final HasNewSecretResponse responseTrue = new HasNewSecretResponse ();
176
190
responseTrue .setHasNewSecret (true );
177
191
final HasNewSecretResponse responseFalse = new HasNewSecretResponse ();
@@ -189,10 +203,9 @@ public void testHasNewSecretEncryptionKey() {
189
203
}
190
204
});
191
205
192
- // EXECUTE
193
206
final boolean result =
194
207
this .secretManagementTestService .hasNewSecret (messageMetadata , DEVICE_IDENTIFICATION );
195
- // ASSERT
208
+
196
209
assertThat (result ).isTrue ();
197
210
}
198
211
}
0 commit comments