From 24f1aabaf1e938cd9bfd18b41ac090abc1801b6d Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Wed, 9 Sep 2020 15:59:07 +0200 Subject: [PATCH 1/8] First experiment on Fluent API setup Create ServiceFactory class for providing services to SymphonyBdk entry point. Simplify code in SymphonyBdk Experimental: First implementing OBO call on StreamService. User can do an OBO authentication by calling SymphonyBdk#obo(Obo.Handle). This method will return an AuthSession which is an obo session. OboStreamService contains only OBO-enabled stream apis. User will do the OBO authentication out of scope of SymphonyBdk, then will pass the AuthSession which is returned by the previous step to the method to perform the call with oboSession. --- .../com/symphony/bdk/core/ServiceFactory.java | 61 ++++++++++++++ .../com/symphony/bdk/core/SymphonyBdk.java | 82 ++++++------------- .../core/service/stream/OboStreamService.java | 37 +++++++++ .../core/service/stream/StreamService.java | 17 +--- .../com/symphony/bdk/examples/AuthMain.java | 5 +- .../bdk/examples/UserExampleMain.java | 10 +++ 6 files changed, 141 insertions(+), 71 deletions(-) create mode 100644 symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java create mode 100644 symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java new file mode 100644 index 000000000..eef6aff74 --- /dev/null +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java @@ -0,0 +1,61 @@ +package com.symphony.bdk.core; + +import com.symphony.bdk.core.api.invoker.ApiClient; +import com.symphony.bdk.core.auth.AuthSession; +import com.symphony.bdk.core.config.model.BdkConfig; +import com.symphony.bdk.core.service.MessageService; +import com.symphony.bdk.core.service.SessionService; +import com.symphony.bdk.core.service.datafeed.DatafeedService; +import com.symphony.bdk.core.service.datafeed.DatafeedVersion; +import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV1; +import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV2; +import com.symphony.bdk.core.service.stream.StreamService; +import com.symphony.bdk.core.service.user.UserService; +import com.symphony.bdk.gen.api.DatafeedApi; +import com.symphony.bdk.gen.api.MessagesApi; +import com.symphony.bdk.gen.api.SessionApi; +import com.symphony.bdk.gen.api.StreamsApi; +import com.symphony.bdk.gen.api.UserApi; +import com.symphony.bdk.gen.api.UsersApi; + +class ServiceFactory { + + private final UserService userService; + private final StreamService streamService; + private final MessageService messageService; + private final SessionService sessionService; + private final DatafeedService datafeedService; + + public ServiceFactory(ApiClient podClient, ApiClient agentClient, AuthSession authSession, BdkConfig config) { + + this.userService = new UserService(new UserApi(podClient), new UsersApi(podClient), authSession); + this.streamService = new StreamService(new StreamsApi(podClient), authSession); + this.messageService = new MessageService(new MessagesApi(agentClient), authSession); + this.sessionService = new SessionService(new SessionApi(podClient)); + if (DatafeedVersion.of(config.getDatafeed().getVersion()) == DatafeedVersion.V2) { + this.datafeedService = new DatafeedServiceV2(new DatafeedApi(agentClient), authSession, config); + } else { + this.datafeedService = new DatafeedServiceV1(new DatafeedApi(agentClient), authSession, config); + } + } + + public UserService getUserService() { + return this.userService; + } + + public StreamService getStreamService() { + return this.streamService; + } + + public MessageService getMessageService() { + return this.messageService; + } + + public SessionService getSessionService() { + return this.sessionService; + } + + public DatafeedService getDatafeedService() { + return this.datafeedService; + } +} diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java index c3ae16094..06094f942 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java @@ -1,7 +1,6 @@ package com.symphony.bdk.core; import com.symphony.bdk.core.activity.ActivityRegistry; -import com.symphony.bdk.core.api.invoker.ApiClient; import com.symphony.bdk.core.auth.AuthSession; import com.symphony.bdk.core.auth.AuthenticatorFactory; import com.symphony.bdk.core.auth.OboAuthenticator; @@ -11,21 +10,9 @@ import com.symphony.bdk.core.config.model.BdkConfig; import com.symphony.bdk.core.service.MessageService; import com.symphony.bdk.core.service.Obo; -import com.symphony.bdk.core.service.SessionService; import com.symphony.bdk.core.service.datafeed.DatafeedService; -import com.symphony.bdk.core.service.datafeed.DatafeedVersion; -import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV1; -import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV2; import com.symphony.bdk.core.service.stream.StreamService; import com.symphony.bdk.core.service.user.UserService; -import com.symphony.bdk.gen.api.DatafeedApi; -import com.symphony.bdk.gen.api.MessagesApi; - -import com.symphony.bdk.gen.api.SessionApi; -import com.symphony.bdk.gen.api.StreamsApi; -import com.symphony.bdk.gen.api.UserApi; - -import com.symphony.bdk.gen.api.UsersApi; import lombok.extern.slf4j.Slf4j; import org.apiguardian.api.API; @@ -39,57 +26,35 @@ @API(status = API.Status.EXPERIMENTAL) public class SymphonyBdk { - private final ApiClientFactory apiClientFactory; - private final ApiClient podClient; - private final ApiClient agentClient; - - private final AuthSession botSession; private final OboAuthenticator oboAuthenticator; - private final DatafeedService datafeedService; - private final UserService userService; - private final StreamService streamService; - private final SessionService sessionService; + private final ServiceFactory serviceFactory; private final ActivityRegistry activityRegistry; public SymphonyBdk(BdkConfig config) throws AuthInitializationException, AuthUnauthorizedException { - this.apiClientFactory = new ApiClientFactory(config); - this.podClient = apiClientFactory.getPodClient(); - this.agentClient = apiClientFactory.getAgentClient(); - - final AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory(config, this.apiClientFactory); - - this.botSession = authenticatorFactory.getBotAuthenticator().authenticateBot(); + ApiClientFactory apiClientFactory = new ApiClientFactory(config); + final AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory(config, apiClientFactory); + AuthSession botSession = authenticatorFactory.getBotAuthenticator().authenticateBot(); this.oboAuthenticator = config.isOboConfigured() ? authenticatorFactory.getOboAuthenticator() : null; - - // setup the datafeed - final DatafeedApi datafeedApi = new DatafeedApi(this.agentClient); - if (DatafeedVersion.of(config.getDatafeed().getVersion()) == DatafeedVersion.V2) { - this.datafeedService = new DatafeedServiceV2(datafeedApi, this.botSession, config); - } else { - this.datafeedService = new DatafeedServiceV1(datafeedApi, this.botSession, config); - } - // setup other services - this.userService = new UserService(new UserApi(this.podClient), new UsersApi(this.podClient), this.botSession); - this.streamService = new StreamService(new StreamsApi(this.podClient), this.botSession); - this.sessionService = new SessionService(new SessionApi(this.podClient)); - this.activityRegistry = new ActivityRegistry(this.sessionService.getSession(this.botSession), this.datafeedService::subscribe); + this.serviceFactory = new ServiceFactory(apiClientFactory.getPodClient(), apiClientFactory.getAgentClient(), botSession, config); + // setup activities + this.activityRegistry = new ActivityRegistry(this.serviceFactory.getSessionService().getSession(botSession), this.serviceFactory.getDatafeedService()::subscribe); } public MessageService messages() { - return new MessageService(new MessagesApi(this.agentClient), this.botSession); + return this.serviceFactory.getMessageService(); } - public MessageService messages(Obo.Handle oboHandle) throws AuthUnauthorizedException { - AuthSession oboSession; - if (oboHandle.hasUsername()) { - oboSession = this.getOboAuthenticator().authenticateByUsername(oboHandle.getUsername()); - } else { - oboSession = this.getOboAuthenticator().authenticateByUserId(oboHandle.getUserId()); - } - return new MessageService(new MessagesApi(this.agentClient), oboSession); - } +// public MessageService messages(Obo.Handle oboHandle) throws AuthUnauthorizedException { +// AuthSession oboSession; +// if (oboHandle.hasUsername()) { +// oboSession = this.getOboAuthenticator().authenticateByUsername(oboHandle.getUsername()); +// } else { +// oboSession = this.getOboAuthenticator().authenticateByUserId(oboHandle.getUserId()); +// } +// return new MessageService(new MessagesApi(this.agentClient), oboSession); +// } /** * Get the {@link DatafeedService} from a Bdk entry point. @@ -98,7 +63,7 @@ public MessageService messages(Obo.Handle oboHandle) throws AuthUnauthorizedExce * @return {@link DatafeedService} datafeed service instance. */ public DatafeedService datafeed() { - return this.datafeedService; + return this.serviceFactory.getDatafeedService(); } /** @@ -107,7 +72,7 @@ public DatafeedService datafeed() { * @return {@link UserService} user service instance. */ public UserService users() { - return this.userService; + return this.serviceFactory.getUserService(); } /** @@ -116,7 +81,7 @@ public UserService users() { * @return {@link StreamService} user service instance. */ public StreamService streams() { - return this.streamService; + return this.serviceFactory.getStreamService(); } /** @@ -128,6 +93,13 @@ public ActivityRegistry activities() { return this.activityRegistry; } + public AuthSession obo(Obo.Handle oboHandle) throws AuthUnauthorizedException { + if (oboHandle.hasUsername()) { + return this.getOboAuthenticator().authenticateByUsername(oboHandle.getUsername()); + } + return this.getOboAuthenticator().authenticateByUserId(oboHandle.getUserId()); + } + protected OboAuthenticator getOboAuthenticator() { return Optional.ofNullable(this.oboAuthenticator) .orElseThrow(() -> new IllegalStateException("OBO is not configured.")); diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java new file mode 100644 index 000000000..3f134ec2b --- /dev/null +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java @@ -0,0 +1,37 @@ +package com.symphony.bdk.core.service.stream; + +import com.symphony.bdk.core.api.invoker.ApiException; +import com.symphony.bdk.core.api.invoker.ApiRuntimeException; +import com.symphony.bdk.core.auth.AuthSession; +import com.symphony.bdk.gen.api.StreamsApi; +import com.symphony.bdk.gen.api.model.StreamAttributes; +import com.symphony.bdk.gen.api.model.StreamFilter; +import com.symphony.bdk.gen.api.model.V2StreamAttributes; + +import java.util.List; + +class OboStreamService { + + protected final StreamsApi streamsApi; + + protected OboStreamService(StreamsApi streamsApi) { + this.streamsApi = streamsApi; + } + + public V2StreamAttributes getStreamInfo(String streamId, AuthSession oboSession) { + try { + return streamsApi.v2StreamsSidInfoGet(streamId, oboSession.getSessionToken()); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } + + public List listStreams(StreamFilter filter, AuthSession oboSession) { + try { + return streamsApi.v1StreamsListPost(oboSession.getSessionToken(), null, null, filter); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } + +} diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java index 4652ff097..d9c624d71 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java @@ -39,13 +39,12 @@ * */ @Slf4j -public class StreamService { +public class StreamService extends OboStreamService { - private final StreamsApi streamsApi; private final AuthSession authSession; public StreamService(StreamsApi streamsApi, AuthSession authSession) { - this.streamsApi = streamsApi; + super(streamsApi); this.authSession = authSession; } @@ -172,11 +171,7 @@ public V3RoomDetail updateRoom(String roomId, V3RoomAttributes roomAttributes) { * @see List Streams */ public List listStreams(StreamFilter filter) { - try { - return streamsApi.v1StreamsListPost(authSession.getSessionToken(), null, null, filter); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.listStreams(filter, this.authSession); } /** @@ -187,11 +182,7 @@ public List listStreams(StreamFilter filter) { * @see Stream Info V2 */ public V2StreamAttributes getStreamInfo(String streamId) { - try { - return streamsApi.v2StreamsSidInfoGet(streamId, authSession.getSessionToken()); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.getStreamInfo(streamId, this.authSession); } /** diff --git a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/AuthMain.java b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/AuthMain.java index 2a39aacd3..302f68f69 100644 --- a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/AuthMain.java +++ b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/AuthMain.java @@ -6,7 +6,6 @@ import com.symphony.bdk.core.auth.exception.AuthInitializationException; import com.symphony.bdk.core.auth.exception.AuthUnauthorizedException; import com.symphony.bdk.core.config.exception.BdkConfigException; -import com.symphony.bdk.core.service.Obo; import com.symphony.bdk.gen.api.model.V4Message; import lombok.extern.slf4j.Slf4j; @@ -30,7 +29,7 @@ public static void main(String[] args) throws BdkConfigException, AuthInitializa log.info("Regular message sent : {}", regularMessage.getMessageId()); // send OBO message - final V4Message oboMessage = bdk.messages(Obo.username("thibault.pensec")).send(STREAM, MESSAGE); - log.info("OBO message sent : {}", oboMessage.getMessageId()); +// final V4Message oboMessage = bdk.messages(Obo.username("thibault.pensec")).send(STREAM, MESSAGE); +// log.info("OBO message sent : {}", oboMessage.getMessageId()); } } diff --git a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java index 5ea7ce87a..468f5870a 100644 --- a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java +++ b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java @@ -3,7 +3,12 @@ import static com.symphony.bdk.core.config.BdkConfigLoader.loadFromSymphonyDir; import com.symphony.bdk.core.SymphonyBdk; +import com.symphony.bdk.core.auth.AuthSession; +import com.symphony.bdk.core.service.Obo; import com.symphony.bdk.core.service.user.constant.UserFeature; +import com.symphony.bdk.gen.api.model.StreamAttributes; +import com.symphony.bdk.gen.api.model.StreamFilter; +import com.symphony.bdk.gen.api.model.StreamType; import com.symphony.bdk.gen.api.model.UserFilter; import com.symphony.bdk.gen.api.model.V2UserDetail; @@ -31,5 +36,10 @@ public static void main(String[] args) throws Exception { log.info("Account type: {}", userDetailList.get(0).getUserAttributes().getAccountType()); log.info("Company: {}", userDetailList.get(0).getUserAttributes().getCompanyName()); log.info("Role: {}", userDetailList.get(0).getRoles()); + + AuthSession oboSession = bdk.obo(Obo.username("hong.le")); + StreamFilter streamFilter = new StreamFilter().addStreamTypesItem(new StreamType().type(StreamType.TypeEnum.IM)); + List streamsList = bdk.streams().listStreams(streamFilter, oboSession); + log.info("Streams List: {}", streamsList); } } From 45a3be00a167dbda545db38bb4aaf3f10eef9847 Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Thu, 10 Sep 2020 10:55:14 +0200 Subject: [PATCH 2/8] Update @symphony-thibault's comments --- .../java/com/symphony/bdk/core/SymphonyBdk.java | 13 ++++++------- .../bdk/core/service/stream/OboStreamService.java | 4 ++-- .../bdk/core/service/stream/StreamService.java | 4 ++-- .../com/symphony/bdk/core/ServiceFactoryTest.java | 4 ++++ .../com/symphony/bdk/examples/UserExampleMain.java | 5 ++--- 5 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java index 06094f942..ddcae8803 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java @@ -9,7 +9,6 @@ import com.symphony.bdk.core.client.ApiClientFactory; import com.symphony.bdk.core.config.model.BdkConfig; import com.symphony.bdk.core.service.MessageService; -import com.symphony.bdk.core.service.Obo; import com.symphony.bdk.core.service.datafeed.DatafeedService; import com.symphony.bdk.core.service.stream.StreamService; import com.symphony.bdk.core.service.user.UserService; @@ -32,7 +31,6 @@ public class SymphonyBdk { private final ActivityRegistry activityRegistry; public SymphonyBdk(BdkConfig config) throws AuthInitializationException, AuthUnauthorizedException { - ApiClientFactory apiClientFactory = new ApiClientFactory(config); final AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory(config, apiClientFactory); AuthSession botSession = authenticatorFactory.getBotAuthenticator().authenticateBot(); @@ -93,11 +91,12 @@ public ActivityRegistry activities() { return this.activityRegistry; } - public AuthSession obo(Obo.Handle oboHandle) throws AuthUnauthorizedException { - if (oboHandle.hasUsername()) { - return this.getOboAuthenticator().authenticateByUsername(oboHandle.getUsername()); - } - return this.getOboAuthenticator().authenticateByUserId(oboHandle.getUserId()); + public AuthSession obo(Long id) throws AuthUnauthorizedException { + return this.getOboAuthenticator().authenticateByUserId(id); + } + + public AuthSession obo(String username) throws AuthUnauthorizedException { + return this.getOboAuthenticator().authenticateByUsername(username); } protected OboAuthenticator getOboAuthenticator() { diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java index 3f134ec2b..b8016ab40 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java @@ -18,7 +18,7 @@ protected OboStreamService(StreamsApi streamsApi) { this.streamsApi = streamsApi; } - public V2StreamAttributes getStreamInfo(String streamId, AuthSession oboSession) { + public V2StreamAttributes getStreamInfo(AuthSession oboSession, String streamId) { try { return streamsApi.v2StreamsSidInfoGet(streamId, oboSession.getSessionToken()); } catch (ApiException apiException) { @@ -26,7 +26,7 @@ public V2StreamAttributes getStreamInfo(String streamId, AuthSession oboSession) } } - public List listStreams(StreamFilter filter, AuthSession oboSession) { + public List listStreams(AuthSession oboSession, StreamFilter filter) { try { return streamsApi.v1StreamsListPost(oboSession.getSessionToken(), null, null, filter); } catch (ApiException apiException) { diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java index d9c624d71..41cef9e8d 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/StreamService.java @@ -171,7 +171,7 @@ public V3RoomDetail updateRoom(String roomId, V3RoomAttributes roomAttributes) { * @see List Streams */ public List listStreams(StreamFilter filter) { - return this.listStreams(filter, this.authSession); + return this.listStreams(this.authSession, filter); } /** @@ -182,7 +182,7 @@ public List listStreams(StreamFilter filter) { * @see Stream Info V2 */ public V2StreamAttributes getStreamInfo(String streamId) { - return this.getStreamInfo(streamId, this.authSession); + return this.getStreamInfo(this.authSession, streamId); } /** diff --git a/symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java new file mode 100644 index 000000000..766efbda5 --- /dev/null +++ b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java @@ -0,0 +1,4 @@ +package com.symphony.bdk.core; + +public class ServiceFactoryTest { +} diff --git a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java index 468f5870a..8220c48bd 100644 --- a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java +++ b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/UserExampleMain.java @@ -4,7 +4,6 @@ import com.symphony.bdk.core.SymphonyBdk; import com.symphony.bdk.core.auth.AuthSession; -import com.symphony.bdk.core.service.Obo; import com.symphony.bdk.core.service.user.constant.UserFeature; import com.symphony.bdk.gen.api.model.StreamAttributes; import com.symphony.bdk.gen.api.model.StreamFilter; @@ -37,9 +36,9 @@ public static void main(String[] args) throws Exception { log.info("Company: {}", userDetailList.get(0).getUserAttributes().getCompanyName()); log.info("Role: {}", userDetailList.get(0).getRoles()); - AuthSession oboSession = bdk.obo(Obo.username("hong.le")); + AuthSession oboSession = bdk.obo("hong.le"); StreamFilter streamFilter = new StreamFilter().addStreamTypesItem(new StreamType().type(StreamType.TypeEnum.IM)); - List streamsList = bdk.streams().listStreams(streamFilter, oboSession); + List streamsList = bdk.streams().listStreams(oboSession, streamFilter); log.info("Streams List: {}", streamsList); } } From 9f52481f04a798b8bd1040bc94703d0901818c71 Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Thu, 10 Sep 2020 13:55:50 +0200 Subject: [PATCH 3/8] Update ServiceFactory --- .../com/symphony/bdk/core/ServiceFactory.java | 36 ++++----- .../com/symphony/bdk/core/SymphonyBdk.java | 28 +++++-- .../symphony/bdk/core/ServiceFactoryTest.java | 79 +++++++++++++++++++ 3 files changed, 116 insertions(+), 27 deletions(-) diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java index eef6aff74..795b6459a 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java @@ -20,42 +20,38 @@ class ServiceFactory { - private final UserService userService; - private final StreamService streamService; - private final MessageService messageService; - private final SessionService sessionService; - private final DatafeedService datafeedService; + private final ApiClient podClient; + private final ApiClient agentClient; + private final AuthSession authSession; + private final BdkConfig config; public ServiceFactory(ApiClient podClient, ApiClient agentClient, AuthSession authSession, BdkConfig config) { - - this.userService = new UserService(new UserApi(podClient), new UsersApi(podClient), authSession); - this.streamService = new StreamService(new StreamsApi(podClient), authSession); - this.messageService = new MessageService(new MessagesApi(agentClient), authSession); - this.sessionService = new SessionService(new SessionApi(podClient)); - if (DatafeedVersion.of(config.getDatafeed().getVersion()) == DatafeedVersion.V2) { - this.datafeedService = new DatafeedServiceV2(new DatafeedApi(agentClient), authSession, config); - } else { - this.datafeedService = new DatafeedServiceV1(new DatafeedApi(agentClient), authSession, config); - } + this.podClient = podClient; + this.agentClient = agentClient; + this.authSession = authSession; + this.config = config; } public UserService getUserService() { - return this.userService; + return new UserService(new UserApi(podClient), new UsersApi(podClient), authSession ); } public StreamService getStreamService() { - return this.streamService; + return new StreamService(new StreamsApi(podClient), authSession); } public MessageService getMessageService() { - return this.messageService; + return new MessageService(new MessagesApi(agentClient), authSession); } public SessionService getSessionService() { - return this.sessionService; + return new SessionService(new SessionApi(podClient)); } public DatafeedService getDatafeedService() { - return this.datafeedService; + if (DatafeedVersion.of(config.getDatafeed().getVersion()) == DatafeedVersion.V2) { + return new DatafeedServiceV2(new DatafeedApi(agentClient), authSession, config); + } + return new DatafeedServiceV1(new DatafeedApi(agentClient), authSession, config); } } diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java index ddcae8803..9a37d07d1 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java @@ -9,6 +9,7 @@ import com.symphony.bdk.core.client.ApiClientFactory; import com.symphony.bdk.core.config.model.BdkConfig; import com.symphony.bdk.core.service.MessageService; +import com.symphony.bdk.core.service.SessionService; import com.symphony.bdk.core.service.datafeed.DatafeedService; import com.symphony.bdk.core.service.stream.StreamService; import com.symphony.bdk.core.service.user.UserService; @@ -27,21 +28,34 @@ public class SymphonyBdk { private final OboAuthenticator oboAuthenticator; - private final ServiceFactory serviceFactory; private final ActivityRegistry activityRegistry; + private final SessionService sessionService; + private final StreamService streamService; + private final UserService userService; + private final MessageService messageService; + private final DatafeedService datafeedService; public SymphonyBdk(BdkConfig config) throws AuthInitializationException, AuthUnauthorizedException { ApiClientFactory apiClientFactory = new ApiClientFactory(config); final AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory(config, apiClientFactory); AuthSession botSession = authenticatorFactory.getBotAuthenticator().authenticateBot(); this.oboAuthenticator = config.isOboConfigured() ? authenticatorFactory.getOboAuthenticator() : null; - this.serviceFactory = new ServiceFactory(apiClientFactory.getPodClient(), apiClientFactory.getAgentClient(), botSession, config); + ServiceFactory serviceFactory = + new ServiceFactory(apiClientFactory.getPodClient(), apiClientFactory.getAgentClient(), botSession, config); + + // service init + this.sessionService = serviceFactory.getSessionService(); + this.userService = serviceFactory.getUserService(); + this.streamService = serviceFactory.getStreamService(); + this.messageService = serviceFactory.getMessageService(); + this.datafeedService = serviceFactory.getDatafeedService(); + // setup activities - this.activityRegistry = new ActivityRegistry(this.serviceFactory.getSessionService().getSession(botSession), this.serviceFactory.getDatafeedService()::subscribe); + this.activityRegistry = new ActivityRegistry(this.sessionService.getSession(botSession), this.datafeedService::subscribe); } public MessageService messages() { - return this.serviceFactory.getMessageService(); + return this.messageService; } // public MessageService messages(Obo.Handle oboHandle) throws AuthUnauthorizedException { @@ -61,7 +75,7 @@ public MessageService messages() { * @return {@link DatafeedService} datafeed service instance. */ public DatafeedService datafeed() { - return this.serviceFactory.getDatafeedService(); + return this.datafeedService; } /** @@ -70,7 +84,7 @@ public DatafeedService datafeed() { * @return {@link UserService} user service instance. */ public UserService users() { - return this.serviceFactory.getUserService(); + return this.userService; } /** @@ -79,7 +93,7 @@ public UserService users() { * @return {@link StreamService} user service instance. */ public StreamService streams() { - return this.serviceFactory.getStreamService(); + return this.streamService; } /** diff --git a/symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java index 766efbda5..8bae21b80 100644 --- a/symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java +++ b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/ServiceFactoryTest.java @@ -1,4 +1,83 @@ package com.symphony.bdk.core; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.mock; + +import com.symphony.bdk.core.api.invoker.ApiClient; +import com.symphony.bdk.core.auth.AuthSession; +import com.symphony.bdk.core.config.BdkConfigLoader; +import com.symphony.bdk.core.config.exception.BdkConfigException; +import com.symphony.bdk.core.config.model.BdkConfig; + +import com.symphony.bdk.core.config.model.BdkDatafeedConfig; +import com.symphony.bdk.core.service.MessageService; +import com.symphony.bdk.core.service.SessionService; +import com.symphony.bdk.core.service.datafeed.DatafeedService; +import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV1; +import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV2; +import com.symphony.bdk.core.service.stream.StreamService; +import com.symphony.bdk.core.service.user.UserService; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + public class ServiceFactoryTest { + + private ServiceFactory serviceFactory; + private ApiClient mPodClient; + private ApiClient mAgentClient; + private AuthSession mAuthSession; + private BdkConfig config; + + @BeforeEach + void setUp() throws BdkConfigException { + this.config = BdkConfigLoader.loadFromClasspath("/config/config.yaml"); + this.mAuthSession = mock(AuthSession.class); + this.mPodClient = mock(ApiClient.class); + this.mAgentClient = mock(ApiClient.class); + this.serviceFactory = new ServiceFactory(mPodClient, mAgentClient, mAuthSession, config); + } + + @Test + void getUserServiceTest() { + UserService userService = this.serviceFactory.getUserService(); + assertNotNull(userService); + } + + @Test + void getStreamServiceTest() { + StreamService streamService = this.serviceFactory.getStreamService(); + assertNotNull(streamService); + } + + @Test + void getSessionServiceTest() { + SessionService sessionService = this.serviceFactory.getSessionService(); + assertNotNull(sessionService); + } + + @Test + void getMessageServiceTest() { + MessageService messageService = this.serviceFactory.getMessageService(); + assertNotNull(messageService); + } + + @Test + void getDatafeedServiceTest() { + BdkDatafeedConfig datafeedConfig = this.config.getDatafeed(); + datafeedConfig.setVersion("v1"); + + this.serviceFactory = new ServiceFactory(mPodClient, mAgentClient, mAuthSession, config); + DatafeedService datafeedServiceV1 = this.serviceFactory.getDatafeedService(); + assertNotNull(datafeedServiceV1); + assertEquals(datafeedServiceV1.getClass(), DatafeedServiceV1.class); + + datafeedConfig.setVersion("v2"); + this.serviceFactory = new ServiceFactory(mPodClient, mAgentClient, mAuthSession, config); + DatafeedService datafeedServiceV2 = this.serviceFactory.getDatafeedService(); + assertNotNull(datafeedServiceV2); + assertEquals(datafeedServiceV2.getClass(), DatafeedServiceV2.class); + + } } From 632ca1d6c0bea2c795c612f12b6b1fedf924cd1a Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Thu, 10 Sep 2020 14:53:57 +0200 Subject: [PATCH 4/8] Update OBO for user Service Create OboUserService for calling user service methods using OBO Session Remove methods wrapping the deprecated user service API Remove redundant OBO class --- .../com/symphony/bdk/core/SymphonyBdk.java | 10 -- .../com/symphony/bdk/core/service/Obo.java | 57 -------- .../bdk/core/service/user/OboUserService.java | 88 ++++++++++++ .../bdk/core/service/user/UserService.java | 132 ++---------------- .../core/service/user/UserServiceTest.java | 47 ------- 5 files changed, 96 insertions(+), 238 deletions(-) delete mode 100644 symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/Obo.java create mode 100644 symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java index 9a37d07d1..05cd055b5 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java @@ -58,16 +58,6 @@ public MessageService messages() { return this.messageService; } -// public MessageService messages(Obo.Handle oboHandle) throws AuthUnauthorizedException { -// AuthSession oboSession; -// if (oboHandle.hasUsername()) { -// oboSession = this.getOboAuthenticator().authenticateByUsername(oboHandle.getUsername()); -// } else { -// oboSession = this.getOboAuthenticator().authenticateByUserId(oboHandle.getUserId()); -// } -// return new MessageService(new MessagesApi(this.agentClient), oboSession); -// } - /** * Get the {@link DatafeedService} from a Bdk entry point. * The returned datafeed service instance depends on the configuration of datafeed version. diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/Obo.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/Obo.java deleted file mode 100644 index 3e7b82fb5..000000000 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/Obo.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.symphony.bdk.core.service; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import org.apiguardian.api.API; - -/** - * - */ -@API(status = API.Status.EXPERIMENTAL) -public class Obo { - - /** - * - * @param username - * @return - */ - public static Handle username(String username) { - return new Handle(username, null); - } - - /** - * - * @param userId - * @return - */ - public static Handle userId(long userId) { - return new Handle(null, userId); - } - - /** - * - */ - @Getter - @AllArgsConstructor - @API(status = API.Status.EXPERIMENTAL) - public static class Handle { - - /** - * - */ - private final String username; - - /** - * - */ - private final Long userId; - - /** - * - * @return - */ - public boolean hasUsername() { - return this.username!= null; - } - } -} diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java new file mode 100644 index 000000000..a7fdc6412 --- /dev/null +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java @@ -0,0 +1,88 @@ +package com.symphony.bdk.core.service.user; + +import com.symphony.bdk.core.api.invoker.ApiException; +import com.symphony.bdk.core.api.invoker.ApiRuntimeException; +import com.symphony.bdk.core.auth.AuthSession; +import com.symphony.bdk.gen.api.UserApi; +import com.symphony.bdk.gen.api.UsersApi; +import com.symphony.bdk.gen.api.model.UserSearchQuery; +import com.symphony.bdk.gen.api.model.UserSearchResults; +import com.symphony.bdk.gen.api.model.UserV2; +import com.symphony.bdk.gen.api.model.V2UserList; + +import lombok.NonNull; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Nullable; + +class OboUserService { + + protected final UserApi userApi; + protected final UsersApi usersApi; + + protected OboUserService(UserApi userApi, UsersApi usersApi) { + this.userApi = userApi; + this.usersApi = usersApi; + } + + public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull List uidList, @NonNull Boolean local) { + try { + String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); + V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), uids, null, null, local); + return v2UserList.getUsers(); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } + + public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull List uidList) { + try { + String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); + V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), uids, null, null, false); + return v2UserList.getUsers(); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } + + public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull List emailList, @NonNull Boolean local) { + try { + String emails = String.join(",", emailList); + V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), null, emails, null, local); + return v2UserList.getUsers(); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } + + public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull List emailList) { + try { + String emails = String.join(",", emailList); + V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), null, emails, null, false); + return v2UserList.getUsers(); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } + + public List searchUserByUsernames(@NonNull AuthSession oboSession, @NonNull List usernameList) { + try { + String usernames = String.join(",", usernameList); + V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), null, null, usernames, true); + return v2UserList.getUsers(); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } + + public List searchUserBySearchQuery(@NonNull AuthSession oboSession, @NonNull UserSearchQuery query, @Nullable Boolean local) { + try { + UserSearchResults results = usersApi.v1UserSearchPost(oboSession.getSessionToken(), query, null, null, local); + return results.getUsers(); + } catch (ApiException apiException) { + throw new ApiRuntimeException(apiException); + } + } +} diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/UserService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/UserService.java index dddf21fd4..31373c226 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/UserService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/UserService.java @@ -16,11 +16,9 @@ import com.symphony.bdk.gen.api.model.UserDetail; import com.symphony.bdk.gen.api.model.UserFilter; import com.symphony.bdk.gen.api.model.UserSearchQuery; -import com.symphony.bdk.gen.api.model.UserSearchResults; import com.symphony.bdk.gen.api.model.UserStatus; import com.symphony.bdk.gen.api.model.UserV2; import com.symphony.bdk.gen.api.model.V2UserDetail; -import com.symphony.bdk.gen.api.model.V2UserList; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; @@ -50,15 +48,12 @@ * */ @Slf4j -public class UserService { +public class UserService extends OboUserService { - private final UserApi userApi; - private final UsersApi usersApi; private final AuthSession authSession; public UserService(UserApi userApi, UsersApi usersApi, AuthSession authSession) { - this.userApi = userApi; - this.usersApi = usersApi; + super(userApi, usersApi); this.authSession = authSession; } @@ -333,82 +328,6 @@ public void updateStatusOfUser(@NonNull Long uid, @NonNull UserStatus status) { } } - /** - * Get user by id - * - * @param uid User Id - * @param local If true then a local DB search will be performed and only local pod users will be - * returned. If absent or false then a directory search will be performed and users - * from other pods who are visible to the calling user will also be returned. - * @return User found by uid - */ - public UserV2 getUserById(@NonNull Long uid, @NonNull Boolean local) { - try { - return usersApi.v2UserGet(authSession.getSessionToken(), uid, null, null, local); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } - } - - /** - * Get user by id - * - * @param uid User Id - * @return User found by uid - */ - public UserV2 getUserById(@NonNull Long uid) { - try { - return usersApi.v2UserGet(authSession.getSessionToken(), uid, null, null, false); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } - } - - /** - * Get user by email - * - * @param email Email address of the user - * @param local If true then a local DB search will be performed and only local pod users will be - * returned. If absent or false then a directory search will be performed and users - * from other pods who are visible to the calling user will also be returned. - * @return User found by email - */ - public UserV2 getUserByEmail(@NonNull String email, @NonNull Boolean local) { - try { - return usersApi.v2UserGet(authSession.getSessionToken(), null, email, null, local); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } - } - - /** - * Get user by email - * - * @param email Email address of the user - * @return User found by email - */ - public UserV2 getUserByEmail(@NonNull String email) { - try { - return usersApi.v2UserGet(authSession.getSessionToken(), null, email, null, false); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } - } - - /** - * Get user within a local pod by username - * - * @param username Username of the user - * @return User found by username - */ - public UserV2 getUserByUsername(@NonNull String username) { - try { - return usersApi.v2UserGet(authSession.getSessionToken(), null, null, username, true); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } - } - /** * Search user by list of user ids * @@ -420,13 +339,7 @@ public UserV2 getUserByUsername(@NonNull String username) { * @see Users Lookup V3 */ public List searchUserByIds(@NonNull List uidList, @NonNull Boolean local) { - try { - String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); - V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), uids, null, null, local); - return v2UserList.getUsers(); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.searchUserByIds(this.authSession, uidList, local); } /** @@ -437,13 +350,7 @@ public List searchUserByIds(@NonNull List uidList, @NonNull Boolea * @see Users Lookup V3 */ public List searchUserByIds(@NonNull List uidList) { - try { - String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); - V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), uids, null, null, false); - return v2UserList.getUsers(); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.searchUserByIds(this.authSession, uidList); } /** @@ -457,13 +364,7 @@ public List searchUserByIds(@NonNull List uidList) { * @see Users Lookup V3 */ public List searchUserByEmails(@NonNull List emailList, @NonNull Boolean local) { - try { - String emails = String.join(",", emailList); - V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), null, emails, null, local); - return v2UserList.getUsers(); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.searchUserByEmails(this.authSession, emailList, local); } /** @@ -474,13 +375,7 @@ public List searchUserByEmails(@NonNull List emailList, @NonNull * @see Users Lookup V3 */ public List searchUserByEmails(@NonNull List emailList) { - try { - String emails = String.join(",", emailList); - V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), null, emails, null, false); - return v2UserList.getUsers(); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.searchUserByEmails(this.authSession, emailList); } /** @@ -491,13 +386,7 @@ public List searchUserByEmails(@NonNull List emailList) { * @see Users Lookup V3 */ public List searchUserByUsernames(@NonNull List usernameList) { - try { - String usernames = String.join(",", usernameList); - V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), null, null, usernames, true); - return v2UserList.getUsers(); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.searchUserByUsernames(this.authSession, usernameList); } /** @@ -511,11 +400,6 @@ public List searchUserByUsernames(@NonNull List usernameList) { * @see Search Users */ public List searchUserBySearchQuery(@NonNull UserSearchQuery query, @Nullable Boolean local) { - try { - UserSearchResults results = usersApi.v1UserSearchPost(authSession.getSessionToken(), query, null, null, local); - return results.getUsers(); - } catch (ApiException apiException) { - throw new ApiRuntimeException(apiException); - } + return this.searchUserBySearchQuery(this.authSession, query, local); } } diff --git a/symphony-bdk-core/src/test/java/com/symphony/bdk/core/service/user/UserServiceTest.java b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/service/user/UserServiceTest.java index 053aef37a..5d56bec50 100644 --- a/symphony-bdk-core/src/test/java/com/symphony/bdk/core/service/user/UserServiceTest.java +++ b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/service/user/UserServiceTest.java @@ -417,53 +417,6 @@ void updateStatusOfUserTestFailed(final BdkMockServer mockServer) { new UserStatus().status(UserStatus.StatusEnum.ENABLED))); } - @Test - void getUserV2Test(final BdkMockServer mockServer) throws IOException { - String response = JsonHelper.readFromClasspath("/user/user.json"); - mockServer.onGet(GET_USER_V2, res -> res.withBody(response)); - - UserV2 user1 = this.service.getUserById(1234L, true); - - assertEquals(user1.getId(), 1234L); - assertEquals(user1.getUsername(), "tibot"); - assertEquals(user1.getDisplayName(), "Test Bot"); - - UserV2 user2 = this.service.getUserByEmail("tibot@symphony.com", true); - - assertEquals(user2.getId(), 1234L); - assertEquals(user2.getUsername(), "tibot"); - assertEquals(user2.getDisplayName(), "Test Bot"); - - UserV2 user3 = this.service.getUserByUsername("tibot"); - - assertEquals(user3.getId(), 1234L); - assertEquals(user3.getUsername(), "tibot"); - assertEquals(user3.getDisplayName(), "Test Bot"); - - UserV2 user4 = this.service.getUserById(1234L); - - assertEquals(user4.getId(), 1234L); - assertEquals(user4.getUsername(), "tibot"); - assertEquals(user4.getDisplayName(), "Test Bot"); - - UserV2 user5 = this.service.getUserByEmail("tibot@symphony.com"); - - assertEquals(user5.getId(), 1234L); - assertEquals(user5.getUsername(), "tibot"); - assertEquals(user5.getDisplayName(), "Test Bot"); - } - - @Test - void getUserV2TestFailed(final BdkMockServer mockServer) { - mockServer.onGetFailed(400, GET_USER_V2, res -> res.withBody("{}")); - - assertThrows(ApiRuntimeException.class, () -> this.service.getUserById(1234L, true)); - assertThrows(ApiRuntimeException.class, () -> this.service.getUserByEmail("tibot@symphony.com", true)); - assertThrows(ApiRuntimeException.class, () -> this.service.getUserByUsername("tibot")); - assertThrows(ApiRuntimeException.class, () -> this.service.getUserById(1234L)); - assertThrows(ApiRuntimeException.class, () -> this.service.getUserByEmail("tibot@symphony.com")); - } - @Test void searchUserV3Test(final BdkMockServer mockServer) throws IOException { String response = JsonHelper.readFromClasspath("/user/users.json"); From 91c5b7695473f089b580e560033e4d718cee72fc Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Thu, 10 Sep 2020 15:31:26 +0200 Subject: [PATCH 5/8] Add javadoc --- .../com/symphony/bdk/core/SymphonyBdk.java | 12 ++++ .../core/service/stream/OboStreamService.java | 16 ++++++ .../bdk/core/service/user/OboUserService.java | 57 +++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java index 05cd055b5..97791aa7e 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java @@ -95,10 +95,22 @@ public ActivityRegistry activities() { return this.activityRegistry; } + /** + * OBO Authenticate by using user Id. + * + * @param id User id + * @return Obo authentication session + */ public AuthSession obo(Long id) throws AuthUnauthorizedException { return this.getOboAuthenticator().authenticateByUserId(id); } + /** + * OBO Authenticate by using username. + * + * @param username Username + * @return Obo authentication session + */ public AuthSession obo(String username) throws AuthUnauthorizedException { return this.getOboAuthenticator().authenticateByUsername(username); } diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java index b8016ab40..beebe3f00 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java @@ -18,6 +18,14 @@ protected OboStreamService(StreamsApi streamsApi) { this.streamsApi = streamsApi; } + /** + * {@link StreamService#getStreamInfo(String)} + * + * @param oboSession Obo Session + * @param streamId The stream id + * @return The information about the stream with the given id. + * @see Stream Info V2 + */ public V2StreamAttributes getStreamInfo(AuthSession oboSession, String streamId) { try { return streamsApi.v2StreamsSidInfoGet(streamId, oboSession.getSessionToken()); @@ -26,6 +34,14 @@ public V2StreamAttributes getStreamInfo(AuthSession oboSession, String streamId) } } + /** + * {@link StreamService#listStreams(StreamFilter)} + * + * @param oboSession Obo Session + * @param filter The stream searching criteria + * @return The list of streams retrieved according to the searching criteria. + * @see List Streams + */ public List listStreams(AuthSession oboSession, StreamFilter filter) { try { return streamsApi.v1StreamsListPost(oboSession.getSessionToken(), null, null, filter); diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java index a7fdc6412..84f8f5894 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java @@ -27,6 +27,17 @@ protected OboUserService(UserApi userApi, UsersApi usersApi) { this.usersApi = usersApi; } + /** + * {@link UserService#searchUserByIds(List, Boolean)} + * + * @param oboSession Obo Session + * @param uidList List of user ids + * @param local If true then a local DB search will be performed and only local pod users will be + * returned. If absent or false then a directory search will be performed and users + * from other pods who are visible to the calling user will also be returned. + * @return Users found by user ids + * @see Users Lookup V3 + */ public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull List uidList, @NonNull Boolean local) { try { String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); @@ -37,6 +48,14 @@ public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull Li } } + /** + * {@link UserService#searchUserByIds(List)} + * + * @param oboSession Obo Session + * @param uidList List of user ids + * @return Users found by user ids + * @see Users Lookup V3 + */ public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull List uidList) { try { String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); @@ -47,6 +66,17 @@ public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull Li } } + /** + * {@link UserService#searchUserByEmails(List, Boolean)} + * + * @param oboSession Obo Session + * @param emailList List of emails + * @param local If true then a local DB search will be performed and only local pod users will be + * returned. If absent or false then a directory search will be performed and users + * from other pods who are visible to the calling user will also be returned. + * @return Users found by emails. + * @see Users Lookup V3 + */ public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull List emailList, @NonNull Boolean local) { try { String emails = String.join(",", emailList); @@ -57,6 +87,14 @@ public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull } } + /** + * {@link UserService#searchUserByEmails(List)} + * + * @param oboSession Obo Session + * @param emailList List of emails + * @return Users found by emails + * @see Users Lookup V3 + */ public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull List emailList) { try { String emails = String.join(",", emailList); @@ -67,6 +105,14 @@ public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull } } + /** + * {@link UserService#searchUserByUsernames(List)} + * + * @param oboSession Obo Session + * @param usernameList List of usernames + * @return Users found by usernames + * @see Users Lookup V3 + */ public List searchUserByUsernames(@NonNull AuthSession oboSession, @NonNull List usernameList) { try { String usernames = String.join(",", usernameList); @@ -77,6 +123,17 @@ public List searchUserByUsernames(@NonNull AuthSession oboSession, @NonN } } + /** + * {@link UserService#searchUserBySearchQuery(UserSearchQuery, Boolean)} + * + * @param oboSession Obo Session + * @param query Searching query containing complicated information like title, location, company... + * @param local If true then a local DB search will be performed and only local pod users will be + * returned. If absent or false then a directory search will be performed and users + * from other pods who are visible to the calling user will also be returned. + * @return List of users found by query + * @see Search Users + */ public List searchUserBySearchQuery(@NonNull AuthSession oboSession, @NonNull UserSearchQuery query, @Nullable Boolean local) { try { UserSearchResults results = usersApi.v1UserSearchPost(oboSession.getSessionToken(), query, null, null, local); From 3604a1bf5a927b97f6c6ae2f0ca7b99cd425388b Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Fri, 11 Sep 2020 10:53:49 +0200 Subject: [PATCH 6/8] Update SymphonyBdk and add unittest --- symphony-bdk-core/pom.xml | 1 - .../com/symphony/bdk/core/SymphonyBdk.java | 24 ++++ .../core/service/stream/OboStreamService.java | 12 +- .../bdk/core/service/user/OboUserService.java | 60 +++++----- .../symphony/bdk/core/SymphonyBdkTest.java | 108 ++++++++++++++++++ 5 files changed, 168 insertions(+), 37 deletions(-) create mode 100644 symphony-bdk-core/src/test/java/com/symphony/bdk/core/SymphonyBdkTest.java diff --git a/symphony-bdk-core/pom.xml b/symphony-bdk-core/pom.xml index 5cd5d7ba3..33eb67f3d 100644 --- a/symphony-bdk-core/pom.xml +++ b/symphony-bdk-core/pom.xml @@ -241,7 +241,6 @@ com/symphony/bdk/core/**/model/*.class - com/symphony/bdk/core/SymphonyBdk.class com/symphony/bdk/core/service/MessageService.class com/symphony/bdk/core/service/Obo.class com/symphony/bdk/core/service/Obo$Handle.class diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java index 97791aa7e..30e465bdb 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java @@ -14,6 +14,7 @@ import com.symphony.bdk.core.service.stream.StreamService; import com.symphony.bdk.core.service.user.UserService; +import lombok.Generated; import lombok.extern.slf4j.Slf4j; import org.apiguardian.api.API; @@ -35,6 +36,7 @@ public class SymphonyBdk { private final MessageService messageService; private final DatafeedService datafeedService; + @Generated public SymphonyBdk(BdkConfig config) throws AuthInitializationException, AuthUnauthorizedException { ApiClientFactory apiClientFactory = new ApiClientFactory(config); final AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory(config, apiClientFactory); @@ -54,6 +56,28 @@ public SymphonyBdk(BdkConfig config) throws AuthInitializationException, AuthUna this.activityRegistry = new ActivityRegistry(this.sessionService.getSession(botSession), this.datafeedService::subscribe); } + protected SymphonyBdk(BdkConfig config, ServiceFactory serviceFactory, AuthenticatorFactory authenticatorFactory) + throws AuthInitializationException, AuthUnauthorizedException { + AuthSession botSession = authenticatorFactory.getBotAuthenticator().authenticateBot(); + this.oboAuthenticator = config.isOboConfigured() ? authenticatorFactory.getOboAuthenticator() : null; + + // service init + this.sessionService = serviceFactory.getSessionService(); + this.userService = serviceFactory.getUserService(); + this.streamService = serviceFactory.getStreamService(); + this.messageService = serviceFactory.getMessageService(); + this.datafeedService = serviceFactory.getDatafeedService(); + + // setup activities + this.activityRegistry = new ActivityRegistry(this.sessionService.getSession(botSession), this.datafeedService::subscribe); + } + + /** + * Get the {@link MessageService} from a Bdk entry point. + * The returned message service instance. + * + * @return {@link MessageService} message service instance. + */ public MessageService messages() { return this.messageService; } diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java index beebe3f00..433622213 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/stream/OboStreamService.java @@ -21,14 +21,14 @@ protected OboStreamService(StreamsApi streamsApi) { /** * {@link StreamService#getStreamInfo(String)} * - * @param oboSession Obo Session + * @param authSession Bot Session or Obo Session * @param streamId The stream id * @return The information about the stream with the given id. * @see Stream Info V2 */ - public V2StreamAttributes getStreamInfo(AuthSession oboSession, String streamId) { + public V2StreamAttributes getStreamInfo(AuthSession authSession, String streamId) { try { - return streamsApi.v2StreamsSidInfoGet(streamId, oboSession.getSessionToken()); + return streamsApi.v2StreamsSidInfoGet(streamId, authSession.getSessionToken()); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); } @@ -37,14 +37,14 @@ public V2StreamAttributes getStreamInfo(AuthSession oboSession, String streamId) /** * {@link StreamService#listStreams(StreamFilter)} * - * @param oboSession Obo Session + * @param authSession Bot Session or Obo Session * @param filter The stream searching criteria * @return The list of streams retrieved according to the searching criteria. * @see List Streams */ - public List listStreams(AuthSession oboSession, StreamFilter filter) { + public List listStreams(AuthSession authSession, StreamFilter filter) { try { - return streamsApi.v1StreamsListPost(oboSession.getSessionToken(), null, null, filter); + return streamsApi.v1StreamsListPost(authSession.getSessionToken(), null, null, filter); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); } diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java index 84f8f5894..71419dd85 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/user/OboUserService.java @@ -30,18 +30,18 @@ protected OboUserService(UserApi userApi, UsersApi usersApi) { /** * {@link UserService#searchUserByIds(List, Boolean)} * - * @param oboSession Obo Session - * @param uidList List of user ids - * @param local If true then a local DB search will be performed and only local pod users will be - * returned. If absent or false then a directory search will be performed and users - * from other pods who are visible to the calling user will also be returned. - * @return Users found by user ids - * @see Users Lookup V3 + * @param authSession Bot session or Obo Session + * @param uidList List of user ids + * @param local If true then a local DB search will be performed and only local pod users will be + * returned. If absent or false then a directory search will be performed and users + * from other pods who are visible to the calling user will also be returned. + * @return Users found by user ids + * @see Users Lookup V3 */ - public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull List uidList, @NonNull Boolean local) { + public List searchUserByIds(@NonNull AuthSession authSession, @NonNull List uidList, @NonNull Boolean local) { try { String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); - V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), uids, null, null, local); + V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), uids, null, null, local); return v2UserList.getUsers(); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); @@ -51,15 +51,15 @@ public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull Li /** * {@link UserService#searchUserByIds(List)} * - * @param oboSession Obo Session + * @param authSession Bot Session or Obo Session * @param uidList List of user ids * @return Users found by user ids * @see Users Lookup V3 */ - public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull List uidList) { + public List searchUserByIds(@NonNull AuthSession authSession, @NonNull List uidList) { try { String uids = uidList.stream().map(String::valueOf).collect(Collectors.joining(",")); - V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), uids, null, null, false); + V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), uids, null, null, false); return v2UserList.getUsers(); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); @@ -69,7 +69,7 @@ public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull Li /** * {@link UserService#searchUserByEmails(List, Boolean)} * - * @param oboSession Obo Session + * @param authSession Bot Session or Obo Session * @param emailList List of emails * @param local If true then a local DB search will be performed and only local pod users will be * returned. If absent or false then a directory search will be performed and users @@ -77,10 +77,10 @@ public List searchUserByIds(@NonNull AuthSession oboSession, @NonNull Li * @return Users found by emails. * @see Users Lookup V3 */ - public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull List emailList, @NonNull Boolean local) { + public List searchUserByEmails(@NonNull AuthSession authSession, @NonNull List emailList, @NonNull Boolean local) { try { String emails = String.join(",", emailList); - V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), null, emails, null, local); + V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), null, emails, null, local); return v2UserList.getUsers(); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); @@ -90,15 +90,15 @@ public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull /** * {@link UserService#searchUserByEmails(List)} * - * @param oboSession Obo Session + * @param authSession Bot Session or Obo Session * @param emailList List of emails * @return Users found by emails * @see Users Lookup V3 */ - public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull List emailList) { + public List searchUserByEmails(@NonNull AuthSession authSession, @NonNull List emailList) { try { String emails = String.join(",", emailList); - V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), null, emails, null, false); + V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), null, emails, null, false); return v2UserList.getUsers(); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); @@ -108,15 +108,15 @@ public List searchUserByEmails(@NonNull AuthSession oboSession, @NonNull /** * {@link UserService#searchUserByUsernames(List)} * - * @param oboSession Obo Session + * @param authSession Bot Session or Obo Session * @param usernameList List of usernames * @return Users found by usernames * @see Users Lookup V3 */ - public List searchUserByUsernames(@NonNull AuthSession oboSession, @NonNull List usernameList) { + public List searchUserByUsernames(@NonNull AuthSession authSession, @NonNull List usernameList) { try { String usernames = String.join(",", usernameList); - V2UserList v2UserList = usersApi.v3UsersGet(oboSession.getSessionToken(), null, null, usernames, true); + V2UserList v2UserList = usersApi.v3UsersGet(authSession.getSessionToken(), null, null, usernames, true); return v2UserList.getUsers(); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); @@ -126,17 +126,17 @@ public List searchUserByUsernames(@NonNull AuthSession oboSession, @NonN /** * {@link UserService#searchUserBySearchQuery(UserSearchQuery, Boolean)} * - * @param oboSession Obo Session - * @param query Searching query containing complicated information like title, location, company... - * @param local If true then a local DB search will be performed and only local pod users will be - * returned. If absent or false then a directory search will be performed and users - * from other pods who are visible to the calling user will also be returned. - * @return List of users found by query - * @see Search Users + * @param authSession Bot Session or Obo Session + * @param query Searching query containing complicated information like title, location, company... + * @param local If true then a local DB search will be performed and only local pod users will be + * returned. If absent or false then a directory search will be performed and users + * from other pods who are visible to the calling user will also be returned. + * @return List of users found by query + * @see Search Users */ - public List searchUserBySearchQuery(@NonNull AuthSession oboSession, @NonNull UserSearchQuery query, @Nullable Boolean local) { + public List searchUserBySearchQuery(@NonNull AuthSession authSession, @NonNull UserSearchQuery query, @Nullable Boolean local) { try { - UserSearchResults results = usersApi.v1UserSearchPost(oboSession.getSessionToken(), query, null, null, local); + UserSearchResults results = usersApi.v1UserSearchPost(authSession.getSessionToken(), query, null, null, local); return results.getUsers(); } catch (ApiException apiException) { throw new ApiRuntimeException(apiException); diff --git a/symphony-bdk-core/src/test/java/com/symphony/bdk/core/SymphonyBdkTest.java b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/SymphonyBdkTest.java new file mode 100644 index 000000000..fb979b069 --- /dev/null +++ b/symphony-bdk-core/src/test/java/com/symphony/bdk/core/SymphonyBdkTest.java @@ -0,0 +1,108 @@ +package com.symphony.bdk.core; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import com.symphony.bdk.core.activity.ActivityRegistry; +import com.symphony.bdk.core.auth.AuthSession; +import com.symphony.bdk.core.auth.AuthenticatorFactory; +import com.symphony.bdk.core.auth.BotAuthenticator; +import com.symphony.bdk.core.auth.OboAuthenticator; +import com.symphony.bdk.core.auth.exception.AuthInitializationException; +import com.symphony.bdk.core.auth.exception.AuthUnauthorizedException; +import com.symphony.bdk.core.client.ApiClientFactory; +import com.symphony.bdk.core.config.BdkConfigLoader; +import com.symphony.bdk.core.config.exception.BdkConfigException; +import com.symphony.bdk.core.config.model.BdkConfig; +import com.symphony.bdk.core.service.MessageService; +import com.symphony.bdk.core.service.SessionService; +import com.symphony.bdk.core.service.datafeed.DatafeedService; +import com.symphony.bdk.core.service.datafeed.impl.DatafeedServiceV1; +import com.symphony.bdk.core.service.stream.StreamService; +import com.symphony.bdk.core.service.user.UserService; +import com.symphony.bdk.gen.api.model.UserV2; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class SymphonyBdkTest { + + private SymphonyBdk symphonyBdk; + + @BeforeEach + void setUp() throws BdkConfigException, AuthUnauthorizedException, AuthInitializationException { + BdkConfig config = BdkConfigLoader.loadFromClasspath("/config/config.yaml"); + ApiClientFactory apiClientFactory = new ApiClientFactory(config); + + AuthSession authSession = mock(AuthSession.class); + doReturn("1234").when(authSession).getSessionToken(); + doReturn("1234").when(authSession).getKeyManagerToken(); + + BotAuthenticator botAuthenticator = mock(BotAuthenticator.class); + doReturn(authSession).when(botAuthenticator).authenticateBot(); + + OboAuthenticator oboAuthenticator = mock(OboAuthenticator.class); + doReturn(authSession).when(oboAuthenticator).authenticateByUsername(anyString()); + doReturn(authSession).when(oboAuthenticator).authenticateByUserId(anyLong()); + + AuthenticatorFactory authenticatorFactory = mock(AuthenticatorFactory.class); + doReturn(botAuthenticator).when(authenticatorFactory).getBotAuthenticator(); + doReturn(oboAuthenticator).when(authenticatorFactory).getOboAuthenticator(); + + ServiceFactory serviceFactory = new ServiceFactory(apiClientFactory.getPodClient(), apiClientFactory.getAgentClient(), authSession, config); + ServiceFactory spiedServiceFactory = spy(serviceFactory); + SessionService sessionService = mock(SessionService.class); + doReturn(new UserV2().id(123L)).when(sessionService).getSession(authSession); + doReturn(sessionService).when(spiedServiceFactory).getSessionService(); + + this.symphonyBdk = new SymphonyBdk(config, spiedServiceFactory, authenticatorFactory); + } + + @Test + void getDatafeedServiceTest() { + DatafeedService datafeedService = this.symphonyBdk.datafeed(); + + assertNotNull(datafeedService); + assertEquals(datafeedService.getClass(), DatafeedServiceV1.class); + } + + @Test + void getStreamServiceTest() { + StreamService streamService = this.symphonyBdk.streams(); + assertNotNull(streamService); + } + + @Test + void getUserServiceTest() { + UserService userService = this.symphonyBdk.users(); + assertNotNull(userService); + } + + @Test + void getActivitiesTest() { + ActivityRegistry registry = this.symphonyBdk.activities(); + assertNotNull(registry); + } + + @Test + void getMessageServiceTest() { + MessageService messageService = this.symphonyBdk.messages(); + assertNotNull(messageService); + } + + @Test + void oboAuthenticateTest() throws AuthUnauthorizedException { + AuthSession authSessionById = this.symphonyBdk.obo(123456L); + assertEquals(authSessionById.getSessionToken(), "1234"); + assertEquals(authSessionById.getKeyManagerToken(), "1234"); + + AuthSession authSessionByUsername = this.symphonyBdk.obo("username"); + assertEquals(authSessionByUsername.getKeyManagerToken(), "1234"); + assertEquals(authSessionByUsername.getSessionToken(), "1234"); + } +} From c4ba6326630273becb2ce72351572146db96d746 Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Fri, 11 Sep 2020 14:00:38 +0200 Subject: [PATCH 7/8] Update documentation for fluent api --- docs/fluent.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 docs/fluent.md diff --git a/docs/fluent.md b/docs/fluent.md new file mode 100644 index 000000000..ec174ee71 --- /dev/null +++ b/docs/fluent.md @@ -0,0 +1,70 @@ +# Fluent API + +The Fluent API is the most basic component of the BDK. This component provides the developers very quickly an entry point +to discover all the features of the BDK, helps them to easily understand how to make a bot interacting with the +Symphony platform. + +## SymphonyBdk + +The heart of the Fluent API is the [`SymphonyBdk`](../symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java). +This component is an entry point for a developer to go through all the features of the BDK. A `SymphonyBdk` object is +built from the information extracting from the BDK configuration file. + +```java +public class Example { + + public static void main(String[] args) { + //Init the BDK entry point + final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); + } +} +``` + + +## Using BDK services from SymphonyBdk + +Once the `SymphonyBdk` instance is created, the bot is automatically authenticated and all the BDK services will be available +for developers to use. + +```java +public class Example { + + public static void main(String[] args) { + // Init the BDK entry point + final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); + // Using users service + final List userDetails = bdk.users().listUsersDetail(new UserFilter()); + // Using datafeed service + bdk.datafeed().start(); + } +} +``` + +Developers can use the services provided by the BDK by calling the method with the name of the service. For the moment, the services +that is available in BDK is: + +- Users Service: `bdk.users()` +- Streams Service: `bdk.streams()` +- Message Service: `bdk.message()` +- Datafeed Service: `bdk.datafeed()` + + +## OBO authenticating using SymphonyBdk + +On Behalf Of (OBO) is a pattern that enables developers to perform operations on behalf of a Symphony end-user. [`Getting started with OBO](https://developers.symphony.com/restapi/docs/get-started-with-obo) +With a SymphonyBdk instance, we can easily authenticate the bot in OBO mode: + +```java +public class Example { + + public static void main(String[] args) { + // Init the BDK entry point + final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); + // OBO authentication + AuthSession oboSession = bdk.obo("user.name"); + // Running service in Obo mode + final List userDetails = bdk.users().listUsersDetail(new UserFilter(), oboSession); + } +} + +``` From 1feb0015112336e3ced4b9e86efb773886833e7d Mon Sep 17 00:00:00 2001 From: symphony-hong Date: Fri, 11 Sep 2020 15:41:46 +0200 Subject: [PATCH 8/8] Update documentation --- docs/authentication.md | 25 +++++++++++- docs/fluent.md | 37 +++++------------- symphony-bdk-core/pom.xml | 4 -- .../com/symphony/bdk/core/ServiceFactory.java | 39 +++++++++++++++++++ 4 files changed, 72 insertions(+), 33 deletions(-) diff --git a/docs/authentication.md b/docs/authentication.md index 31cb18cd5..65c42e11e 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -1,3 +1,26 @@ # Authentication -To be done... \ No newline at end of file +## Bot authentication + +To be updated... + +## OBO authenticating using SymphonyBdk + +On Behalf Of (OBO) is a pattern that enables developers to perform operations on behalf of a Symphony end-user. [`Getting started with OBO](https://developers.symphony.com/restapi/docs/get-started-with-obo) + +With a [`SymphonyBdk`](../symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java) instance, we can easily authenticate the bot in OBO mode: + +```java +public class Example { + + public static void main(String[] args) { + // Initialize the BDK entry point + final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("config.yaml")); + // OBO authentication + AuthSession oboSession = bdk.obo("user.name"); + // Running service in Obo mode + final List userDetails = bdk.users().listUsersDetail(new UserFilter(), oboSession); + } +} + +``` diff --git a/docs/fluent.md b/docs/fluent.md index ec174ee71..899f5f746 100644 --- a/docs/fluent.md +++ b/docs/fluent.md @@ -1,21 +1,21 @@ # Fluent API -The Fluent API is the most basic component of the BDK. This component provides the developers very quickly an entry point -to discover all the features of the BDK, helps them to easily understand how to make a bot interacting with the +The Fluent API is the most basic feature of the BDK. This component provides the developers very quickly an entry point +to discover all others features of the BDK, helps them to easily understand how to make a bot interacting with the Symphony platform. ## SymphonyBdk The heart of the Fluent API is the [`SymphonyBdk`](../symphony-bdk-core/src/main/java/com/symphony/bdk/core/SymphonyBdk.java). This component is an entry point for a developer to go through all the features of the BDK. A `SymphonyBdk` object is -built from the information extracting from the BDK configuration file. +built from the information extracted from the BDK configuration file. ```java public class Example { public static void main(String[] args) { - //Init the BDK entry point - final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); + // Initialize the BDK entry point + final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("config.yaml")); } } ``` @@ -30,8 +30,8 @@ for developers to use. public class Example { public static void main(String[] args) { - // Init the BDK entry point - final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); + // Initialize the BDK entry point + final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("config.yaml")); // Using users service final List userDetails = bdk.users().listUsersDetail(new UserFilter()); // Using datafeed service @@ -45,26 +45,7 @@ that is available in BDK is: - Users Service: `bdk.users()` - Streams Service: `bdk.streams()` -- Message Service: `bdk.message()` +- Message Service: `bdk.messages()` - Datafeed Service: `bdk.datafeed()` +- Activities Registry: `bdk.activities()` - -## OBO authenticating using SymphonyBdk - -On Behalf Of (OBO) is a pattern that enables developers to perform operations on behalf of a Symphony end-user. [`Getting started with OBO](https://developers.symphony.com/restapi/docs/get-started-with-obo) -With a SymphonyBdk instance, we can easily authenticate the bot in OBO mode: - -```java -public class Example { - - public static void main(String[] args) { - // Init the BDK entry point - final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); - // OBO authentication - AuthSession oboSession = bdk.obo("user.name"); - // Running service in Obo mode - final List userDetails = bdk.users().listUsersDetail(new UserFilter(), oboSession); - } -} - -``` diff --git a/symphony-bdk-core/pom.xml b/symphony-bdk-core/pom.xml index 59d97244d..1cf03825c 100644 --- a/symphony-bdk-core/pom.xml +++ b/symphony-bdk-core/pom.xml @@ -250,10 +250,6 @@ com/symphony/bdk/core/**/*Exception.class com/symphony/bdk/core/**/model/*.class - - com/symphony/bdk/core/service/Obo.class - com/symphony/bdk/core/service/Obo$Handle.class - com/symphony/bdk/gen/**/* diff --git a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java index 3a8135113..e48f55889 100644 --- a/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java +++ b/symphony-bdk-core/src/main/java/com/symphony/bdk/core/ServiceFactory.java @@ -23,6 +23,20 @@ import com.symphony.bdk.gen.api.UserApi; import com.symphony.bdk.gen.api.UsersApi; +import org.apiguardian.api.API; + +/** + * Factory responsible for creating BDK service instances for Symphony Bdk entry point. + * : + *
    + *
  • {@link UserService}
  • + *
  • {@link StreamService}
  • + *
  • {@link MessageService}
  • + *
  • {@link DatafeedService}
  • + *
  • {@link SessionService}
  • + *
+ */ +@API(status = API.Status.INTERNAL) class ServiceFactory { private final ApiClient podClient; @@ -37,18 +51,38 @@ public ServiceFactory(ApiClient podClient, ApiClient agentClient, AuthSession au this.config = config; } + /** + * Returns a fully initialized {@link UserService}. + * + * @return an new {@link UserService} instance. + */ public UserService getUserService() { return new UserService(new UserApi(podClient), new UsersApi(podClient), authSession ); } + /** + * Returns a fully initialized {@link StreamService}. + * + * @return an new {@link StreamService} instance. + */ public StreamService getStreamService() { return new StreamService(new StreamsApi(podClient), authSession); } + /** + * Returns a fully initialized {@link SessionService}. + * + * @return an new {@link SessionService} instance. + */ public SessionService getSessionService() { return new SessionService(new SessionApi(podClient)); } + /** + * Returns a fully initialized {@link DatafeedService}. + * + * @return an new {@link DatafeedService} instance. + */ public DatafeedService getDatafeedService() { if (DatafeedVersion.of(config.getDatafeed().getVersion()) == DatafeedVersion.V2) { return new DatafeedServiceV2(new DatafeedApi(agentClient), authSession, config); @@ -56,6 +90,11 @@ public DatafeedService getDatafeedService() { return new DatafeedServiceV1(new DatafeedApi(agentClient), authSession, config); } + /** + * Returns a fully initialized {@link MessageService}. + * + * @return an new {@link MessageService} instance. + */ public MessageService getMessageService() { return new MessageService(new MessagesApi(this.agentClient), new MessageApi(this.podClient), new MessageSuppressionApi(this.podClient), new StreamsApi(this.podClient), new PodApi(this.podClient),