Skip to content

Commit cd572b3

Browse files
authored
Merge pull request #1515 from OSGP/bug/exception_storeNewKeys_SecretManagementService
Bug/exception store new keys secret management service
2 parents 827145a + 2f2cc7c commit cd572b3

File tree

3 files changed

+63
-29
lines changed

3 files changed

+63
-29
lines changed

osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/main/java/org/opensmartgridplatform/adapter/protocol/dlms/application/services/SecretManagementService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.commons.codec.binary.Hex;
1616
import org.opensmartgridplatform.adapter.protocol.dlms.application.wsclient.SecretManagementClient;
1717
import org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType;
18+
import org.opensmartgridplatform.adapter.protocol.dlms.exceptions.StoreNewKeyException;
1819
import org.opensmartgridplatform.shared.infra.jms.MessageMetadata;
1920
import org.opensmartgridplatform.shared.security.RsaEncrypter;
2021
import org.opensmartgridplatform.ws.schema.core.secret.management.ActivateSecretsRequest;
@@ -267,7 +268,7 @@ public void storeNewKeys(
267268
try {
268269
response = this.secretManagementClient.storeSecretsRequest(messageMetadata, request);
269270
} catch (final RuntimeException exc) {
270-
throw new IllegalStateException("Could not store keys: unexpected exception occured", exc);
271+
throw new StoreNewKeyException("Could not store keys: unexpected exception occured", exc);
271272
}
272273
if (response == null) {
273274
throw new IllegalStateException("Could not store keys: NULL response");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package org.opensmartgridplatform.adapter.protocol.dlms.exceptions;
6+
7+
import java.io.Serial;
8+
9+
public class StoreNewKeyException extends RuntimeException implements NonRetryableException {
10+
11+
@Serial private static final long serialVersionUID = -539165888074888682L;
12+
13+
public StoreNewKeyException(final String message, final Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public StoreNewKeyException(final String message) {
18+
super(message);
19+
}
20+
}

osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/test/java/org/opensmartgridplatform/adapter/protocol/dlms/application/services/SecretManagementServiceTest.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
package org.opensmartgridplatform.adapter.protocol.dlms.application.services;
66

77
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
89
import static org.mockito.ArgumentMatchers.any;
910
import static org.mockito.ArgumentMatchers.same;
1011
import static org.mockito.Mockito.times;
1112
import static org.mockito.Mockito.verify;
1213
import static org.mockito.Mockito.when;
1314

14-
import java.util.Arrays;
15-
import java.util.HashMap;
15+
import java.util.EnumMap;
1616
import java.util.List;
1717
import java.util.Map;
1818
import org.apache.commons.codec.binary.Hex;
@@ -24,7 +24,7 @@
2424
import org.mockito.junit.jupiter.MockitoExtension;
2525
import org.opensmartgridplatform.adapter.protocol.dlms.application.wsclient.SecretManagementClient;
2626
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;
2828
import org.opensmartgridplatform.shared.infra.jms.MessageMetadata;
2929
import org.opensmartgridplatform.shared.security.RsaEncrypter;
3030
import org.opensmartgridplatform.ws.schema.core.secret.management.ActivateSecretsRequest;
@@ -41,7 +41,7 @@
4141
import org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecrets;
4242

4343
@ExtendWith(MockitoExtension.class)
44-
public class SecretManagementServiceTest {
44+
class SecretManagementServiceTest {
4545
private static final String DEVICE_IDENTIFICATION = "E000123456789";
4646
private static final SecurityKeyType KEY_TYPE = SecurityKeyType.E_METER_ENCRYPTION;
4747
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() {
7272
}
7373

7474
@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);
7877
final GetSecretsResponse response = new GetSecretsResponse();
7978
response.setResult(OsgpResultType.OK);
8079
response.setTypedSecrets(new TypedSecrets());
@@ -83,35 +82,51 @@ public void testGetKeys() {
8382
same(messageMetadata), any(GetSecretsRequest.class)))
8483
.thenReturn(response);
8584
when(this.decrypterForProtocolAdapterDlms.decrypt(SOAP_SECRET)).thenReturn(UNENCRYPTED_SECRET);
86-
// EXECUTE
85+
8786
final Map<SecurityKeyType, byte[]> result =
8887
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);
9290
assertThat(result.keySet().iterator().next()).isEqualTo(KEY_TYPE);
9391
assertThat(result.values().iterator().next()).isEqualTo(UNENCRYPTED_SECRET);
9492
}
9593

9694
@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);
9997
keys.put(KEY_TYPE, UNENCRYPTED_SECRET);
10098
final StoreSecretsResponse response = new StoreSecretsResponse();
10199
response.setResult(OsgpResultType.OK);
102100
when(this.encrypterForSecretManagement.encrypt(UNENCRYPTED_SECRET)).thenReturn(SOAP_SECRET);
103101
when(this.secretManagementClient.storeSecretsRequest(same(messageMetadata), any()))
104102
.thenReturn(response);
105-
// EXECUTE
103+
106104
this.secretManagementTestService.storeNewKeys(messageMetadata, DEVICE_IDENTIFICATION, keys);
107-
// ASSERT
105+
108106
verify(this.secretManagementClient, times(1))
109107
.storeSecretsRequest(same(messageMetadata), any(StoreSecretsRequest.class));
110108
}
111109

112110
@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);
115130
final ArgumentCaptor<ActivateSecretsRequest> activateSecretsCaptor =
116131
ArgumentCaptor.forClass(ActivateSecretsRequest.class);
117132
// EXECUTE
@@ -127,25 +142,25 @@ public void testActivateKeys() throws ProtocolAdapterException {
127142
}
128143

129144
@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);
132147
final GenerateAndStoreSecretsResponse response = new GenerateAndStoreSecretsResponse();
133148
response.setResult(OsgpResultType.OK);
134149
response.setTypedSecrets(new TypedSecrets());
135150
response.getTypedSecrets().getTypedSecret().add(TYPED_SECRET);
136151
when(this.secretManagementClient.generateAndStoreSecrets(same(messageMetadata), any()))
137152
.thenReturn(response);
138153
when(this.decrypterForProtocolAdapterDlms.decrypt(SOAP_SECRET)).thenReturn(UNENCRYPTED_SECRET);
139-
// EXECUTE
154+
140155
final Map<SecurityKeyType, byte[]> keys =
141156
this.secretManagementTestService.generate128BitsKeysAndStoreAsNewKeys(
142157
messageMetadata, DEVICE_IDENTIFICATION, keyTypes);
143-
// ASSERT
144-
assertThat(keys.get(KEY_TYPE)).isEqualTo(UNENCRYPTED_SECRET);
158+
159+
assertThat(keys).containsEntry(KEY_TYPE, UNENCRYPTED_SECRET);
145160
}
146161

147162
@Test
148-
public void testHasNewSecretAuthenticationKey() {
163+
void testHasNewSecretAuthenticationKey() {
149164
final HasNewSecretResponse responseTrue = new HasNewSecretResponse();
150165
responseTrue.setHasNewSecret(true);
151166
final HasNewSecretResponse responseFalse = new HasNewSecretResponse();
@@ -163,15 +178,14 @@ public void testHasNewSecretAuthenticationKey() {
163178
}
164179
});
165180

166-
// EXECUTE
167181
final boolean result =
168182
this.secretManagementTestService.hasNewSecret(messageMetadata, DEVICE_IDENTIFICATION);
169-
// ASSERT
183+
170184
assertThat(result).isTrue();
171185
}
172186

173187
@Test
174-
public void testHasNewSecretEncryptionKey() {
188+
void testHasNewSecretEncryptionKey() {
175189
final HasNewSecretResponse responseTrue = new HasNewSecretResponse();
176190
responseTrue.setHasNewSecret(true);
177191
final HasNewSecretResponse responseFalse = new HasNewSecretResponse();
@@ -189,10 +203,9 @@ public void testHasNewSecretEncryptionKey() {
189203
}
190204
});
191205

192-
// EXECUTE
193206
final boolean result =
194207
this.secretManagementTestService.hasNewSecret(messageMetadata, DEVICE_IDENTIFICATION);
195-
// ASSERT
208+
196209
assertThat(result).isTrue();
197210
}
198211
}

0 commit comments

Comments
 (0)