Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create user #2

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<id>tmortagne</id>
<name>Thomas Mortagne</name>
</developer>
<developer>
<id>martindelille</id>
<name>Martin Delille</name>
</developer>
</developers>
<modules>
<module>usersync-api</module>
Expand All @@ -60,6 +64,13 @@

<!-- The repository where to get XWiki dependencies -->
<repositories>
<repository>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il n'y as aucune raison pour que tu ai besoin de ça. Maven central est activé de base dans Maven.

<id>central</id>
<url>http://central.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>xwiki-releases</id>
<name>XWiki Nexus Releases Repository Proxy</name>
Expand All @@ -85,4 +96,21 @@
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@Role
public interface UserSyncConnector
{
void getUser(String userId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette methode n'as pas de sens dans cette API. Si c'est juste pour ton test unitaire il te suffit de caster this.mocker.getComponentUnderTest() en DiscourseUserSyncConnector pour appeller la methode temporaire. Ça évitera d'oublier de la virer à la fin ;)

/**
* @param user the object containing the metadata of the new user
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.xwiki.contrib.usersync.discourse.internal;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

import org.xwiki.contrib.usersync.discourse.internal.GetUserResponse;

public interface DiscourseService {
@GET("users/{username}.json")
Call<GetUserResponse> getUser(@Path("username") String username, @Query("api_key") String apiKey, @Query("api_user") String apiUser);
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package org.xwiki.contrib.usersync.discourse.internal;

import java.util.List;
import java.io.IOException;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
Expand All @@ -29,9 +32,17 @@

import com.xpn.xwiki.objects.BaseObject;

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;

/**
* {@link UserSyncConnector} implementation for Discourse.
*
*
* @version $Id$
*/
@Component
Expand All @@ -40,25 +51,51 @@
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 CONFIGURATION_API_KEY = PREFIX_CONFIGURATION + "api_key";
private static final String CONFIGURATION_API_USER = PREFIX_CONFIGURATION + "api_user";

@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;
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);
discourseURL = this.configuration.getProperty(CONFIGURATION_URL);
discourseApiKey = this.configuration.getProperty(CONFIGURATION_API_KEY);
discourseApiUser = this.configuration.getProperty(CONFIGURATION_API_USER);

// TODO
retrofit = new Retrofit.Builder()
.baseUrl(discourseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();

service = retrofit.create(DiscourseService.class);
}

@Override
public void getUser(String userId) {
Call<GetUserResponse> call = service.getUser(userId, discourseApiKey, discourseApiUser);

System.out.println("calling get user");
try {
Response<GetUserResponse> response = call.execute();
if(response.isSuccessful()) {
System.out.println("succeed!");
GetUserResponse getUserResponse = response.body();
System.out.println("Name: " + getUserResponse.getUser().getName());
} else {
System.out.println("Code: " + response.code());
}
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ 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
this.mocker.getComponentUnderTest().createUser(this.newUser);
}

public void modifyUser() throws ComponentLookupException
{
// Modify the user
Expand Down