From 527477c6142b06d97e55fb15637bf1ebc556a7a1 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Wed, 19 Dec 2018 21:13:24 +0100 Subject: [PATCH 01/22] add retrofit with failing import --- pom.xml | 11 +++++++++++ .../internal/DiscourseUserSyncConnector.java | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 4338272..802ba01 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,10 @@ tmortagne Thomas Mortagne + + martindelille + Martin Delille + usersync-api @@ -85,4 +89,11 @@ + + + com.squareup.retrofit2 + retrofit + 2.5.0 + + diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 13ff277..56983c2 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -29,9 +29,11 @@ import com.xpn.xwiki.objects.BaseObject; +import com.retrofit.Builder; + /** * {@link UserSyncConnector} implementation for Discourse. - * + * * @version $Id$ */ @Component @@ -40,7 +42,7 @@ public class DiscourseUserSyncConnector implements UserSyncConnector { private static final String PREFIX_CONFIGURATION = "usersync.discourse."; - + private static final String CONFIGURATION_URL = PREFIX_CONFIGURATION + "url"; @Inject From 189063045d8429abd8c346002ff6563bb203993c Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Wed, 19 Dec 2018 22:15:50 +0100 Subject: [PATCH 02/22] fix import --- .../usersync/discourse/internal/DiscourseUserSyncConnector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 56983c2..9087fd0 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -29,7 +29,7 @@ import com.xpn.xwiki.objects.BaseObject; -import com.retrofit.Builder; +import retrofit2.Retrofit.Builder; /** * {@link UserSyncConnector} implementation for Discourse. From 92c260f3bc308c8c26190be7a4c2a21b93783765 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Fri, 21 Dec 2018 15:30:31 +0100 Subject: [PATCH 03/22] first compiling draft --- pom.xml | 5 +++++ .../internal/DiscourseUserSyncConnector.java | 20 ++++++++++++++++++- .../DiscourseUserSyncConnectorTest.java | 6 ++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 802ba01..243df1d 100644 --- a/pom.xml +++ b/pom.xml @@ -95,5 +95,10 @@ retrofit 2.5.0 + + org.simpleframework + simple-xml + 2.7.1 + diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 9087fd0..484495d 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -19,6 +19,8 @@ */ package org.xwiki.contrib.usersync.discourse.internal; +import java.util.List; + import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; @@ -29,7 +31,11 @@ import com.xpn.xwiki.objects.BaseObject; +import retrofit2.Retrofit; import retrofit2.Retrofit.Builder; +import retrofit2.Call; + +import org.xwiki.contrib.usersync.discourse.internal.DiscourseService; /** * {@link UserSyncConnector} implementation for Discourse. @@ -44,6 +50,7 @@ public class DiscourseUserSyncConnector implements UserSyncConnector private static final String PREFIX_CONFIGURATION = "usersync.discourse."; private static final String CONFIGURATION_URL = PREFIX_CONFIGURATION + "url"; + private static final String API_KEY = PREFIX_CONFIGURATION + "api_key"; @Inject private ConfigurationSource configuration; @@ -59,8 +66,19 @@ public void createUser(BaseObject user) // Get the URL of the discourse server to synchronize with String discourseURL = this.configuration.getProperty(CONFIGURATION_URL); + String discourseApiKey = this.configuration.getProperty(API_KEY); - // TODO + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("https://api.github.com/") + .build(); + + DiscourseService service = retrofit.create(DiscourseService.class); + Call> repos = service.listRepos("martindelille"); + + + System.out.println("url"); + System.out.println(discourseURL); + System.out.println("end"); } @Override diff --git a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java index 079eebe..c4c0f75 100644 --- a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java +++ b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java @@ -67,6 +67,12 @@ public void before() } @Test + public void createUser() throws ComponentLookupException + { + // Call the component + this.mocker.getComponentUnderTest().createUser(this.newUser); + } + public void modifyUser() throws ComponentLookupException { // Modify the user From efdb8c8d69f73b73aac173adf5d0ac489e559e22 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Fri, 21 Dec 2018 15:49:26 +0100 Subject: [PATCH 04/22] add missing files --- .../discourse/internal/DiscourseService.java | 15 +++++++++++++++ .../contrib/usersync/discourse/internal/Repo.java | 13 +++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java new file mode 100644 index 0000000..5e0ddc9 --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java @@ -0,0 +1,15 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +import java.util.List; +import org.simpleframework.xml.Path; +import retrofit2.Call; +import retrofit2.http.GET; + +import org.xwiki.contrib.usersync.discourse.internal.Repo; + +public interface DiscourseService { + //@GET("admin/users/list/active.json") + @GET("users/{user}/repos") + Call> listRepos(@Path("user") String user); +} + diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java new file mode 100644 index 0000000..87ea3f3 --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java @@ -0,0 +1,13 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +public class Repo { + String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} From bb84a692a4ea1ebce3959b36a75c19c8a90818ca Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Sun, 23 Dec 2018 15:41:11 +0100 Subject: [PATCH 05/22] test new tutorial --- pom.xml | 12 ++++++++ .../discourse/internal/DiscourseService.java | 9 +++--- .../internal/DiscourseUserSyncConnector.java | 6 ++-- .../usersync/discourse/internal/Post.java | 30 +++++++++++++++++++ .../usersync/discourse/internal/Repo.java | 13 -------- 5 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java delete mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java diff --git a/pom.xml b/pom.xml index 243df1d..e8fc762 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,13 @@ + + central + http://repo1.maven.org/maven2 + + false + + xwiki-releases XWiki Nexus Releases Repository Proxy @@ -95,6 +102,11 @@ retrofit 2.5.0 + + com.squareup.retrofit2 + converter-gson + latest.version + org.simpleframework simple-xml diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java index 5e0ddc9..d232c6a 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java @@ -1,15 +1,14 @@ package org.xwiki.contrib.usersync.discourse.internal; import java.util.List; -import org.simpleframework.xml.Path; +//import org.simpleframework.xml.Path; import retrofit2.Call; import retrofit2.http.GET; -import org.xwiki.contrib.usersync.discourse.internal.Repo; +import org.xwiki.contrib.usersync.discourse.internal.Post; public interface DiscourseService { - //@GET("admin/users/list/active.json") - @GET("users/{user}/repos") - Call> listRepos(@Path("user") String user); + @GET("posts") + Call> getPosts(); } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 484495d..5b98b6c 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -34,6 +34,7 @@ import retrofit2.Retrofit; import retrofit2.Retrofit.Builder; import retrofit2.Call; +import retrofit2.converter.gson.GsonConverterFactory; import org.xwiki.contrib.usersync.discourse.internal.DiscourseService; @@ -69,11 +70,12 @@ public void createUser(BaseObject user) String discourseApiKey = this.configuration.getProperty(API_KEY); Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") + .baseUrl("https://jsonplaceholder.typicode.com/") + .addConvertFactory(GsonConverterFactory.create()) .build(); DiscourseService service = retrofit.create(DiscourseService.class); - Call> repos = service.listRepos("martindelille"); + Call> posts = service.getPosts(); System.out.println("url"); diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java new file mode 100644 index 0000000..ba77da8 --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java @@ -0,0 +1,30 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +import com.google.json.annotation.SerializedName; + +public class Post { + private int userId; + + private int id; + + private String title; + + @SerializedName("body") + private String text; + + public int getUserId() { + return d; + } + + public int getId() { + return id; + } + + public String getTitle() { + return title; + } + + public String getText() { + return text; + } +} diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java deleted file mode 100644 index 87ea3f3..0000000 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Repo.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.xwiki.contrib.usersync.discourse.internal; - -public class Repo { - String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} From 8b2b479094690a0095fd9987d1ea71bc142e217a Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Sun, 23 Dec 2018 15:43:27 +0100 Subject: [PATCH 06/22] remove simpleframework dependency --- pom.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pom.xml b/pom.xml index e8fc762..21b2d06 100644 --- a/pom.xml +++ b/pom.xml @@ -107,10 +107,5 @@ converter-gson latest.version - - org.simpleframework - simple-xml - 2.7.1 - From 009937362e028d0ae833d9013938699c006e5421 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Sun, 23 Dec 2018 19:31:20 +0100 Subject: [PATCH 07/22] fix repository and dependency form retrofit2 converter-gson --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 21b2d06..172c1b7 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ central - http://repo1.maven.org/maven2 + http://central.maven.org/maven2 false @@ -105,7 +105,7 @@ com.squareup.retrofit2 converter-gson - latest.version + 2.5.0 From a1341ee77b60449a750b679f1b5e61087d28d535 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Sun, 23 Dec 2018 19:43:17 +0100 Subject: [PATCH 08/22] fix compilation errors --- .../discourse/internal/DiscourseUserSyncConnector.java | 2 +- .../org/xwiki/contrib/usersync/discourse/internal/Post.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 5b98b6c..21175d3 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -71,7 +71,7 @@ public void createUser(BaseObject user) Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://jsonplaceholder.typicode.com/") - .addConvertFactory(GsonConverterFactory.create()) + .addConverterFactory(GsonConverterFactory.create()) .build(); DiscourseService service = retrofit.create(DiscourseService.class); diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java index ba77da8..1282419 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java @@ -1,6 +1,6 @@ package org.xwiki.contrib.usersync.discourse.internal; -import com.google.json.annotation.SerializedName; +import com.google.gson.annotations.SerializedName; public class Post { private int userId; @@ -13,7 +13,7 @@ public class Post { private String text; public int getUserId() { - return d; + return userId; } public int getId() { From 9c2df0b6ccb04c5c461971f3a3b4014cb9ae750c Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Sun, 23 Dec 2018 20:25:02 +0100 Subject: [PATCH 09/22] list posts on jsonplaceholder --- .../internal/DiscourseUserSyncConnector.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 21175d3..abbd3bd 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -20,6 +20,7 @@ package org.xwiki.contrib.usersync.discourse.internal; import java.util.List; +import java.io.IOException; import javax.inject.Inject; import javax.inject.Named; @@ -34,6 +35,7 @@ import retrofit2.Retrofit; import retrofit2.Retrofit.Builder; import retrofit2.Call; +import retrofit2.Response; import retrofit2.converter.gson.GsonConverterFactory; import org.xwiki.contrib.usersync.discourse.internal.DiscourseService; @@ -75,12 +77,22 @@ public void createUser(BaseObject user) .build(); DiscourseService service = retrofit.create(DiscourseService.class); - Call> posts = service.getPosts(); - - - System.out.println("url"); - System.out.println(discourseURL); - System.out.println("end"); + Call> call = service.getPosts(); + + try { + Response> response = call.execute(); + if(response.isSuccessful()) { + System.out.println("succeed!"); + List posts = response.body(); + for(Post post : posts) { + System.out.println(post.getId() + " / " + post.getTitle()); + } + } else { + System.out.println("Code: " + response.code()); + } + } catch (IOException exception) { + System.out.println(exception.getMessage()); + } } @Override From 8942b1ed1d721a2f795f73d5b27a48da650a9a32 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Sun, 23 Dec 2018 23:57:45 +0100 Subject: [PATCH 10/22] using constructor --- .../internal/DiscourseUserSyncConnector.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index abbd3bd..9ff1720 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -58,25 +58,31 @@ public class DiscourseUserSyncConnector implements UserSyncConnector @Inject private ConfigurationSource configuration; - @Override - public void createUser(BaseObject user) - { - // Get the user login - String userId = user.getReference().getName(); - - // Get the user mail - String mail = user.getStringValue("email"); + private Retrofit retrofit; + private DiscourseService service; + public DiscourseUserSyncConnector() { // Get the URL of the discourse server to synchronize with String discourseURL = this.configuration.getProperty(CONFIGURATION_URL); String discourseApiKey = this.configuration.getProperty(API_KEY); - Retrofit retrofit = new Retrofit.Builder() + retrofit = new Retrofit.Builder() .baseUrl("https://jsonplaceholder.typicode.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); - DiscourseService service = retrofit.create(DiscourseService.class); + service = retrofit.create(DiscourseService.class); + } + + @Override + public void createUser(BaseObject user) + { + // Get the user login + String userId = user.getReference().getName(); + + // Get the user mail + String mail = user.getStringValue("email"); + Call> call = service.getPosts(); try { From ae000558720f13bc05e0cd9f2f0ab1c3e1a66724 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Mon, 24 Dec 2018 19:59:46 +0100 Subject: [PATCH 11/22] first try with discourse api => 403 --- .../contrib/usersync/UserSyncConnector.java | 1 + .../discourse/internal/DiscourseService.java | 10 +++--- .../internal/DiscourseUserSyncConnector.java | 33 +++++++++---------- .../discourse/internal/GetUserResponse.java | 11 +++++++ .../usersync/discourse/internal/Post.java | 30 ----------------- .../usersync/discourse/internal/User.java | 23 +++++++++++++ .../DiscourseUserSyncConnectorTest.java | 6 ++++ 7 files changed, 61 insertions(+), 53 deletions(-) create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/GetUserResponse.java delete mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java diff --git a/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java b/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java index cb1d0b2..d9e4fd1 100644 --- a/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java +++ b/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java @@ -31,6 +31,7 @@ @Role public interface UserSyncConnector { + void getUser(String userId); /** * @param user the object containing the metadata of the new user */ diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java index d232c6a..6d4c6ed 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java @@ -1,14 +1,14 @@ package org.xwiki.contrib.usersync.discourse.internal; -import java.util.List; -//import org.simpleframework.xml.Path; import retrofit2.Call; import retrofit2.http.GET; +import retrofit2.http.Path; +import retrofit2.http.Query; -import org.xwiki.contrib.usersync.discourse.internal.Post; +import org.xwiki.contrib.usersync.discourse.internal.GetUserResponse; public interface DiscourseService { - @GET("posts") - Call> getPosts(); + @GET("users/{username}.json") + Call getUser(@Path("username") String username, @Query("api_key") String apiKey, @Query("api_user") String apiUser); } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 9ff1720..44ea5d4 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -53,21 +53,26 @@ public class DiscourseUserSyncConnector implements UserSyncConnector private static final String PREFIX_CONFIGURATION = "usersync.discourse."; private static final String CONFIGURATION_URL = PREFIX_CONFIGURATION + "url"; - private static final String API_KEY = PREFIX_CONFIGURATION + "api_key"; + private static final String CONFIGURATION_API_KEY = PREFIX_CONFIGURATION + "api_key"; + private static final String CONFIGURATION_API_USER = PREFIX_CONFIGURATION + "api_user"; @Inject private ConfigurationSource configuration; private Retrofit retrofit; private DiscourseService service; + String discourseURL; + String discourseApiKey; + String discourseApiUser; public DiscourseUserSyncConnector() { // Get the URL of the discourse server to synchronize with - String discourseURL = this.configuration.getProperty(CONFIGURATION_URL); - String discourseApiKey = this.configuration.getProperty(API_KEY); + discourseURL = this.configuration.getProperty(CONFIGURATION_URL); + discourseApiKey = this.configuration.getProperty(CONFIGURATION_API_KEY); + discourseApiUser = this.configuration.getProperty(CONFIGURATION_API_USER); retrofit = new Retrofit.Builder() - .baseUrl("https://jsonplaceholder.typicode.com/") + .baseUrl(discourseURL) .addConverterFactory(GsonConverterFactory.create()) .build(); @@ -75,24 +80,16 @@ public DiscourseUserSyncConnector() { } @Override - public void createUser(BaseObject user) - { - // Get the user login - String userId = user.getReference().getName(); - - // Get the user mail - String mail = user.getStringValue("email"); - - Call> call = service.getPosts(); + public void getUser(String userId) { + Call call = service.getUser(userId, discourseApiKey, discourseApiUser); + System.out.println("calling get user"); try { - Response> response = call.execute(); + Response response = call.execute(); if(response.isSuccessful()) { System.out.println("succeed!"); - List posts = response.body(); - for(Post post : posts) { - System.out.println(post.getId() + " / " + post.getTitle()); - } + GetUserResponse getUserResponse = response.body(); + System.out.println("Name: " + getUserResponse.getUser().getName()); } else { System.out.println("Code: " + response.code()); } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/GetUserResponse.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/GetUserResponse.java new file mode 100644 index 0000000..cfad7ea --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/GetUserResponse.java @@ -0,0 +1,11 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +import org.xwiki.contrib.usersync.discourse.internal.User; + +public class GetUserResponse { + private User user; + + public User getUser() { + return user; + } +} diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java deleted file mode 100644 index 1282419..0000000 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/Post.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.xwiki.contrib.usersync.discourse.internal; - -import com.google.gson.annotations.SerializedName; - -public class Post { - private int userId; - - private int id; - - private String title; - - @SerializedName("body") - private String text; - - public int getUserId() { - return userId; - } - - public int getId() { - return id; - } - - public String getTitle() { - return title; - } - - public String getText() { - return text; - } -} diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java new file mode 100644 index 0000000..d8f6246 --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java @@ -0,0 +1,23 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +import com.google.gson.annotations.SerializedName; + +public class User { + private int id; + + private String username; + + private String name; + + public int getId() { + return id; + } + + public String getUsername() { + return username; + } + + public String getName() { + return name; + } +} diff --git a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java index c4c0f75..7c20bf4 100644 --- a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java +++ b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java @@ -67,6 +67,12 @@ public void before() } @Test + public void getUser() throws ComponentLookupException + { + // Call the component + this.mocker.getComponentUnderTest().getUser("ThomasMortagne"); + } + public void createUser() throws ComponentLookupException { // Call the component From e8428ea9870a6d073d9837d2cfd7098d216090f5 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Tue, 25 Dec 2018 10:44:13 +0100 Subject: [PATCH 12/22] try to add okhttp3 as dependency --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 172c1b7..6b689ef 100644 --- a/pom.xml +++ b/pom.xml @@ -107,5 +107,10 @@ converter-gson 2.5.0 + + com.squareup.okhttp3 + okhttp + 3.11.0 + From 4dad6781220059bfa830491b2cc65b6ec2e75d05 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Thu, 10 Jan 2019 11:08:49 +0100 Subject: [PATCH 13/22] add import --- pom.xml | 9 +-------- .../discourse/internal/DiscourseUserSyncConnector.java | 6 ++++++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 6b689ef..e4744e6 100644 --- a/pom.xml +++ b/pom.xml @@ -64,13 +64,6 @@ - - central - http://central.maven.org/maven2 - - false - - xwiki-releases XWiki Nexus Releases Repository Proxy @@ -110,7 +103,7 @@ com.squareup.okhttp3 okhttp - 3.11.0 + 3.12.1 diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 44ea5d4..362f4fb 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -32,6 +32,9 @@ import com.xpn.xwiki.objects.BaseObject; +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; + import retrofit2.Retrofit; import retrofit2.Retrofit.Builder; import retrofit2.Call; @@ -59,6 +62,9 @@ public class DiscourseUserSyncConnector implements UserSyncConnector @Inject private ConfigurationSource configuration; + HttpLoggingInterceptor logging; + OkHttpClient.Builder httpClient; + private Retrofit retrofit; private DiscourseService service; String discourseURL; From d27db3a42bed9d5f3931293fc82794259f3edff4 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Thu, 10 Jan 2019 16:23:06 +0100 Subject: [PATCH 14/22] log request --- pom.xml | 4 ++-- .../discourse/internal/DiscourseUserSyncConnector.java | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index e4744e6..1003ff6 100644 --- a/pom.xml +++ b/pom.xml @@ -102,8 +102,8 @@ com.squareup.okhttp3 - okhttp - 3.12.1 + logging-interceptor + 3.12.0 diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 362f4fb..185b83c 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -77,9 +77,15 @@ public DiscourseUserSyncConnector() { discourseApiKey = this.configuration.getProperty(CONFIGURATION_API_KEY); discourseApiUser = this.configuration.getProperty(CONFIGURATION_API_USER); + logging = new HttpLoggingInterceptor(); + logging.setLevel(HttpLoggingInterceptor.Level.BODY); + httpClient = new OkHttpClient.Builder(); + httpClient.addInterceptor(logging); + retrofit = new Retrofit.Builder() .baseUrl(discourseURL) .addConverterFactory(GsonConverterFactory.create()) + .client(httpClient.build()) .build(); service = retrofit.create(DiscourseService.class); From 26b29fd51b4d641eb638a101a7131f94bd42f644 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Thu, 10 Jan 2019 16:35:04 +0100 Subject: [PATCH 15/22] fix get user --- .../usersync/discourse/internal/DiscourseService.java | 2 +- .../discourse/internal/DiscourseUserSyncConnector.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java index 6d4c6ed..857869a 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java @@ -9,6 +9,6 @@ public interface DiscourseService { @GET("users/{username}.json") - Call getUser(@Path("username") String username, @Query("api_key") String apiKey, @Query("api_user") String apiUser); + Call getUser(@Path("username") String username, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 185b83c..8a7a85e 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -57,7 +57,7 @@ public class DiscourseUserSyncConnector implements UserSyncConnector private static final String CONFIGURATION_URL = PREFIX_CONFIGURATION + "url"; private static final String CONFIGURATION_API_KEY = PREFIX_CONFIGURATION + "api_key"; - private static final String CONFIGURATION_API_USER = PREFIX_CONFIGURATION + "api_user"; + private static final String CONFIGURATION_API_USERNAME = PREFIX_CONFIGURATION + "api_username"; @Inject private ConfigurationSource configuration; @@ -69,13 +69,13 @@ public class DiscourseUserSyncConnector implements UserSyncConnector private DiscourseService service; String discourseURL; String discourseApiKey; - String discourseApiUser; + String discourseApiUsername; public DiscourseUserSyncConnector() { // Get the URL of the discourse server to synchronize with discourseURL = this.configuration.getProperty(CONFIGURATION_URL); discourseApiKey = this.configuration.getProperty(CONFIGURATION_API_KEY); - discourseApiUser = this.configuration.getProperty(CONFIGURATION_API_USER); + discourseApiUsername = this.configuration.getProperty(CONFIGURATION_API_USERNAME); logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); @@ -93,7 +93,7 @@ public DiscourseUserSyncConnector() { @Override public void getUser(String userId) { - Call call = service.getUser(userId, discourseApiKey, discourseApiUser); + Call call = service.getUser(userId, discourseApiKey, discourseApiUsername); System.out.println("calling get user"); try { From 0557cc22b53325f4b65a65243a96d578b2a5f66d Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Fri, 11 Jan 2019 16:45:07 +0100 Subject: [PATCH 16/22] create user working --- .../contrib/usersync/UserSyncConnector.java | 8 ++-- .../usersync/interval/UserSyncListener.java | 17 +++++--- .../internal/CreateUserResponse.java | 16 ++++++++ .../discourse/internal/DiscourseService.java | 7 ++++ .../internal/DiscourseUserSyncConnector.java | 41 ++++++++++++++++++- .../usersync/discourse/internal/User.java | 19 +++++++++ .../DiscourseUserSyncConnectorTest.java | 35 +++++++++++----- 7 files changed, 122 insertions(+), 21 deletions(-) create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java diff --git a/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java b/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java index d9e4fd1..a08ba2d 100644 --- a/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java +++ b/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java @@ -31,20 +31,20 @@ @Role public interface UserSyncConnector { - void getUser(String userId); + void getUser(String userId) throws UserSyncException; /** * @param user the object containing the metadata of the new user */ - void createUser(BaseObject user); + void createUser(BaseObject user) throws UserSyncException; /** * @param previousUser the object containing the previous metadata of the user * @param previousUser the object containing the new metadata of the user */ - void modifyUser(BaseObject previousUser, BaseObject newUser); + void modifyUser(BaseObject previousUser, BaseObject newUser) throws UserSyncException; /** * @param deletedUser the object containing the metadata of the deleted user */ - void deleteUser(BaseObject deletedUser); + void deleteUser(BaseObject deletedUser) throws UserSyncException; } diff --git a/usersync-api/src/main/java/org/xwiki/contrib/usersync/interval/UserSyncListener.java b/usersync-api/src/main/java/org/xwiki/contrib/usersync/interval/UserSyncListener.java index b9d2ecb..8ba9b1c 100644 --- a/usersync-api/src/main/java/org/xwiki/contrib/usersync/interval/UserSyncListener.java +++ b/usersync-api/src/main/java/org/xwiki/contrib/usersync/interval/UserSyncListener.java @@ -27,6 +27,7 @@ import org.xwiki.component.annotation.Component; import org.xwiki.contrib.usersync.UserSyncConnector; +import org.xwiki.contrib.usersync.UserSyncException; import org.xwiki.model.reference.EntityReference; import org.xwiki.observation.AbstractEventListener; import org.xwiki.observation.event.Event; @@ -80,12 +81,16 @@ private void onEvent(UserSyncConnector connector, XObjectEvent event, XWikiDocum BaseObject newUser = document.getXObject(event.getReference()); BaseObject previousUser = document.getOriginalDocument().getXObject(event.getReference()); - if (event instanceof XObjectAddedEvent) { - connector.createUser(newUser); - } else if (event instanceof XObjectDeletedEvent) { - connector.deleteUser(previousUser); - } else { - connector.modifyUser(previousUser, newUser); + try { + if (event instanceof XObjectAddedEvent) { + connector.createUser(newUser); + } else if (event instanceof XObjectDeletedEvent) { + connector.deleteUser(previousUser); + } else { + connector.modifyUser(previousUser, newUser); + } + } catch (UserSyncException exception) { + System.out.println(exception.getMessage()); } } } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java new file mode 100644 index 0000000..7b1fe9c --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java @@ -0,0 +1,16 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +import org.xwiki.contrib.usersync.discourse.internal.User; + +public class CreateUserResponse { + private Boolean success; + + public Boolean getSuccess() { + return success; + } + private String message; + + public String getMessage() { + return message; + } +} diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java index 857869a..cd3f3ad 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java @@ -2,13 +2,20 @@ import retrofit2.Call; import retrofit2.http.GET; +import retrofit2.http.POST; import retrofit2.http.Path; import retrofit2.http.Query; +import retrofit2.http.Body; +import org.xwiki.contrib.usersync.discourse.internal.User; import org.xwiki.contrib.usersync.discourse.internal.GetUserResponse; +import org.xwiki.contrib.usersync.discourse.internal.CreateUserResponse; public interface DiscourseService { @GET("users/{username}.json") Call getUser(@Path("username") String username, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); + + @POST("users") + Call createUser(@Body User user, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 8a7a85e..b2478ff 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -29,6 +29,7 @@ import org.xwiki.component.annotation.Component; import org.xwiki.configuration.ConfigurationSource; import org.xwiki.contrib.usersync.UserSyncConnector; +import org.xwiki.contrib.usersync.UserSyncException; import com.xpn.xwiki.objects.BaseObject; @@ -95,7 +96,6 @@ public DiscourseUserSyncConnector() { public void getUser(String userId) { Call call = service.getUser(userId, discourseApiKey, discourseApiUsername); - System.out.println("calling get user"); try { Response response = call.execute(); if(response.isSuccessful()) { @@ -110,6 +110,45 @@ public void getUser(String userId) { } } + @Override + public void createUser(BaseObject userObject) throws UserSyncException { + // Get the user login + String userId = userObject.getStringValue("id"); + + // Get the user email + String email = userObject.getStringValue("email"); + + // Get the user password + String password = userObject.getStringValue("password"); + + // Get the user name + String name = userObject.getStringValue("name"); + + System.out.printf("creating user: %s / %s / %s / %s\n", userId, email, password, name); + + User user = new User(userId, name, email, password); + + Call call = service.createUser(user, discourseApiKey, discourseApiUsername); + + try { + Response response = call.execute(); + if(response.isSuccessful()) { + System.out.println("succeed!"); + CreateUserResponse createUserResponse = response.body(); + if (createUserResponse.getSuccess()) { + System.out.println("Success creating user"); + } else { + throw new UserSyncException(createUserResponse.getMessage()); + } + } else { + System.out.println("Code: " + response.code()); + throw new UserSyncException("Bad response code:" + response.code()); + } + } catch (IOException exception) { + System.out.println(exception.getMessage()); + } + } + @Override public void modifyUser(BaseObject previousUser, BaseObject newUser) { diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java index d8f6246..52d9e5c 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java @@ -9,6 +9,17 @@ public class User { private String name; + private String email; + + private String password; + + public User(String _username, String _name, String _email, String _password) { + username = _username; + name = _name; + email = _email; + password = _password; + } + public int getId() { return id; } @@ -20,4 +31,12 @@ public String getUsername() { public String getName() { return name; } + + public String getEmail() { + return email; + } + + public String getPassword() { + return password; + } } diff --git a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java index 7c20bf4..5ffcd16 100644 --- a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java +++ b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java @@ -22,8 +22,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.Assert; import org.xwiki.component.manager.ComponentLookupException; import org.xwiki.contrib.usersync.UserSyncConnector; +import org.xwiki.contrib.usersync.UserSyncException; import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.LocalDocumentReference; import org.xwiki.test.mockito.MockitoComponentMockingRule; @@ -57,7 +59,10 @@ public void before() this.newUser = new BaseObject(); this.newUser.setXClassReference(new LocalDocumentReference("XWiki", "XWikiUsers")); - this.newUser.setStringValue("email", "mail@domain.com"); + this.newUser.setStringValue("id", "SergeGainsbourg"); + this.newUser.setStringValue("email", "martin@phonations.com"); + this.newUser.setStringValue("password", "abcdefgh1234"); + this.newUser.setStringValue("name", "Serge Gainsbourg"); userDocument.addXObject(this.newUser); @@ -66,17 +71,25 @@ public void before() userDocument.addXObject(this.previousUser); } - @Test public void getUser() throws ComponentLookupException { - // Call the component - this.mocker.getComponentUnderTest().getUser("ThomasMortagne"); + try { + // Call the component + this.mocker.getComponentUnderTest().getUser("ThomasMortagne"); + } catch (UserSyncException exception) { + Assert.fail(exception.getMessage()); + } } + @Test public void createUser() throws ComponentLookupException { - // Call the component - this.mocker.getComponentUnderTest().createUser(this.newUser); + try { + // Call the component + this.mocker.getComponentUnderTest().createUser(this.newUser); + } catch (UserSyncException exception) { + Assert.fail(exception.getMessage()); + } } public void modifyUser() throws ComponentLookupException @@ -84,9 +97,11 @@ public void modifyUser() throws ComponentLookupException // Modify the user this.newUser.setStringValue("email", "differentmail@domain.com"); - // Call the component - this.mocker.getComponentUnderTest().modifyUser(this.previousUser, this.newUser); - - // TODO + try { + // Call the component + this.mocker.getComponentUnderTest().modifyUser(this.previousUser, this.newUser); + } catch (UserSyncException exception) { + Assert.fail(exception.getMessage()); + } } } From b822b333f96961ec3030a3718827e33d006066bd Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Mon, 14 Jan 2019 18:29:02 +0100 Subject: [PATCH 17/22] add delete user --- .../discourse/internal/CreateUserBody.java | 20 ++++++++ .../internal/CreateUserResponse.java | 7 ++- .../internal/DeleteUserResponse.java | 11 ++++ .../discourse/internal/DiscourseService.java | 15 +++--- .../internal/DiscourseUserSyncConnector.java | 51 ++++++++++++++++--- .../usersync/discourse/internal/User.java | 11 +--- .../DiscourseUserSyncConnectorTest.java | 12 +++++ 7 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java create mode 100644 usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DeleteUserResponse.java diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java new file mode 100644 index 0000000..27999bb --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java @@ -0,0 +1,20 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +import com.google.gson.annotations.SerializedName; + +public class CreateUserBody { + private String username; + + private String name; + + private String email; + + private String password; + + public CreateUserBody(String _username, String _name, String _email, String _password) { + username = _username; + name = _name; + email = _email; + password = _password; + } +} diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java index 7b1fe9c..b6f16aa 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserResponse.java @@ -5,12 +5,17 @@ public class CreateUserResponse { private Boolean success; + private String message; + + private Integer user_id; + public Boolean getSuccess() { return success; } - private String message; public String getMessage() { return message; } + + public Integer getUserId() { return user_id; } } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DeleteUserResponse.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DeleteUserResponse.java new file mode 100644 index 0000000..0eda85d --- /dev/null +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DeleteUserResponse.java @@ -0,0 +1,11 @@ +package org.xwiki.contrib.usersync.discourse.internal; + +import org.xwiki.contrib.usersync.discourse.internal.User; + +public class DeleteUserResponse { + private Boolean deleted; + + public Boolean getDeleted() { + return deleted; + } +} diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java index cd3f3ad..bcf414f 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java @@ -1,21 +1,18 @@ package org.xwiki.contrib.usersync.discourse.internal; import retrofit2.Call; -import retrofit2.http.GET; -import retrofit2.http.POST; -import retrofit2.http.Path; -import retrofit2.http.Query; -import retrofit2.http.Body; +import retrofit2.http.*; -import org.xwiki.contrib.usersync.discourse.internal.User; -import org.xwiki.contrib.usersync.discourse.internal.GetUserResponse; -import org.xwiki.contrib.usersync.discourse.internal.CreateUserResponse; +import org.xwiki.contrib.usersync.discourse.internal.*; public interface DiscourseService { @GET("users/{username}.json") Call getUser(@Path("username") String username, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); @POST("users") - Call createUser(@Body User user, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); + Call createUser(@Body CreateUserBody createUserBody, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); + + @DELETE("/admin/users/{userid}.json") + Call deleteUser(@Path("userid") Integer userId, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index b2478ff..b330b65 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -100,8 +100,7 @@ public void getUser(String userId) { Response response = call.execute(); if(response.isSuccessful()) { System.out.println("succeed!"); - GetUserResponse getUserResponse = response.body(); - System.out.println("Name: " + getUserResponse.getUser().getName()); + System.out.println("Name: " + response.body().getUser().getName()); } else { System.out.println("Code: " + response.code()); } @@ -126,9 +125,9 @@ public void createUser(BaseObject userObject) throws UserSyncException { System.out.printf("creating user: %s / %s / %s / %s\n", userId, email, password, name); - User user = new User(userId, name, email, password); + CreateUserBody createUserBody = new CreateUserBody(userId, name, email, password); - Call call = service.createUser(user, discourseApiKey, discourseApiUsername); + Call call = service.createUser(createUserBody, discourseApiKey, discourseApiUsername); try { Response response = call.execute(); @@ -136,7 +135,8 @@ public void createUser(BaseObject userObject) throws UserSyncException { System.out.println("succeed!"); CreateUserResponse createUserResponse = response.body(); if (createUserResponse.getSuccess()) { - System.out.println("Success creating user"); + System.out.println("Success creating user with id:" + createUserResponse.getUserId()); + userObject.setIntValue("id", createUserResponse.getUserId()); } else { throw new UserSyncException(createUserResponse.getMessage()); } @@ -150,14 +150,49 @@ public void createUser(BaseObject userObject) throws UserSyncException { } @Override - public void modifyUser(BaseObject previousUser, BaseObject newUser) + public void modifyUser(BaseObject previousUser, BaseObject newUser) throws UserSyncException { // TODO } @Override - public void deleteUser(BaseObject deletedUser) + public void deleteUser(BaseObject userObject) throws UserSyncException { - // TODO + // Get the user login + String userName = userObject.getStringValue("id"); + + System.out.printf("deleting user: %s\n", userName); + + Call getUserResponseCall = service.getUser(userName, discourseApiKey, discourseApiUsername); + + + + try { + Response getUserResponse = getUserResponseCall.execute(); + if (getUserResponse.isSuccessful()) { + System.out.printf("user name is " + getUserResponse.body().getUser().getEmail()); + Integer userId = getUserResponse.body().getUser().getId(); + System.out.printf("user id is " + userId); + Call deleteUserResponseCall = service.deleteUser(userId, discourseApiKey, discourseApiUsername); + + Response deleteUserResponse = deleteUserResponseCall.execute(); + if(deleteUserResponse.isSuccessful()) { + System.out.println("succeed!"); + if (deleteUserResponse.body().getDeleted()) { + System.out.println("Success deleting user"); + } else { + throw new UserSyncException("Failed deleting user"); + } + } else { + System.out.println("Code: " + deleteUserResponse.code()); + throw new UserSyncException("Bad response code for deleteUser:" + deleteUserResponse.code()); + } + } else { + System.out.println("Code: " + getUserResponse.code()); + throw new UserSyncException("Bad response code for getUser:" + getUserResponse.code()); + } + } catch (IOException exception) { + System.out.println(exception.getMessage()); + } } } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java index 52d9e5c..6cbe3c8 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/User.java @@ -13,16 +13,7 @@ public class User { private String password; - public User(String _username, String _name, String _email, String _password) { - username = _username; - name = _name; - email = _email; - password = _password; - } - - public int getId() { - return id; - } + public int getId() { return id; } public String getUsername() { return username; diff --git a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java index 5ffcd16..9aad28b 100644 --- a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java +++ b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java @@ -71,6 +71,7 @@ public void before() userDocument.addXObject(this.previousUser); } + @Test public void getUser() throws ComponentLookupException { try { @@ -104,4 +105,15 @@ public void modifyUser() throws ComponentLookupException Assert.fail(exception.getMessage()); } } + + @Test + public void deleteUser() throws ComponentLookupException + { + try { + // Call the component + this.mocker.getComponentUnderTest().deleteUser(this.newUser); + } catch (UserSyncException exception) { + Assert.fail(exception.getMessage()); + } + } } From 10fe78c5d3b3f61f998166e8cc5b1f2af5ee7d54 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Mon, 14 Jan 2019 18:42:40 +0100 Subject: [PATCH 18/22] remove getUser --- .../contrib/usersync/UserSyncConnector.java | 1 - .../internal/DiscourseUserSyncConnector.java | 17 ----------------- .../DiscourseUserSyncConnectorTest.java | 11 ----------- 3 files changed, 29 deletions(-) diff --git a/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java b/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java index a08ba2d..8737580 100644 --- a/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java +++ b/usersync-api/src/main/java/org/xwiki/contrib/usersync/UserSyncConnector.java @@ -31,7 +31,6 @@ @Role public interface UserSyncConnector { - void getUser(String userId) throws UserSyncException; /** * @param user the object containing the metadata of the new user */ diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index b330b65..6a30bb9 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -92,23 +92,6 @@ public DiscourseUserSyncConnector() { service = retrofit.create(DiscourseService.class); } - @Override - public void getUser(String userId) { - Call call = service.getUser(userId, discourseApiKey, discourseApiUsername); - - try { - Response response = call.execute(); - if(response.isSuccessful()) { - System.out.println("succeed!"); - System.out.println("Name: " + response.body().getUser().getName()); - } else { - System.out.println("Code: " + response.code()); - } - } catch (IOException exception) { - System.out.println(exception.getMessage()); - } - } - @Override public void createUser(BaseObject userObject) throws UserSyncException { // Get the user login diff --git a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java index 9aad28b..b5df310 100644 --- a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java +++ b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java @@ -71,17 +71,6 @@ public void before() userDocument.addXObject(this.previousUser); } - @Test - public void getUser() throws ComponentLookupException - { - try { - // Call the component - this.mocker.getComponentUnderTest().getUser("ThomasMortagne"); - } catch (UserSyncException exception) { - Assert.fail(exception.getMessage()); - } - } - @Test public void createUser() throws ComponentLookupException { From 2b0ab39c2171c97d0e2e55460c11da0dd5710228 Mon Sep 17 00:00:00 2001 From: Thomas Mortagne Date: Tue, 16 Jun 2020 17:00:56 +0200 Subject: [PATCH 19/22] Fix component initialization --- .../discourse/internal/DiscourseUserSyncConnector.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 6a30bb9..65ed98a 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -27,6 +27,8 @@ import javax.inject.Singleton; import org.xwiki.component.annotation.Component; +import org.xwiki.component.phase.Initializable; +import org.xwiki.component.phase.InitializationException; import org.xwiki.configuration.ConfigurationSource; import org.xwiki.contrib.usersync.UserSyncConnector; import org.xwiki.contrib.usersync.UserSyncException; @@ -52,7 +54,7 @@ @Component @Singleton @Named("discourse") -public class DiscourseUserSyncConnector implements UserSyncConnector +public class DiscourseUserSyncConnector implements UserSyncConnector, Initializable { private static final String PREFIX_CONFIGURATION = "usersync.discourse."; @@ -72,7 +74,9 @@ public class DiscourseUserSyncConnector implements UserSyncConnector String discourseApiKey; String discourseApiUsername; - public DiscourseUserSyncConnector() { + @Override + public void initialize() throws InitializationException + { // Get the URL of the discourse server to synchronize with discourseURL = this.configuration.getProperty(CONFIGURATION_URL); discourseApiKey = this.configuration.getProperty(CONFIGURATION_API_KEY); From b0890bd6dcf2e6ade1b870b0d6dfa5687e286bf1 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Mon, 21 Sep 2020 17:19:50 +0200 Subject: [PATCH 20/22] Fix MemoryConfigurationSource with Thomas --- .../usersync/discourse/internal/CreateUserBody.java | 3 +++ .../internal/DiscourseUserSyncConnector.java | 13 ++++--------- .../internal/DiscourseUserSyncConnectorTest.java | 10 +++++++++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java index 27999bb..26393ab 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/CreateUserBody.java @@ -11,10 +11,13 @@ public class CreateUserBody { private String password; + private Boolean active; + public CreateUserBody(String _username, String _name, String _email, String _password) { username = _username; name = _name; email = _email; password = _password; + active = true; } } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 65ed98a..8f4a1c4 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -19,7 +19,6 @@ */ package org.xwiki.contrib.usersync.discourse.internal; -import java.util.List; import java.io.IOException; import javax.inject.Inject; @@ -37,15 +36,11 @@ import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; - -import retrofit2.Retrofit; -import retrofit2.Retrofit.Builder; import retrofit2.Call; import retrofit2.Response; +import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; -import org.xwiki.contrib.usersync.discourse.internal.DiscourseService; - /** * {@link UserSyncConnector} implementation for Discourse. * @@ -58,9 +53,9 @@ public class DiscourseUserSyncConnector implements UserSyncConnector, Initializa { private static final String PREFIX_CONFIGURATION = "usersync.discourse."; - private static final String CONFIGURATION_URL = PREFIX_CONFIGURATION + "url"; - private static final String CONFIGURATION_API_KEY = PREFIX_CONFIGURATION + "api_key"; - private static final String CONFIGURATION_API_USERNAME = PREFIX_CONFIGURATION + "api_username"; + public static final String CONFIGURATION_URL = PREFIX_CONFIGURATION + "url"; + public static final String CONFIGURATION_API_KEY = PREFIX_CONFIGURATION + "api_key"; + public static final String CONFIGURATION_API_USERNAME = PREFIX_CONFIGURATION + "api_username"; @Inject private ConfigurationSource configuration; diff --git a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java index b5df310..9a0093f 100644 --- a/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java +++ b/usersync-connectors/usersync-connector-discourse/src/test/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnectorTest.java @@ -19,15 +19,17 @@ */ package org.xwiki.contrib.usersync.discourse.internal; +import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.Assert; import org.xwiki.component.manager.ComponentLookupException; +import org.xwiki.configuration.internal.MemoryConfigurationSource; import org.xwiki.contrib.usersync.UserSyncConnector; import org.xwiki.contrib.usersync.UserSyncException; import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.LocalDocumentReference; +import org.xwiki.test.annotation.AfterComponent; import org.xwiki.test.mockito.MockitoComponentMockingRule; import com.xpn.xwiki.doc.XWikiDocument; @@ -50,6 +52,12 @@ public class DiscourseUserSyncConnectorTest private BaseObject newUser; private BaseObject previousUser; + private MemoryConfigurationSource configuration = oldcore.getConfigurationSource(); + + @AfterComponent + public void afterComponent() throws Exception { + configuration = mocker.registerMemoryConfigurationSource(); + } @Before public void before() From 7794e904417707a22fd088df7ac3388f9068ee6c Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Sat, 26 Sep 2020 18:06:16 +0200 Subject: [PATCH 21/22] Pass api key and username in the header --- .../discourse/internal/DiscourseService.java | 15 +++++++++------ .../internal/DiscourseUserSyncConnector.java | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java index bcf414f..803212b 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseService.java @@ -1,18 +1,21 @@ package org.xwiki.contrib.usersync.discourse.internal; import retrofit2.Call; -import retrofit2.http.*; - -import org.xwiki.contrib.usersync.discourse.internal.*; +import retrofit2.http.Body; +import retrofit2.http.DELETE; +import retrofit2.http.GET; +import retrofit2.http.POST; +import retrofit2.http.Path; +import retrofit2.http.Header; public interface DiscourseService { @GET("users/{username}.json") - Call getUser(@Path("username") String username, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); + Call getUser(@Path("username") String username, @Header("Api-Key") String apiKey, @Header("Api-Username") String apiUsername); @POST("users") - Call createUser(@Body CreateUserBody createUserBody, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); + Call createUser(@Body CreateUserBody createUserBody, @Header("Api-Key") String apiKey, @Header("Api-Username") String apiUsername); @DELETE("/admin/users/{userid}.json") - Call deleteUser(@Path("userid") Integer userId, @Query("api_key") String apiKey, @Query("api_username") String apiUsername); + Call deleteUser(@Path("userid") Integer userId, @Header("Api-Key") String apiKey, @Header("Api-Username") String apiUsername); } diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 8f4a1c4..1a00ba8 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -128,6 +128,7 @@ public void createUser(BaseObject userObject) throws UserSyncException { } } catch (IOException exception) { System.out.println(exception.getMessage()); + throw new UserSyncException("Unknown error:" + exception.getMessage()); } } From 15e80aa05426ef9df7cc41d14e2464fda2d2ad27 Mon Sep 17 00:00:00 2001 From: Martin Delille Date: Mon, 28 Sep 2020 14:29:57 +0200 Subject: [PATCH 22/22] Rethrow IOException properly --- .../discourse/internal/DiscourseUserSyncConnector.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java index 1a00ba8..7041c10 100644 --- a/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java +++ b/usersync-connectors/usersync-connector-discourse/src/main/java/org/xwiki/contrib/usersync/discourse/internal/DiscourseUserSyncConnector.java @@ -128,7 +128,7 @@ public void createUser(BaseObject userObject) throws UserSyncException { } } catch (IOException exception) { System.out.println(exception.getMessage()); - throw new UserSyncException("Unknown error:" + exception.getMessage()); + throw new UserSyncException("Unknown error", exception); } } @@ -176,6 +176,7 @@ public void deleteUser(BaseObject userObject) throws UserSyncException } } catch (IOException exception) { System.out.println(exception.getMessage()); + throw new UserSyncException("Unknown error", exception); } } }