Skip to content

Commit

Permalink
[Communication]: Add identifier model and serializer (#18685)
Browse files Browse the repository at this point in the history
* Add CommunicationIdentifierModel to models

* Move IdentifierModel to common and add serializer

* Add unit tests

* Fix code styling issues

* Rename PhoneNumberIdentifier getValue to getPhoneNumber

Co-authored-by: JP Chen <[email protected]>
  • Loading branch information
JianpingChen and JP Chen authored Jan 20, 2021
1 parent ddd9976 commit f707bc7
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Mono<Response<UpdateNumberCapabilitiesResponse>> updateCapabilitiesWithResponse(
Map<String, NumberUpdateCapabilities> capabilitiesMap = new HashMap<>();
for (Map.Entry<PhoneNumberIdentifier, NumberUpdateCapabilities> entry
: phoneNumberCapabilitiesUpdate.entrySet()) {
capabilitiesMap.put(entry.getKey().getValue(), entry.getValue());
capabilitiesMap.put(entry.getKey().getPhoneNumber(), entry.getValue());
}
UpdateNumberCapabilitiesRequest updateNumberCapabilitiesRequest = new UpdateNumberCapabilitiesRequest();
updateNumberCapabilitiesRequest.setPhoneNumberCapabilitiesUpdate(capabilitiesMap);
Expand Down Expand Up @@ -289,7 +289,7 @@ Mono<Response<NumberConfigurationResponse>> getNumberConfigurationWithResponse(
try {
Objects.requireNonNull(phoneNumber, "'phoneNumber' cannot be null.");
NumberConfigurationPhoneNumber configurationPhoneNumber = new NumberConfigurationPhoneNumber();
configurationPhoneNumber.setPhoneNumber(phoneNumber.getValue());
configurationPhoneNumber.setPhoneNumber(phoneNumber.getPhoneNumber());

if (context == null) {
return phoneNumberAdministrations.getNumberConfigurationWithResponseAsync(
Expand Down Expand Up @@ -335,7 +335,7 @@ Mono<Response<Void>> configureNumberWithResponse(
Objects.requireNonNull(pstnConfiguration, "'pstnConfiguration' cannot be null.");

NumberConfiguration numberConfiguration = new NumberConfiguration();
numberConfiguration.setPhoneNumber(phoneNumber.getValue()).setPstnConfiguration(pstnConfiguration);
numberConfiguration.setPhoneNumber(phoneNumber.getPhoneNumber()).setPstnConfiguration(pstnConfiguration);

if (context == null) {
return phoneNumberAdministrations.configureNumberWithResponseAsync(numberConfiguration);
Expand Down Expand Up @@ -373,7 +373,7 @@ Mono<Response<Void>> unconfigureNumberWithResponse(PhoneNumberIdentifier phoneNu
try {
Objects.requireNonNull(phoneNumber, "'phoneNumber' cannot be null.");
NumberConfigurationPhoneNumber configurationPhoneNumber = new NumberConfigurationPhoneNumber();
configurationPhoneNumber.setPhoneNumber(phoneNumber.getValue());
configurationPhoneNumber.setPhoneNumber(phoneNumber.getPhoneNumber());

if (context == null) {
return phoneNumberAdministrations.unconfigureNumberWithResponseAsync(configurationPhoneNumber);
Expand Down Expand Up @@ -559,7 +559,7 @@ private Mono<Response<ReleaseResponse>> releasePhoneNumbersWithResponse(

List<String> phoneNumberStrings = phoneNumbers
.stream()
.map(PhoneNumberIdentifier::getValue)
.map(PhoneNumberIdentifier::getPhoneNumber)
.collect(Collectors.toList());
ReleaseRequest releaseRequest = new ReleaseRequest();
releaseRequest.setPhoneNumbers(phoneNumberStrings);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.communication.common;

import com.azure.core.util.ExpandableStringEnum;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Collection;

/** Defines values for CommunicationIdentifierKind. */
public final class CommunicationIdentifierKind extends ExpandableStringEnum<CommunicationIdentifierKind> {
/** Static value unknown for CommunicationIdentifierKind. */
public static final CommunicationIdentifierKind UNKNOWN = fromString("unknown");

/** Static value communicationUser for CommunicationIdentifierKind. */
public static final CommunicationIdentifierKind COMMUNICATION_USER = fromString("communicationUser");

/** Static value phoneNumber for CommunicationIdentifierKind. */
public static final CommunicationIdentifierKind PHONE_NUMBER = fromString("phoneNumber");

/** Static value callingApplication for CommunicationIdentifierKind. */
public static final CommunicationIdentifierKind CALLING_APPLICATION = fromString("callingApplication");

/** Static value microsoftTeamsUser for CommunicationIdentifierKind. */
public static final CommunicationIdentifierKind MICROSOFT_TEAMS_USER = fromString("microsoftTeamsUser");

/**
* Creates or finds a CommunicationIdentifierKind from its string representation.
*
* @param name a name to look for.
* @return the corresponding CommunicationIdentifierKind.
*/
@JsonCreator
public static CommunicationIdentifierKind fromString(String name) {
return fromString(name, CommunicationIdentifierKind.class);
}

/** @return known CommunicationIdentifierKind values. */
public static Collection<CommunicationIdentifierKind> values() {
return values(CommunicationIdentifierKind.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.communication.common;

import com.azure.core.annotation.Fluent;
import com.fasterxml.jackson.annotation.JsonProperty;

/** The CommunicationIdentifierModel model. */
@Fluent
public final class CommunicationIdentifierModel {
/*
* Kind of the communication identifier.
*/
@JsonProperty(value = "kind", required = true)
private com.azure.communication.common.CommunicationIdentifierKind kind;

/*
* Full Id of the identifier.
*/
@JsonProperty(value = "id")
private String id;

/*
* The phone number in E.164 format.
*/
@JsonProperty(value = "phoneNumber")
private String phoneNumber;

/*
* The AAD object Id of the Microsoft Teams user.
*/
@JsonProperty(value = "microsoftTeamsUserId")
private String microsoftTeamsUserId;

/*
* True if the identifier is anonymous.
*/
@JsonProperty(value = "isAnonymous")
private Boolean isAnonymous;

/**
* Get the kind property: Kind of the communication identifier.
*
* @return the kind value.
*/
public com.azure.communication.common.CommunicationIdentifierKind getKind() {
return this.kind;
}

/**
* Set the kind property: Kind of the communication identifier.
*
* @param kind the kind value to set.
* @return the CommunicationIdentifierModel object itself.
*/
public CommunicationIdentifierModel setKind(com.azure.communication.common.CommunicationIdentifierKind kind) {
this.kind = kind;
return this;
}

/**
* Get the id property: Full Id of the identifier.
*
* @return the id value.
*/
public String getId() {
return this.id;
}

/**
* Set the id property: Full Id of the identifier.
*
* @param id the id value to set.
* @return the CommunicationIdentifierModel object itself.
*/
public CommunicationIdentifierModel setId(String id) {
this.id = id;
return this;
}

/**
* Get the phoneNumber property: The phone number in E.164 format.
*
* @return the phoneNumber value.
*/
public String getPhoneNumber() {
return this.phoneNumber;
}

/**
* Set the phoneNumber property: The phone number in E.164 format.
*
* @param phoneNumber the phoneNumber value to set.
* @return the CommunicationIdentifierModel object itself.
*/
public CommunicationIdentifierModel setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
return this;
}

/**
* Get the microsoftTeamsUserId property: The AAD object Id of the Microsoft Teams user.
*
* @return the microsoftTeamsUserId value.
*/
public String getMicrosoftTeamsUserId() {
return this.microsoftTeamsUserId;
}

/**
* Set the microsoftTeamsUserId property: The AAD object Id of the Microsoft Teams user.
*
* @param microsoftTeamsUserId the microsoftTeamsUserId value to set.
* @return the CommunicationIdentifierModel object itself.
*/
public CommunicationIdentifierModel setMicrosoftTeamsUserId(String microsoftTeamsUserId) {
this.microsoftTeamsUserId = microsoftTeamsUserId;
return this;
}

/**
* Get the isAnonymous property: True if the identifier is anonymous.
*
* @return the isAnonymous value.
*/
public Boolean isAnonymous() {
return this.isAnonymous;
}

/**
* Set the isAnonymous property: True if the identifier is anonymous.
*
* @param isAnonymous the isAnonymous value to set.
* @return the CommunicationIdentifierModel object itself.
*/
public CommunicationIdentifierModel setIsAnonymous(Boolean isAnonymous) {
this.isAnonymous = isAnonymous;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.communication.common;

import java.util.Objects;

class CommunicationIdentifierSerializer {
public static CommunicationIdentifier deserialize(CommunicationIdentifierModel identifier) {
String id = identifier.getId();
CommunicationIdentifierKind kind = identifier.getKind();

if (kind == CommunicationIdentifierKind.COMMUNICATION_USER) {
Objects.requireNonNull(id);
return new CommunicationUserIdentifier(id);
}

if (kind == CommunicationIdentifierKind.CALLING_APPLICATION) {
Objects.requireNonNull(id);
return new CallingApplicationIdentifier(id);
}

if (kind == CommunicationIdentifierKind.PHONE_NUMBER) {
Objects.requireNonNull(identifier.getPhoneNumber());
return new PhoneNumberIdentifier(identifier.getPhoneNumber());
}

if (kind == CommunicationIdentifierKind.MICROSOFT_TEAMS_USER) {
Objects.requireNonNull(identifier.getMicrosoftTeamsUserId());
return new MicrosoftTeamsUserIdentifier(identifier.getMicrosoftTeamsUserId(), identifier.isAnonymous());
}

Objects.requireNonNull(id);
return new UnknownIdentifier(id);
}

public static CommunicationIdentifierModel serialize(CommunicationIdentifier identifier) {
if (identifier instanceof CommunicationUserIdentifier) {
return new CommunicationIdentifierModel()
.setKind(CommunicationIdentifierKind.COMMUNICATION_USER)
.setId(((CommunicationUserIdentifier) identifier).getId());
}

if (identifier instanceof CallingApplicationIdentifier) {
return new CommunicationIdentifierModel()
.setKind(CommunicationIdentifierKind.CALLING_APPLICATION)
.setId(((CallingApplicationIdentifier) identifier).getId());
}

if (identifier instanceof PhoneNumberIdentifier) {
return new CommunicationIdentifierModel()
.setKind(CommunicationIdentifierKind.PHONE_NUMBER)
.setPhoneNumber(((PhoneNumberIdentifier) identifier).getPhoneNumber());
}

if (identifier instanceof MicrosoftTeamsUserIdentifier) {
return new CommunicationIdentifierModel()
.setKind(CommunicationIdentifierKind.MICROSOFT_TEAMS_USER)
.setMicrosoftTeamsUserId(((MicrosoftTeamsUserIdentifier) identifier).getUserId())
.setIsAnonymous(((MicrosoftTeamsUserIdentifier) identifier).isAnonymous());
}

return new CommunicationIdentifierModel()
.setKind(CommunicationIdentifierKind.UNKNOWN)
.setId(((UnknownIdentifier) identifier).getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*/
public class PhoneNumberIdentifier extends CommunicationIdentifier {

private final String value;
private final String phoneNumber;

/**
* 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"
* @throws IllegalArgumentException thrown if phoneNumber parameter fail the validation.
Expand All @@ -22,13 +22,13 @@ public PhoneNumberIdentifier(String phoneNumber) {
if (CoreUtils.isNullOrEmpty(phoneNumber)) {
throw new IllegalArgumentException("The initialization parameter [phoneNumber] cannot be null to empty.");
}
this.value = phoneNumber;
this.phoneNumber = phoneNumber;
}

/**
* @return the string identifier representing the object identity
*/
public String getValue() {
return value;
public String getPhoneNumber() {
return phoneNumber;
}
}
Loading

0 comments on commit f707bc7

Please sign in to comment.