/services/device-registry-mongodb/src/test/java/org/eclipse/hono/deviceregistry/mongodb/model/CredentialsDtoTest.java

https://github.com/eclipse/hono · Java · 117 lines · 67 code · 26 blank · 24 comment · 0 complexity · 12ebed1d83eddc59b046f9bff9a7f8a1 MD5 · raw file

  1. /**
  2. * Copyright (c) 2020 Contributors to the Eclipse Foundation
  3. *
  4. * See the NOTICE file(s) distributed with this work for additional
  5. * information regarding copyright ownership.
  6. *
  7. * This program and the accompanying materials are made available under the
  8. * terms of the Eclipse Public License 2.0 which is available at
  9. * http://www.eclipse.org/legal/epl-2.0
  10. *
  11. * SPDX-License-Identifier: EPL-2.0
  12. */
  13. package org.eclipse.hono.deviceregistry.mongodb.model;
  14. import static org.assertj.core.api.Assertions.assertThat;
  15. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  16. import java.nio.charset.StandardCharsets;
  17. import java.util.List;
  18. import org.eclipse.hono.client.ClientErrorException;
  19. import org.eclipse.hono.service.credentials.Credentials;
  20. import org.eclipse.hono.service.management.credentials.PskCredential;
  21. import org.eclipse.hono.service.management.credentials.PskSecret;
  22. import org.junit.jupiter.api.BeforeEach;
  23. import org.junit.jupiter.api.Test;
  24. /**
  25. * A CredentialsDtoTest.
  26. *
  27. */
  28. class CredentialsDtoTest {
  29. private PskSecret existingSecret;
  30. private PskCredential existingCred;
  31. /**
  32. * Sets up the fixture.
  33. */
  34. @BeforeEach
  35. void setUp() {
  36. existingSecret = new PskSecret();
  37. existingSecret.setId("abc");
  38. existingSecret.setKey("shared-key".getBytes(StandardCharsets.UTF_8));
  39. existingCred = new PskCredential("psk-id", List.of(existingSecret));
  40. }
  41. @Test
  42. void testConstructorDetectsDuplicateSecretId() {
  43. final PskSecret dup = new PskSecret();
  44. dup.setId(existingSecret.getId());
  45. existingCred.setSecrets(List.of(existingSecret, dup));
  46. assertThatThrownBy(() -> new CredentialsDto("tenant", "device", List.of(existingCred), "1"))
  47. .isInstanceOf(ClientErrorException.class);
  48. }
  49. @Test
  50. void testNewSecretWithoutIdDoesNotRequireMerging() {
  51. final PskCredential updatedCred = Credentials.createPSKCredential("psk-id", "other-key");
  52. final CredentialsDto updatedDto = new CredentialsDto("tenant", "device", List.of(updatedCred), "1");
  53. assertThat(updatedDto.requiresMerging()).isFalse();
  54. }
  55. @Test
  56. void testMergeRejectsUnknownSecretId() {
  57. final CredentialsDto existingDto = new CredentialsDto("tenant", "device", List.of(existingCred), "1");
  58. final PskSecret updatedSecret = new PskSecret();
  59. updatedSecret.setId("def");
  60. updatedSecret.setKey("irrelevant".getBytes(StandardCharsets.UTF_8));
  61. final PskCredential updatedCred = new PskCredential("psk-id", List.of(existingSecret, updatedSecret));
  62. final CredentialsDto updatedDto = new CredentialsDto("tenant", "device", List.of(updatedCred), "1");
  63. assertThat(updatedDto.requiresMerging());
  64. assertThatThrownBy(() -> updatedDto.merge(existingDto)).isInstanceOf(IllegalArgumentException.class);
  65. }
  66. /**
  67. * Verifies that existing credentials with a secret are merged into updated credentials
  68. * that contain a secret with the same ID as the existing secret and an additional secret
  69. * without an ID.
  70. */
  71. @Test
  72. void testMergeSucceedsForAdditionalSecretWithNoId() {
  73. final CredentialsDto existingDto = new CredentialsDto("tenant", "device", List.of(existingCred), "1");
  74. final PskSecret unchangedSecret = new PskSecret();
  75. unchangedSecret.setId(existingSecret.getId());
  76. final PskSecret newSecret = new PskSecret();
  77. newSecret.setKey("irrelevant".getBytes(StandardCharsets.UTF_8));
  78. final PskCredential updatedCred = new PskCredential("psk-id", List.of(unchangedSecret, newSecret));
  79. final CredentialsDto updatedDto = new CredentialsDto("tenant", "device", List.of(updatedCred), "1");
  80. assertThat(updatedDto.requiresMerging());
  81. updatedDto.merge(existingDto);
  82. final PskSecret secret = updatedDto.getCredentials().get(0).getSecrets()
  83. .stream()
  84. .filter(s -> s.getId().equals(existingSecret.getId()))
  85. .map(PskSecret.class::cast)
  86. .findAny()
  87. .orElse(null);
  88. assertThat(secret).isNotNull();
  89. assertThat(secret.getKey()).isEqualTo(existingSecret.getKey());
  90. }
  91. }