Browse Source

fix(0.0.2): fix pageable errors.

Bay 5 years ago
parent
commit
c0e2bb4de6

+ 1 - 1
pom.xml

@@ -6,7 +6,7 @@
 
     <groupId>cn.seecoder</groupId>
     <artifactId>seecoder-bok</artifactId>
-    <version>0.0.1</version>
+    <version>0.0.2</version>
     <modules>
         <module>seecoder-bok-client</module>
         <module>seecoder-bok-common</module>

+ 2 - 2
seecoder-bok-client/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>seecoder-bok</artifactId>
         <groupId>cn.seecoder</groupId>
-        <version>0.0.1</version>
+        <version>0.0.2</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 
@@ -44,7 +44,7 @@
         <dependency>
             <groupId>cn.seecoder</groupId>
             <artifactId>seecoder-bok-common</artifactId>
-            <version>0.0.1</version>
+            <version>0.0.2</version>
             <scope>compile</scope>
         </dependency>
     </dependencies>

+ 27 - 15
seecoder-bok-client/src/main/java/cn/seecoder/bok/client/SeecoderBokClient.java

@@ -14,6 +14,7 @@ import okhttp3.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.data.domain.*;
+import org.springframework.data.util.Streamable;
 
 import javax.validation.constraints.NotBlank;
 import javax.validation.constraints.NotNull;
@@ -38,6 +39,11 @@ public class SeecoderBokClient
     private final int CONFIG_PAGE_START;
     private final String CONFIG_HOST;
 
+    /**
+     * 创建client
+     *
+     * @param host bok地址,示例:{@code "http://bok.seecoder.cn"}
+     */
     public SeecoderBokClient(String host) {
         this(host, 0);
     }
@@ -178,18 +184,6 @@ public class SeecoderBokClient
         return getQuestionVOS(pageable, urlBuilder);
     }
 
-    private Page<QuestionVO> getQuestionVOS(Pageable pageable, HttpUrl.Builder urlBuilder) {
-        HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
-        Response response = get(httpUrl);
-        return getResponseBody(response, PageableQuestionListVO.class)
-                .map(pageableQuestionListVO ->
-                        new PageImpl<>(
-                                pageableQuestionListVO.getQuestions(),
-                                parseBokToPageable(pageableQuestionListVO.getPage(), pageable),
-                                pageableQuestionListVO.getPage().getTotalElements()))
-                .orElse(new PageImpl<>(Collections.emptyList()));
-    }
-
     @Override
     public Optional<InteractiveVO> getUserInteractiveQuestion(
             @NotBlank String userId, @NotBlank String questionId) {
@@ -209,6 +203,18 @@ public class SeecoderBokClient
                 .orElse(Collections.emptyList());
     }
 
+    private Page<QuestionVO> getQuestionVOS(Pageable pageable, HttpUrl.Builder urlBuilder) {
+        HttpUrl httpUrl = addPageableToParams(urlBuilder, pageable).build();
+        Response response = get(httpUrl);
+        return getResponseBody(response, PageableQuestionListVO.class)
+                .map(pageableQuestionListVO ->
+                        new PageImpl<>(
+                                pageableQuestionListVO.getQuestions(),
+                                parseBokToPageable(pageableQuestionListVO.getPage(), pageable),
+                                pageableQuestionListVO.getPage().getTotalElements()))
+                .orElse(new PageImpl<>(Collections.emptyList()));
+    }
+
     private Response get(HttpUrl httpUrl) {
         return requestWithMethod("GET", httpUrl, null);
     }
@@ -313,16 +319,22 @@ public class SeecoderBokClient
     private HttpUrl.Builder addPageableToParams(HttpUrl.Builder builder, Pageable pageable) {
         return Optional.ofNullable(pageable)
                 .map(notNullPageable ->
-                        builder.addQueryParameter("page", String.valueOf(parsePageableNumberToBok(notNullPageable.getPageNumber())))
-                                .addQueryParameter("total", notNullPageable.getSort().toString())
+                        // TODO: not accept error
+                        builder.addQueryParameter("page", String.valueOf(parsePageableNumberToBok(notNullPageable.getPageNumber()) + 1))
+                                .addQueryParameter("sort", parseSort(notNullPageable.getSort()))
                                 .addQueryParameter("size", String.valueOf(notNullPageable.getPageSize()))
                 )
                 .orElse(builder);
     }
 
-    private String joinComma(List<String> list) {
+    private String joinComma(Iterable<String> list) {
         return Optional.ofNullable(list)
                 .map(notNullList -> String.join(",", notNullList))
                 .orElse("");
     }
+
+    private String parseSort(Sort sort) {
+        return String.join("|",
+                sort.map(order -> order.getProperty() + "," + order.getDirection()));
+    }
 }

+ 16 - 3
seecoder-bok-client/src/test/java/cn/seecoder/bok/client/SeecoderBokClientTest.java

@@ -5,9 +5,15 @@ import cn.seecoder.bok.common.vo.QuestionVO;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.opentest4j.AssertionFailedError;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
 
 import java.text.MessageFormat;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
 
@@ -25,21 +31,26 @@ class SeecoderBokClientTest {
                 .filter(node -> node.getAbbreviation().equals("SR.RV.CM"))
                 .findFirst()
                 .orElseThrow(() -> new AssertionFailedError("SR.RV.CM not found."));
-        assertEquals(one.getName(), "变更管理");
+        assertEquals("变更管理", one.getName());
     }
 
     @Test
     void getQuestionList() {
-        Page<QuestionVO> questionList = seecoderBokClient.getQuestionList(null, null, null, null);
-        System.out.println(questionList);
+        Pageable pageable = PageRequest.of(0, 10, Sort.by(Sort.Order.asc("id")));
+        Page<QuestionVO> questionList = seecoderBokClient.getQuestionList("软工", null, null, pageable);
+        System.out.println(questionList.getContent());
+        assertTrue(questionList.getContent().stream().anyMatch(questionVO -> questionVO.getId().equals("1017")));
     }
 
     @Test
     void createQuestion() {
+
     }
 
     @Test
     void getQuestionById() {
+        QuestionVO questionVO = seecoderBokClient.getQuestionById("1017").get();
+        assertEquals("1017", questionVO.getId());
     }
 
     @Test
@@ -52,6 +63,8 @@ class SeecoderBokClientTest {
 
     @Test
     void getQuestionListByIds() {
+        List<QuestionVO> questions = seecoderBokClient.getQuestionListByIds(Arrays.asList("1017", "1018"));
+        assertEquals(2, questions.size());
     }
 
     @Test

+ 1 - 1
seecoder-bok-common/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>seecoder-bok</artifactId>
         <groupId>cn.seecoder</groupId>
-        <version>0.0.1</version>
+        <version>0.0.2</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>