From 0c7a472a16a128ed79d18b9613d301ab79c8b81b Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 18 Mar 2018 14:25:22 +0800 Subject: [PATCH] =?UTF-8?q?#456=20=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=83=A8=E9=97=A8=E5=88=97=E8=A1=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=96=B9=E6=B3=95listAll=E4=BF=AE=E6=94=B9=E4=B8=BAli?= =?UTF-8?q?st=EF=BC=8C=E4=BB=A5=E6=94=AF=E6=8C=81=E6=8C=89=E9=83=A8?= =?UTF-8?q?=E5=88=86id=E8=8E=B7=E5=8F=96=E5=85=B6=E4=B8=8B=E5=B1=9E?= =?UTF-8?q?=E9=83=A8=E9=97=A8=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weixin-java-cp/pom.xml | 6 ++++++ .../weixin/cp/api/WxCpDepartmentService.java | 7 ++++--- .../api/impl/WxCpDepartmentServiceImpl.java | 10 ++++----- .../impl/WxCpDepartmentServiceImplTest.java | 21 +++++++++++++------ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/weixin-java-cp/pom.xml b/weixin-java-cp/pom.xml index 5060cbe81..ba35ea973 100644 --- a/weixin-java-cp/pom.xml +++ b/weixin-java-cp/pom.xml @@ -73,6 +73,12 @@ org.projectlombok lombok + + + org.assertj + assertj-guava + test + diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java index 7ca1ade3e..f981f0352 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java @@ -29,11 +29,12 @@ public interface WxCpDepartmentService { /** *
-   * 部门管理接口 - 查询所有部门
-   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
+   * 部门管理接口 - 查询部门
+   * 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E5.88.97.E8.A1.A8
    * 
+ * @param id 部门id。获取指定部门及其下的子部门。非必需,可为null */ - List listAll() throws WxErrorException; + List list(Integer id) throws WxErrorException; /** *
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
index 82cd8c345..d836517fa 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
@@ -48,13 +48,13 @@ public class WxCpDepartmentServiceImpl implements WxCpDepartmentService {
   }
 
   @Override
-  public List listAll() throws WxErrorException {
+  public List list(Integer id) throws WxErrorException {
     String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
+    if (id != null) {
+      url += "?id=" + id;
+    }
+
     String responseContent = this.mainService.get(url, null);
-    /*
-     * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} }
-     * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] }
-     */
     JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
     return WxCpGsonBuilder.INSTANCE.create()
       .fromJson(tmpJsonElement.getAsJsonObject().get("department"),
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java
index 5e2502082..522d169cf 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java
@@ -8,6 +8,7 @@ import org.testng.annotations.*;
 
 import java.util.List;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.testng.Assert.*;
 
 /**
@@ -34,16 +35,24 @@ public class WxCpDepartmentServiceImplTest {
     System.out.println(departId);
   }
 
-  @Test
-  public void testListAll() throws Exception {
+  @DataProvider
+  public Object[][] departIds(){
+    return new Object[][]{
+      {null},
+      {1},
+      {5}
+    };
+  }
+
+  @Test(dataProvider = "departIds")
+  public void testList(Integer id) throws Exception {
     System.out.println("=================获取部门");
-    List departList = this.wxCpService.getDepartmentService().listAll();
-    assertNotNull(departList);
-    assertTrue(departList.size() > 0);
+    List departList = this.wxCpService.getDepartmentService().list(id);
+    assertThat(departList).isNotEmpty();
     for (WxCpDepart g : departList) {
       this.depart = g;
       System.out.println(this.depart.getId() + ":" + this.depart.getName());
-      assertNotNull(g.getName());
+      assertThat(g.getName()).isNotBlank();
     }
   }