跟新测试和重构 Package

This commit is contained in:
YuCheng Hu 2024-10-22 17:36:31 -04:00
parent 2d38e6cdc1
commit 94ed05d9b8
9 changed files with 270 additions and 17 deletions

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="ossez-repo-snapshots" />
<option name="name" value="iSharkFly Private Snapshots" />
<option name="url" value="https://repo.isharkfly.com/repository/maven-snapshots/" />
</remote-repository>
<remote-repository>
<option name="id" value="ossez-repo-snapshots" />
<option name="name" value="OSSEZ Private Snapshots" />
@ -31,6 +36,11 @@
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="ossez-repo-releases" />
<option name="name" value="iSharkFly Private Releases" />
<option name="url" value="https://repo.isharkfly.com/repository/maven-releases/" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />

View File

@ -15,6 +15,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
@ -59,7 +61,7 @@ public class TopicsService extends DiscourseClient {
return discourseTopic;
}
public Optional<Topic> createTopic(TopicCreation topicCreation) {
public Optional<Topic> createTopic(TopicCreation topicCreation) throws DiscourseRuntimeException {
String path = "/posts.json";
Optional<Topic> topic = Optional.ofNullable(new Topic());
@ -88,7 +90,13 @@ public class TopicsService extends DiscourseClient {
topic = getTopic(post.get().getTopicId());
}
} else {
throw new DiscourseRuntimeException(responseStr);
List<String> errorString = new ArrayList<>();
errorString.add(response.body().string());
DiscourseError discourseError = new DiscourseError();
discourseError.setAction("API Error with code " + response.code());
discourseError.setErrors(errorString);
throw new DiscourseRuntimeException(discourseError);
}
} catch (IOException e) {
throw new DiscourseRuntimeException(e);

View File

@ -0,0 +1,65 @@
package com.ossez.discourse.client.service;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.ossez.discourse.client.DiscourseClient;
import com.ossez.discourse.common.exception.DiscourseRuntimeException;
import com.ossez.discourse.common.model.req.SyncSso;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* WeChat Official Account Platform related Service
*
* @author YuCheng
*/
public class UserService extends DiscourseClient {
private final Logger log = LoggerFactory.getLogger(UserService.class);
private WeChatOfficialAccountApi weChatOfficialAccountApi;
private OkHttpClient client = new OkHttpClient();
public UserService(String siteUrl, String apiName, String apiKey) {
DiscourseClient.site_url = siteUrl;
DiscourseClient.api_username = apiName;
DiscourseClient.api_key = apiKey;
}
public void syncSSO(String ssoPayload) {
String path = "admin/users/sync_sso";
String hmac = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, "55619458534897682511405307018226").hmacHex(ssoPayload);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
SyncSso syncSSO = new SyncSso();
syncSSO.setSso(ssoPayload);
syncSSO.setSig(hmac);
try {
RequestBody body = RequestBody.create(
MediaType.parse("application/json"), objectMapper.writeValueAsString(syncSSO));
Response response = client.newCall(postRequest(path, body)).execute();
String responseStr = response.body().string();
log.debug("PROCESS CREATE RESPONSE CODE AND STR - [{}]", response.code());
} catch (IOException e) {
throw new DiscourseRuntimeException(e);
}
}
}

View File

@ -0,0 +1,66 @@
package com.ossez.discourse.client.test;
import com.google.inject.Inject;
import com.ossez.discourse.client.service.TopicsService;
import com.ossez.discourse.client.service.UserService;
import com.ossez.discourse.common.exception.DiscourseErrorException;
import com.ossez.discourse.common.model.dto.Topic;
import com.ossez.discourse.common.model.dto.TopicCreation;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for datacube API
*
* @author YuCheng
*/
@TestInstance(Lifecycle.PER_CLASS)
public class SSOTest extends TestBase {
private static final Logger log = LoggerFactory.getLogger(SSOTest.class);
@Inject
protected UserService userService;
/**
* Test Create Menu
*
* @throws DiscourseErrorException
*/
@Test
public void testGetTopic() throws DiscourseErrorException {
Base64 base64 = new Base64();
String url = null;
try {
URIBuilder builder = new URIBuilder();
builder.addParameter("external_id", "1");
builder.addParameter("email", "info@isharkfly.com");
builder.addParameter("username", "info.visafn.sso");
// builder.addParameter("add_groups", "bar");
builder.addParameter("require_activation", "false");
url = builder.build().toString();
// url = StringUtils.removeStart(builder.build().toString(),"?");
System.out.println(StringUtils.removeStart(url, "?"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
String ssoPayload = base64.encodeToString(url.getBytes(StandardCharsets.UTF_8));
userService.syncSSO(ssoPayload);
}
}

View File

@ -6,6 +6,7 @@ import com.google.inject.Injector;
import com.ossez.discourse.client.DiscourseClient;
import com.ossez.discourse.client.service.PostsService;
import com.ossez.discourse.client.service.TopicsService;
import com.ossez.discourse.client.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger;
@ -50,11 +51,15 @@ public class TestBase {
DiscourseClient.api_username = prop.getProperty("api.username");
DiscourseClient.api_key = prop.getProperty("api.key");
// UserService
UserService userService= new UserService(DiscourseClient.site_url, DiscourseClient.api_username, DiscourseClient.api_key);
bind(UserService.class).toInstance(userService);
// TopicsService
TopicsService topicsService = new TopicsService(DiscourseClient.site_url, DiscourseClient.api_username, DiscourseClient.api_key);
bind(TopicsService.class).toInstance(topicsService);
// TopicsService
// PostService
PostsService postsService = new PostsService(DiscourseClient.site_url, DiscourseClient.api_username, DiscourseClient.api_key);
bind(PostsService.class).toInstance(postsService);

View File

@ -47,17 +47,18 @@ public class TopicServiceTest extends TestBase {
}
@Test
public void testCreateTopic() throws DiscourseErrorException {
// log.debug("Create Discourse Topic for Testing");
// TopicCreation topicCreation = new TopicCreation();
// topicCreation.setTitle(DISCOURSE_TOPIC_TITLE_CREATE);
// topicCreation.setCategory(3);
// topicCreation.setRaw(DISCOURSE_TOPIC_TITLE_CREATE);
//
// Optional<Topic> topic = topicsService.createTopic(topicCreation);
// assertThat(topic).isNotEmpty();
//
// log.debug("Created Topic Id - [{}]", topic.get().getId());
public void testCreateTopic() {
log.debug("Create Discourse Topic for Testing");
TopicCreation topicCreation = new TopicCreation();
topicCreation.setTitle(DISCOURSE_TOPIC_TITLE_CREATE);
topicCreation.setCategory(3);
topicCreation.setRaw(DISCOURSE_TOPIC_TITLE_CREATE);
Optional<Topic> topic = topicsService.createTopic(topicCreation);
assertThat(topic).isNotEmpty();
//
// assertThat(topic.get().getId()).isGreaterThan(0);
// assertThat(topic.get().getPostStream().getPosts().get(0).getId()).isGreaterThan(0);

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.time.LocalDate;
import java.util.List;
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@ -17,7 +18,7 @@ public class Topic {
private String title;
private String fancyTitle;
private Integer postsCount;
private String createdAt;
private LocalDate createdAt;
private Integer views;
private Integer replyCount;
private Integer likeCount;
@ -108,11 +109,11 @@ public class Topic {
this.postsCount = postsCount;
}
public String getCreatedAt() {
public LocalDate getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
public void setCreatedAt(LocalDate createdAt) {
this.createdAt = createdAt;
}

View File

@ -0,0 +1,64 @@
package com.ossez.discourse.common.model.dto.user;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ossez.discourse.common.model.dto.Creation;
import java.io.Serializable;
public class UserSso extends Creation implements Serializable {
private static final long serialVersionUID = 4520700408603200435L;
@JsonProperty(required = true, value = "external_id")
private String externalId;
private String email;
private String username;
@JsonProperty(value = "add_groups")
private String addGroups;
@JsonProperty(value = "require_activation")
private String requireActivation;
@Override
public String getExternalId() {
return externalId;
}
@Override
public void setExternalId(String externalId) {
this.externalId = externalId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddGroups() {
return addGroups;
}
public void setAddGroups(String addGroups) {
this.addGroups = addGroups;
}
public String getRequireActivation() {
return requireActivation;
}
public void setRequireActivation(String requireActivation) {
this.requireActivation = requireActivation;
}
}

View File

@ -0,0 +1,33 @@
package com.ossez.discourse.common.model.req;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ossez.discourse.common.model.dto.Creation;
import java.io.Serializable;
public class SyncSso extends Creation implements Serializable {
private static final long serialVersionUID = 4520700408603200435L;
@JsonProperty(required = true)
private String sso;
private String sig;
public String getSig() {
return sig;
}
public void setSig(String sig) {
this.sig = sig;
}
public String getSso() {
return sso;
}
public void setSso(String sso) {
this.sso = sso;
}
}