diff --git a/sdk/communication/azure-communication-common/CHANGELOG.md b/sdk/communication/azure-communication-common/CHANGELOG.md index 9005e80f880d6..d910fafc2fd6e 100644 --- a/sdk/communication/azure-communication-common/CHANGELOG.md +++ b/sdk/communication/azure-communication-common/CHANGELOG.md @@ -12,6 +12,10 @@ ## 1.1.4 (2022-06-07) +### Features Added + +- Added `String getRawId()` and `static CommunicationIdentifier fromRawId(String rawId)` to `CommunicationIdentifier` to translate between a `CommunicationIdentifier` and its underlying canonical rawId representation. Developers can now use the rawId as an encoded format for identifiers to store in their databases or as stable keys in general. + ### Other Changes #### Dependency Updates diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationIdentifier.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationIdentifier.java index 6b6374df1fac2..dc1b0f081eac1 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationIdentifier.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationIdentifier.java @@ -2,8 +2,81 @@ // Licensed under the MIT License. package com.azure.communication.common; +import com.azure.core.util.CoreUtils; + /** * Common communication identifier for Communication Services */ public abstract class CommunicationIdentifier { + /** + * encoded format for identifiers to store in databases or as stable keys in general. + */ + protected String rawId; + + /** + * When storing rawIds, use this function to restore the identifier that was encoded in the rawId. + * + * @param rawId raw id. + * @return CommunicationIdentifier + * @throws IllegalArgumentException raw id is null or empty. + */ + public static CommunicationIdentifier fromRawId(String rawId) { + if (CoreUtils.isNullOrEmpty(rawId)) { + throw new IllegalArgumentException("The parameter [rawId] cannot be null to empty."); + } + + if (rawId.startsWith("4:")) { + return new PhoneNumberIdentifier("+" + rawId.substring("4:".length())); + } + final String[] segments = rawId.split(":"); + if (segments.length < 3) { + return new UnknownIdentifier(rawId); + } + + final String prefix = segments[0] + ":" + segments[1] + ":"; + final String suffix = rawId.substring(prefix.length()); + + if ("8:teamsvisitor:".equals(prefix)) { + return new MicrosoftTeamsUserIdentifier(suffix, true); + } else if ("8:orgid:".equals(prefix)) { + return new MicrosoftTeamsUserIdentifier(suffix, false); + } else if ("8:dod:".equals(prefix)) { + return new MicrosoftTeamsUserIdentifier(suffix, false).setCloudEnvironment(CommunicationCloudEnvironment.DOD); + } else if ("8:gcch:".equals(prefix)) { + return new MicrosoftTeamsUserIdentifier(suffix, false).setCloudEnvironment(CommunicationCloudEnvironment.GCCH); + } else if ("8:acs:".equals(prefix) || "8:spool:".equals(prefix) || "8:dod-acs:".equals(prefix) || "8:gcch-acs:".equals(prefix)) { + return new CommunicationUserIdentifier(rawId); + } + + return new UnknownIdentifier(rawId); + } + + /** + * Returns the rawId for a given CommunicationIdentifier. + * You can use the rawId for encoding the identifier and then use it as a key in a database. + * + * @return raw id + */ + public String getRawId() { + return rawId; + } + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + + if (!(that instanceof CommunicationIdentifier)) { + return false; + } + + CommunicationIdentifier thatId = (CommunicationIdentifier) that; + return this.getRawId().equals(thatId.getRawId()); + } + + @Override + public int hashCode() { + return getRawId().hashCode(); + } } diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationUserIdentifier.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationUserIdentifier.java index ab86164b083c3..a44e7356b8205 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationUserIdentifier.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationUserIdentifier.java @@ -22,10 +22,12 @@ public CommunicationUserIdentifier(String id) { throw new IllegalArgumentException("The initialization parameter [id] cannot be null or empty."); } this.id = id; + this.rawId = id; } /** * Get id of the communication user. + * * @return id of the communication user. */ public String getId() { @@ -42,11 +44,11 @@ public boolean equals(Object that) { return false; } - return ((CommunicationUserIdentifier) that).getId().equals(id); + return ((CommunicationUserIdentifier) that).getRawId().equals(getRawId()); } @Override public int hashCode() { - return getId().hashCode(); + return getRawId().hashCode(); } } diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/MicrosoftTeamsUserIdentifier.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/MicrosoftTeamsUserIdentifier.java index 610c2196d2146..a50315bce4dce 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/MicrosoftTeamsUserIdentifier.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/MicrosoftTeamsUserIdentifier.java @@ -12,9 +12,9 @@ public final class MicrosoftTeamsUserIdentifier extends CommunicationIdentifier private final String userId; private final boolean isAnonymous; - private CommunicationCloudEnvironment cloudEnvironment = CommunicationCloudEnvironment.PUBLIC; + private boolean rawIdSet = false; - private String rawId; + private CommunicationCloudEnvironment cloudEnvironment = CommunicationCloudEnvironment.PUBLIC; /** * Creates a MicrosoftTeamsUserIdentifier object @@ -23,13 +23,14 @@ public final class MicrosoftTeamsUserIdentifier extends CommunicationIdentifier * @param isAnonymous set this to true if the user is anonymous, * for example when joining a meeting with a share link * @throws IllegalArgumentException thrown if userId parameter fail the validation. - */ + */ public MicrosoftTeamsUserIdentifier(String userId, boolean isAnonymous) { if (CoreUtils.isNullOrEmpty(userId)) { throw new IllegalArgumentException("The initialization parameter [userId] cannot be null or empty."); } this.userId = userId; this.isAnonymous = isAnonymous; + generateRawId(); } /** @@ -37,7 +38,7 @@ public MicrosoftTeamsUserIdentifier(String userId, boolean isAnonymous) { * * @param userId Id of the Microsoft Teams user. If the user isn't anonymous, the id is the Azure AD object id of the user. * @throws IllegalArgumentException thrown if userId parameter fail the validation. - */ + */ public MicrosoftTeamsUserIdentifier(String userId) { this(userId, false); } @@ -57,18 +58,9 @@ public boolean isAnonymous() { return this.isAnonymous; } - /** - * Set cloud environment of the Teams user identifier - * @param cloudEnvironment the cloud environment in which this identifier is created - * @return this object - */ - public MicrosoftTeamsUserIdentifier setCloudEnvironment(CommunicationCloudEnvironment cloudEnvironment) { - this.cloudEnvironment = cloudEnvironment; - return this; - } - /** * Get cloud environment of the Teams user identifier + * * @return cloud environment in which this identifier is created */ public CommunicationCloudEnvironment getCloudEnvironment() { @@ -76,20 +68,26 @@ public CommunicationCloudEnvironment getCloudEnvironment() { } /** - * Get full id of the identifier. This id is optional. - * @return full id of the identifier + * Set cloud environment of the Teams user identifier + * + * @param cloudEnvironment the cloud environment in which this identifier is created + * @return this object */ - public String getRawId() { - return rawId; + public MicrosoftTeamsUserIdentifier setCloudEnvironment(CommunicationCloudEnvironment cloudEnvironment) { + this.cloudEnvironment = cloudEnvironment; + generateRawId(); + return this; } /** * Set full id of the identifier + * * @param rawId full id of the identifier * @return CommunicationIdentifier object itself */ public MicrosoftTeamsUserIdentifier setRawId(String rawId) { this.rawId = rawId; + rawIdSet = true; return this; } @@ -104,10 +102,6 @@ public boolean equals(Object that) { } MicrosoftTeamsUserIdentifier thatId = (MicrosoftTeamsUserIdentifier) that; - if (!thatId.getUserId().equals(this.getUserId()) - || thatId.isAnonymous != this.isAnonymous) { - return false; - } if (cloudEnvironment != null && !cloudEnvironment.equals(thatId.cloudEnvironment)) { return false; @@ -122,10 +116,22 @@ public boolean equals(Object that) { || thatId.getRawId().equals(this.getRawId()); } - @Override public int hashCode() { - return userId.hashCode(); + return getRawId().hashCode(); } + private void generateRawId() { + if (!rawIdSet) { + if (this.isAnonymous) { + this.rawId = "8:teamsvisitor:" + this.userId; + } else if (cloudEnvironment.equals(CommunicationCloudEnvironment.DOD)) { + this.rawId = "8:dod:" + this.userId; + } else if (cloudEnvironment.equals(CommunicationCloudEnvironment.GCCH)) { + this.rawId = "8:gcch:" + this.userId; + } else { + this.rawId = "8:orgid:" + this.userId; + } + } + } } diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/PhoneNumberIdentifier.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/PhoneNumberIdentifier.java index 17f56b3e89a4a..fe5dd6b6f1ed7 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/PhoneNumberIdentifier.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/PhoneNumberIdentifier.java @@ -10,13 +10,12 @@ public final class PhoneNumberIdentifier extends CommunicationIdentifier { private final String phoneNumber; - private String rawId; /** * Creates a PhoneNumberIdentifier object * * @param phoneNumber the string identifier representing the PhoneNumber in E.164 format. - * E.164 is a phone number formatted as +[CountryCode][AreaCode][LocalNumber] eg. "+18005555555" + * E.164 is a phone number formatted as +[CountryCode][AreaCode][LocalNumber] eg. "+18005555555" * @throws IllegalArgumentException thrown if phoneNumber parameter fail the validation. */ public PhoneNumberIdentifier(String phoneNumber) { @@ -24,6 +23,7 @@ public PhoneNumberIdentifier(String phoneNumber) { throw new IllegalArgumentException("The initialization parameter [phoneNumber] cannot be null to empty."); } this.phoneNumber = phoneNumber; + this.rawId = "4:" + phoneNumber.replaceAll("^[+]", ""); } /** @@ -33,16 +33,9 @@ public String getPhoneNumber() { return phoneNumber; } - /** - * Get full id of the identifier. This id is optional. - * @return full id of the identifier - */ - public String getRawId() { - return rawId; - } - /** * Set full id of the identifier + * * @param rawId full id of the identifier * @return PhoneNumberIdentifier object itself */ @@ -62,9 +55,6 @@ public boolean equals(Object that) { } PhoneNumberIdentifier phoneId = (PhoneNumberIdentifier) that; - if (!phoneNumber.equals(phoneId.phoneNumber)) { - return false; - } return getRawId() == null || phoneId.getRawId() == null @@ -73,6 +63,6 @@ public boolean equals(Object that) { @Override public int hashCode() { - return phoneNumber.hashCode(); + return getRawId().hashCode(); } } diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/UnknownIdentifier.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/UnknownIdentifier.java index 58c5fba60a736..4522400d1be5b 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/UnknownIdentifier.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/UnknownIdentifier.java @@ -22,10 +22,12 @@ public UnknownIdentifier(String id) { throw new IllegalArgumentException("The initialization parameter [id] cannot be null or empty."); } this.id = id; + this.rawId = id; } /** * Get id of this identifier + * * @return id of this identifier */ public String getId() { @@ -43,11 +45,11 @@ public boolean equals(Object that) { } UnknownIdentifier thatId = (UnknownIdentifier) that; - return this.id.equals(thatId.id); + return this.getRawId().equals(thatId.getRawId()); } @Override public int hashCode() { - return id.hashCode(); + return getRawId().hashCode(); } } diff --git a/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationIdentifierTests.java b/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationIdentifierTests.java index b5b286f99b8b7..c8c418926e1e8 100644 --- a/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationIdentifierTests.java +++ b/sdk/communication/azure-communication-common/src/test/java/com/azure/communication/common/CommunicationIdentifierTests.java @@ -3,37 +3,143 @@ package com.azure.communication.common; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import static org.junit.jupiter.api.Assertions.*; public class CommunicationIdentifierTests { final String userId = "user id"; final String fullId = "some lengthy id string"; - @Test - public void equalityOnlyTestRawIdIfPresentOnBothSide() { - assertEquals(new MicrosoftTeamsUserIdentifier(userId, true).setRawId(fullId), - new MicrosoftTeamsUserIdentifier(userId, true)); - assertEquals(new MicrosoftTeamsUserIdentifier(userId, true), - new MicrosoftTeamsUserIdentifier(userId, true)); - assertEquals(new MicrosoftTeamsUserIdentifier(userId, true), - new MicrosoftTeamsUserIdentifier(userId, true).setRawId(fullId)); - assertNotEquals(new MicrosoftTeamsUserIdentifier(userId, true).setRawId(fullId), - new MicrosoftTeamsUserIdentifier(userId, true).setRawId("another id")); - - assertEquals(new PhoneNumberIdentifier("+12223334444").setRawId(fullId), - new PhoneNumberIdentifier("+12223334444")); - assertEquals(new PhoneNumberIdentifier("+12223334444"), new PhoneNumberIdentifier("+12223334444")); - assertEquals(new PhoneNumberIdentifier("+12223334444"), - new PhoneNumberIdentifier("+12223334444").setRawId(fullId)); - assertNotEquals(new PhoneNumberIdentifier("+12223334444").setRawId(fullId), - new PhoneNumberIdentifier("+12223334444").setRawId("another id")); - } - @Test public void defaultCloudIsPublicForMicrosoftTeamsUserIdentifier() { assertEquals(CommunicationCloudEnvironment.PUBLIC, new MicrosoftTeamsUserIdentifier(userId, true).setRawId(fullId).getCloudEnvironment()); } + + @Test + public void rawIdTakesPrecedenceInEqualityCheck() { + // Teams users + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true), + new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true)); + assertNotEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true) + .setRawId("Raw Id"), + new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true) + .setRawId("Another Raw Id")); + assertEquals(new MicrosoftTeamsUserIdentifier("override", true) + .setRawId("8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130"), + new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true)); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true), + new MicrosoftTeamsUserIdentifier("override", true).setRawId("8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", false) + .setCloudEnvironment(CommunicationCloudEnvironment.GCCH), + new MicrosoftTeamsUserIdentifier("override", false) + .setCloudEnvironment(CommunicationCloudEnvironment.GCCH).setRawId("8:gcch:45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true) + .setCloudEnvironment(CommunicationCloudEnvironment.GCCH), + new MicrosoftTeamsUserIdentifier("override", false) + .setCloudEnvironment(CommunicationCloudEnvironment.GCCH).setRawId("8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", false) + .setCloudEnvironment(CommunicationCloudEnvironment.DOD), + new MicrosoftTeamsUserIdentifier("override", false) + .setCloudEnvironment(CommunicationCloudEnvironment.DOD).setRawId("8:dod:45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true) + .setCloudEnvironment(CommunicationCloudEnvironment.DOD), + new MicrosoftTeamsUserIdentifier("override", false) + .setCloudEnvironment(CommunicationCloudEnvironment.DOD).setRawId("8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", false), + new MicrosoftTeamsUserIdentifier("override", false) + .setRawId("8:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true), + new MicrosoftTeamsUserIdentifier("override", false) + .setRawId("8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertEquals(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true) + .setRawId("test raw id") + .setCloudEnvironment(CommunicationCloudEnvironment.GCCH), + new MicrosoftTeamsUserIdentifier("override", false) + .setCloudEnvironment(CommunicationCloudEnvironment.GCCH).setRawId("test raw id")); + + // Phone numbers + assertEquals(new PhoneNumberIdentifier("+14255550123"), new PhoneNumberIdentifier("+14255550123")); + assertNotEquals(new PhoneNumberIdentifier("+14255550123").setRawId("Raw Id"), + new PhoneNumberIdentifier("+14255550123").setRawId("Another Raw Id")); + + assertEquals(new PhoneNumberIdentifier("+override").setRawId("4:14255550123"), + new PhoneNumberIdentifier("+14255550123")); + assertEquals(new PhoneNumberIdentifier("+14255550123"), + new PhoneNumberIdentifier("+override").setRawId("4:14255550123")); + } + + @Test + public void getRawIdOfIdentifier() + { + assertRawId(new CommunicationUserIdentifier("8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"), "8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new CommunicationUserIdentifier("8:gcch-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"), "8:gcch-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new CommunicationUserIdentifier("someFutureFormat"), "someFutureFormat"); + assertRawId(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130"), "8:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130"), "8:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130").setCloudEnvironment(CommunicationCloudEnvironment.DOD), "8:dod:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130").setCloudEnvironment(CommunicationCloudEnvironment.GCCH), "8:gcch:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", false), "8:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true), "8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true).setRawId("8:orgid:legacyFormat"), "8:orgid:legacyFormat"); + assertRawId(new PhoneNumberIdentifier("+112345556789"), "4:112345556789"); + assertRawId(new PhoneNumberIdentifier("+112345556789").setRawId("4:otherFormat"), "4:otherFormat"); + assertRawId(new UnknownIdentifier("28:45ab2481-1c1c-4005-be24-0ffb879b1130"), "28:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRawId(new UnknownIdentifier("someFutureFormat"), "someFutureFormat"); + } + + @Test + public void createIdentifierFromRawId() + { + assertIdentifier("8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130", new CommunicationUserIdentifier("8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertIdentifier("8:spool:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130", new CommunicationUserIdentifier("8:spool:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertIdentifier("8:dod-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130", new CommunicationUserIdentifier("8:dod-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertIdentifier("8:gcch-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130", new CommunicationUserIdentifier("8:gcch-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130")); + assertIdentifier("8:acs:something", new CommunicationUserIdentifier("8:acs:something")); + assertIdentifier("8:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130", new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", false).setCloudEnvironment(CommunicationCloudEnvironment.PUBLIC)); + assertIdentifier("8:dod:45ab2481-1c1c-4005-be24-0ffb879b1130", new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", false).setCloudEnvironment(CommunicationCloudEnvironment.DOD)); + assertIdentifier("8:gcch:45ab2481-1c1c-4005-be24-0ffb879b1130", new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", false).setCloudEnvironment(CommunicationCloudEnvironment.GCCH)); + assertIdentifier("8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130", new MicrosoftTeamsUserIdentifier("45ab2481-1c1c-4005-be24-0ffb879b1130", true).setCloudEnvironment(CommunicationCloudEnvironment.PUBLIC)); + assertIdentifier("8:orgid:legacyFormat", new MicrosoftTeamsUserIdentifier("legacyFormat", false).setCloudEnvironment(CommunicationCloudEnvironment.PUBLIC)); + assertIdentifier("4:112345556789", new PhoneNumberIdentifier("+112345556789")); + assertIdentifier("4:otherFormat", new PhoneNumberIdentifier("+otherFormat")); + assertIdentifier("28:45ab2481-1c1c-4005-be24-0ffb879b1130", new UnknownIdentifier("28:45ab2481-1c1c-4005-be24-0ffb879b1130")); + + final IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, () -> CommunicationIdentifier.fromRawId(null)); + assertEquals("The parameter [rawId] cannot be null to empty.", illegalArgumentException.getMessage()); + } + + @Test + public void rawIdStaysTheSameAfterConversionToIdentifierAndBack() + { + assertRoundTrip("8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:spool:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:dod-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:gcch-acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:acs:something"); + assertRoundTrip("8:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:dod:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:gcch:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:teamsvisitor:45ab2481-1c1c-4005-be24-0ffb879b1130"); + assertRoundTrip("8:orgid:legacyFormat"); + assertRoundTrip("4:112345556789"); + assertRoundTrip("4:otherFormat"); + assertRoundTrip("28:45ab2481-1c1c-4005-be24-0ffb879b1130"); + } + + private void assertRawId(CommunicationIdentifier identifier, String expectedRawId) { + assertEquals(identifier.rawId, expectedRawId); + } + + private void assertIdentifier(String rawId, CommunicationIdentifier expectedIdentifier) + { + assertEquals(CommunicationIdentifier.fromRawId(rawId), expectedIdentifier); + assertEquals(CommunicationIdentifier.fromRawId(rawId).hashCode(), expectedIdentifier.hashCode()); + } + + private void assertRoundTrip(String rawId) + { + assertEquals(CommunicationIdentifier.fromRawId(rawId).getRawId(), rawId); + } }