mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	调整部分素材管理的单元测试代码
This commit is contained in:
		| @ -50,7 +50,10 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||||||
|   @Override |   @Override | ||||||
|   public File mediaDownload(String media_id) throws WxErrorException { |   public File mediaDownload(String media_id) throws WxErrorException { | ||||||
|     String url = MEDIA_API_URL_PREFIX + "/get"; |     String url = MEDIA_API_URL_PREFIX + "/get"; | ||||||
|     return this.wxMpService.execute(new MediaDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, "media_id=" + media_id); |     return this.wxMpService.execute( | ||||||
|  |       new MediaDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), | ||||||
|  |       url, | ||||||
|  |       "media_id=" + media_id); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|  | |||||||
| @ -1,26 +1,6 @@ | |||||||
| package me.chanjar.weixin.mp.api.impl; | package me.chanjar.weixin.mp.api.impl; | ||||||
|  |  | ||||||
| import static org.junit.Assert.assertEquals; |  | ||||||
| import static org.junit.Assert.assertNotNull; |  | ||||||
| import static org.junit.Assert.assertTrue; |  | ||||||
|  |  | ||||||
| import java.io.File; |  | ||||||
| import java.io.IOException; |  | ||||||
| import java.io.InputStream; |  | ||||||
| import java.util.ArrayList; |  | ||||||
| import java.util.HashMap; |  | ||||||
| import java.util.Iterator; |  | ||||||
| import java.util.LinkedHashMap; |  | ||||||
| import java.util.List; |  | ||||||
| import java.util.Map; |  | ||||||
| import java.util.UUID; |  | ||||||
|  |  | ||||||
| import org.testng.annotations.DataProvider; |  | ||||||
| import org.testng.annotations.Guice; |  | ||||||
| import org.testng.annotations.Test; |  | ||||||
|  |  | ||||||
| import com.google.inject.Inject; | import com.google.inject.Inject; | ||||||
|  |  | ||||||
| import me.chanjar.weixin.common.api.WxConsts; | import me.chanjar.weixin.common.api.WxConsts; | ||||||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
| @ -29,11 +9,17 @@ import me.chanjar.weixin.mp.api.ApiTestModule; | |||||||
| import me.chanjar.weixin.mp.bean.WxMpMaterial; | import me.chanjar.weixin.mp.bean.WxMpMaterial; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate; | import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpMaterialNews; | import me.chanjar.weixin.mp.bean.WxMpMaterialNews; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpMaterialCountResult; | import me.chanjar.weixin.mp.bean.result.*; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpMaterialFileBatchGetResult; | import org.testng.annotations.DataProvider; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpMaterialNewsBatchGetResult; | import org.testng.annotations.Guice; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpMaterialUploadResult; | import org.testng.annotations.Test; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpMaterialVideoInfoResult; |  | ||||||
|  | import java.io.File; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.util.*; | ||||||
|  |  | ||||||
|  | import static org.junit.Assert.*; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 素材管理相关接口的测试 |  * 素材管理相关接口的测试 | ||||||
| @ -48,7 +34,7 @@ public class WxMpMaterialServiceImplTest { | |||||||
|   @Inject |   @Inject | ||||||
|   protected WxMpServiceImpl wxService; |   protected WxMpServiceImpl wxService; | ||||||
|  |  | ||||||
|   private Map<String, Map<String, Object>> media_ids = new LinkedHashMap<>(); |   private Map<String, Map<String, Object>> mediaIds = new LinkedHashMap<>(); | ||||||
|   // 缩略图的id,测试上传图文使用 |   // 缩略图的id,测试上传图文使用 | ||||||
|   private String thumbMediaId = ""; |   private String thumbMediaId = ""; | ||||||
|   // 单图文消息media_id |   // 单图文消息media_id | ||||||
| @ -59,7 +45,7 @@ public class WxMpMaterialServiceImplTest { | |||||||
|   private WxMpMaterialCountResult wxMaterialCountResultBeforeTest; |   private WxMpMaterialCountResult wxMaterialCountResultBeforeTest; | ||||||
|  |  | ||||||
|   @DataProvider |   @DataProvider | ||||||
|   public Object[][] uploadMedia() { |   public Object[][] mediaFiles() { | ||||||
|     return new Object[][] { |     return new Object[][] { | ||||||
|             new Object[] { WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, "mm.jpeg" }, |             new Object[] { WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, "mm.jpeg" }, | ||||||
|             new Object[] { WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, "mm.mp3" }, |             new Object[] { WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, "mm.mp3" }, | ||||||
| @ -68,7 +54,7 @@ public class WxMpMaterialServiceImplTest { | |||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test(dataProvider = "uploadMedia") |   @Test(dataProvider = "mediaFiles") | ||||||
|   public void testUploadMaterial(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { |   public void testUploadMaterial(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { | ||||||
|     if (this.wxMaterialCountResultBeforeTest == null) { |     if (this.wxMaterialCountResultBeforeTest == null) { | ||||||
|       this.wxMaterialCountResultBeforeTest = this.wxService.getMaterialService() |       this.wxMaterialCountResultBeforeTest = this.wxService.getMaterialService() | ||||||
| @ -104,7 +90,7 @@ public class WxMpMaterialServiceImplTest { | |||||||
|       materialInfo.put("media_id", res.getMediaId()); |       materialInfo.put("media_id", res.getMediaId()); | ||||||
|       materialInfo.put("length", tempFile.length()); |       materialInfo.put("length", tempFile.length()); | ||||||
|       materialInfo.put("filename", tempFile.getName()); |       materialInfo.put("filename", tempFile.getName()); | ||||||
|       this.media_ids.put(res.getMediaId(), materialInfo); |       this.mediaIds.put(res.getMediaId(), materialInfo); | ||||||
|  |  | ||||||
|       System.out.println(res); |       System.out.println(res); | ||||||
|     } |     } | ||||||
| @ -172,18 +158,18 @@ public class WxMpMaterialServiceImplTest { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test(dependsOnMethods = {"testMaterialCount"}, dataProvider = "downloadMaterial") |   @Test(dependsOnMethods = {"testMaterialCount"}, dataProvider = "downloadMaterial") | ||||||
|   public void testDownloadMaterial(String media_id) throws WxErrorException, IOException { |   public void testDownloadMaterial(String mediaId) throws WxErrorException, IOException { | ||||||
|     Map<String, Object> materialInfo = this.media_ids.get(media_id); |     Map<String, Object> materialInfo = this.mediaIds.get(mediaId); | ||||||
|     assertNotNull(materialInfo); |     assertNotNull(materialInfo); | ||||||
|     String filename = materialInfo.get("filename").toString(); |     String filename = materialInfo.get("filename").toString(); | ||||||
|     if (filename.endsWith(".mp3") || filename.endsWith(".jpeg")) { |     if (filename.endsWith(".mp3") || filename.endsWith(".jpeg")) { | ||||||
|       try (InputStream inputStream = this.wxService.getMaterialService() |       try (InputStream inputStream = this.wxService.getMaterialService() | ||||||
|           .materialImageOrVoiceDownload(media_id)) { |           .materialImageOrVoiceDownload(mediaId)) { | ||||||
|         assertNotNull(inputStream); |         assertNotNull(inputStream); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     if (filename.endsWith("mp4")) { |     if (filename.endsWith("mp4")) { | ||||||
|       WxMpMaterialVideoInfoResult wxMaterialVideoInfoResult = this.wxService.getMaterialService().materialVideoInfo(media_id); |       WxMpMaterialVideoInfoResult wxMaterialVideoInfoResult = this.wxService.getMaterialService().materialVideoInfo(mediaId); | ||||||
|       assertNotNull(wxMaterialVideoInfoResult); |       assertNotNull(wxMaterialVideoInfoResult); | ||||||
|       assertNotNull(wxMaterialVideoInfoResult.getDownUrl()); |       assertNotNull(wxMaterialVideoInfoResult.getDownUrl()); | ||||||
|     } |     } | ||||||
| @ -260,9 +246,9 @@ public class WxMpMaterialServiceImplTest { | |||||||
|  |  | ||||||
|   @DataProvider |   @DataProvider | ||||||
|   public Object[][] downloadMaterial() { |   public Object[][] downloadMaterial() { | ||||||
|     Object[][] params = new Object[this.media_ids.size()][]; |     Object[][] params = new Object[this.mediaIds.size()][]; | ||||||
|     int index = 0; |     int index = 0; | ||||||
|     for (String mediaId : this.media_ids.keySet()) { |     for (String mediaId : this.mediaIds.keySet()) { | ||||||
|       params[index] = new Object[]{mediaId}; |       params[index] = new Object[]{mediaId}; | ||||||
|       index++; |       index++; | ||||||
|     } |     } | ||||||
| @ -272,7 +258,7 @@ public class WxMpMaterialServiceImplTest { | |||||||
|   @DataProvider |   @DataProvider | ||||||
|   public Iterator<Object[]> allTestMaterial() { |   public Iterator<Object[]> allTestMaterial() { | ||||||
|     List<Object[]> params = new ArrayList<>(); |     List<Object[]> params = new ArrayList<>(); | ||||||
|     for (String mediaId : this.media_ids.keySet()) { |     for (String mediaId : this.mediaIds.keySet()) { | ||||||
|       params.add(new Object[]{mediaId}); |       params.add(new Object[]{mediaId}); | ||||||
|     } |     } | ||||||
|     params.add(new Object[]{this.singleNewsMediaId}); |     params.add(new Object[]{this.singleNewsMediaId}); | ||||||
| @ -281,8 +267,8 @@ public class WxMpMaterialServiceImplTest { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   // 以下为media接口的测试 |   // 以下为media接口的测试 | ||||||
|   private List<String> mediaIds = new ArrayList<>(); |   private List<String> mediaIdsToDownload = new ArrayList<>(); | ||||||
|   @Test(dataProvider="uploadMedia") |   @Test(dataProvider="mediaFiles") | ||||||
|   public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { |   public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { | ||||||
|     try(InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)){ |     try(InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)){ | ||||||
|       WxMediaUploadResult res = this.wxService.getMaterialService().mediaUpload(mediaType, fileType, inputStream); |       WxMediaUploadResult res = this.wxService.getMaterialService().mediaUpload(mediaType, fileType, inputStream); | ||||||
| @ -290,31 +276,32 @@ public class WxMpMaterialServiceImplTest { | |||||||
|       assertNotNull(res.getCreatedAt()); |       assertNotNull(res.getCreatedAt()); | ||||||
|       assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); |       assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); | ||||||
|  |  | ||||||
|       if (res.getMediaId() != null) { |       if (res.getMediaId() != null && !mediaType.equals(WxConsts.MEDIA_VIDEO)) { | ||||||
|         this.mediaIds.add(res.getMediaId()); |         //video 不支持下载,所以不加入 | ||||||
|  |         this.mediaIdsToDownload.add(res.getMediaId()); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       if (res.getThumbMediaId() != null) { |       if (res.getThumbMediaId() != null) { | ||||||
|         this.mediaIds.add(res.getThumbMediaId()); |         this.mediaIdsToDownload.add(res.getThumbMediaId()); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       System.out.println(res); |       System.out.println(res); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia") |  | ||||||
|   public void testDownloadMedia(String media_id) throws WxErrorException { |  | ||||||
|     File file = this.wxService.getMaterialService().mediaDownload(media_id); |  | ||||||
|     assertNotNull(file); |  | ||||||
|     System.out.println(file.getAbsolutePath()); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   @DataProvider |   @DataProvider | ||||||
|   public Object[][] downloadMedia() { |   public Object[][] downloadMedia() { | ||||||
|     Object[][] params = new Object[this.mediaIds.size()][]; |     Object[][] params = new Object[this.mediaIdsToDownload.size()][]; | ||||||
|     for (int i = 0; i < this.mediaIds.size(); i++) { |     for (int i = 0; i < this.mediaIdsToDownload.size(); i++) { | ||||||
|       params[i] = new Object[] { this.mediaIds.get(i) }; |       params[i] = new Object[] { this.mediaIdsToDownload.get(i) }; | ||||||
|     } |     } | ||||||
|     return params; |     return params; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia") | ||||||
|  |   public void testDownloadMedia(String mediaId) throws WxErrorException { | ||||||
|  |     File file = this.wxService.getMaterialService().mediaDownload(mediaId); | ||||||
|  |     assertNotNull(file); | ||||||
|  |     System.out.println(file.getAbsolutePath()); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 BinaryWang
					BinaryWang