|
| 1 | +/* |
| 2 | + * Copyright [ 2020 - 2024 ] [Matthew Buckton] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.mapsmessaging.security.access.mapping; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.UUID; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +class UserMapParserTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + void testCreateMappingValidFormat() { |
| 30 | + String validIdentifier = "123e4567-e89b-12d3-a456-426614174000 = authDomain:username"; |
| 31 | + UserMapParser parser = new UserMapParser(); |
| 32 | + |
| 33 | + UserIdMap userIdMap = parser.createMapping(validIdentifier); |
| 34 | + |
| 35 | + assertNotNull(userIdMap, "Parsed UserIdMap should not be null"); |
| 36 | + assertEquals(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), userIdMap.getAuthId(), "AuthId should match the UUID in the identifier"); |
| 37 | + assertEquals("authDomain", userIdMap.getAuthDomain(), "AuthDomain should match the authDomain in the identifier"); |
| 38 | + assertEquals("username", userIdMap.getUsername(), "Username should match the username in the identifier"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void testCreateMappingInvalidFormat() { |
| 43 | + String invalidIdentifier = "invalidFormat"; |
| 44 | + UserMapParser parser = new UserMapParser(); |
| 45 | + |
| 46 | + assertThrows(IllegalArgumentException.class, () -> parser.createMapping(invalidIdentifier), "Invalid format should throw IllegalArgumentException"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testCreateListAndWriteToList() { |
| 51 | + List<String> identifiers = Arrays.asList("123e4567-e89b-12d3-a456-426614174000 = authDomain1:username1", "123e4567-e89b-12d3-a456-426614174001 = authDomain2:username2"); |
| 52 | + UserMapParser parser = new UserMapParser(); |
| 53 | + |
| 54 | + List<UserIdMap> userIdMaps = parser.createList(identifiers); |
| 55 | + assertEquals(2, userIdMaps.size(), "Should create two UserIdMap instances"); |
| 56 | + |
| 57 | + List<String> serializedList = parser.writeToList(userIdMaps); |
| 58 | + assertTrue(serializedList.containsAll(identifiers), "Serialized list should contain all original identifiers"); |
| 59 | + } |
| 60 | + |
| 61 | + // Additional tests can include more edge cases and parsing/writing scenarios |
| 62 | +} |
| 63 | + |
0 commit comments