@@ -73,6 +73,12 @@ | |||||
<groupId>org.projectlombok</groupId> | <groupId>org.projectlombok</groupId> | ||||
<artifactId>lombok</artifactId> | <artifactId>lombok</artifactId> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>org.assertj</groupId> | |||||
<artifactId>assertj-guava</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | </dependencies> | ||||
<build> | <build> | ||||
@@ -29,11 +29,12 @@ public interface WxCpDepartmentService { | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 部门管理接口 - 查询所有部门 | |||||
* 详情请见: 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 | |||||
* </pre> | * </pre> | ||||
* @param id 部门id。获取指定部门及其下的子部门。非必需,可为null | |||||
*/ | */ | ||||
List<WxCpDepart> listAll() throws WxErrorException; | |||||
List<WxCpDepart> list(Integer id) throws WxErrorException; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
@@ -48,13 +48,13 @@ public class WxCpDepartmentServiceImpl implements WxCpDepartmentService { | |||||
} | } | ||||
@Override | @Override | ||||
public List<WxCpDepart> listAll() throws WxErrorException { | |||||
public List<WxCpDepart> list(Integer id) throws WxErrorException { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; | String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; | ||||
if (id != null) { | |||||
url += "?id=" + id; | |||||
} | |||||
String responseContent = this.mainService.get(url, null); | String responseContent = this.mainService.get(url, null); | ||||
/* | |||||
* 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} } | |||||
* 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] } | |||||
*/ | |||||
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); | JsonElement tmpJsonElement = new JsonParser().parse(responseContent); | ||||
return WxCpGsonBuilder.INSTANCE.create() | return WxCpGsonBuilder.INSTANCE.create() | ||||
.fromJson(tmpJsonElement.getAsJsonObject().get("department"), | .fromJson(tmpJsonElement.getAsJsonObject().get("department"), | ||||
@@ -8,6 +8,7 @@ import org.testng.annotations.*; | |||||
import java.util.List; | import java.util.List; | ||||
import static org.assertj.core.api.Assertions.assertThat; | |||||
import static org.testng.Assert.*; | import static org.testng.Assert.*; | ||||
/** | /** | ||||
@@ -34,16 +35,24 @@ public class WxCpDepartmentServiceImplTest { | |||||
System.out.println(departId); | 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("=================获取部门"); | System.out.println("=================获取部门"); | ||||
List<WxCpDepart> departList = this.wxCpService.getDepartmentService().listAll(); | |||||
assertNotNull(departList); | |||||
assertTrue(departList.size() > 0); | |||||
List<WxCpDepart> departList = this.wxCpService.getDepartmentService().list(id); | |||||
assertThat(departList).isNotEmpty(); | |||||
for (WxCpDepart g : departList) { | for (WxCpDepart g : departList) { | ||||
this.depart = g; | this.depart = g; | ||||
System.out.println(this.depart.getId() + ":" + this.depart.getName()); | System.out.println(this.depart.getId() + ":" + this.depart.getName()); | ||||
assertNotNull(g.getName()); | |||||
assertThat(g.getName()).isNotBlank(); | |||||
} | } | ||||
} | } | ||||