@@ -147,7 +147,7 @@ public static JSONObject initAccess(boolean shutdownWhenServerError, APIJSONCrea
147147 }
148148 APIJSON_CREATOR = creator ;
149149
150-
150+
151151 boolean isAll = table == null || table .isEmpty ();
152152
153153 JSONObject access = isAll ? new JSONRequest () : table ;
@@ -160,7 +160,7 @@ public static JSONObject initAccess(boolean shutdownWhenServerError, APIJSONCrea
160160 JSONRequest request = new JSONRequest ();
161161 request .putAll (accessItem .toArray (0 , 0 , ACCESS_ ));
162162
163-
163+
164164 JSONObject response = creator .createParser ().setMethod (RequestMethod .GET ).setNeedVerify (false ).parseResponse (request );
165165 if (JSONResponse .isSuccess (response ) == false ) {
166166 Log .e (TAG , "\n \n \n \n \n !!!! 查询权限配置异常 !!!\n " + response .getString (JSONResponse .KEY_MSG ) + "\n \n \n \n \n " );
@@ -277,7 +277,7 @@ public static JSONObject initRequest(boolean shutdownWhenServerError, APIJSONCre
277277 }
278278 APIJSON_CREATOR = creator ;
279279
280-
280+
281281 boolean isAll = table == null || table .isEmpty ();
282282
283283 JSONRequest requestItem = new JSONRequest ();
@@ -380,61 +380,79 @@ public int compare(Integer o1, Integer o2) {
380380 }
381381
382382
383- public static void test () {
383+ public static void test () throws Exception {
384384 testStructure ();
385385 }
386386
387- static final String requestString = "{\" Comment\" :{\" REFUSE\" : \" id\" , \" MUST\" : \" userId,momentId,content\" }, \" INSERT\" :{\" Comment:to \" :{} }}" ;
388- static final String responseString = "{\" User\" :{\" REMOVE\" : \" phone\" , \" REPLACE\" :{\" sex\" :2}, \" INSERT\" :{\" name\" :\" api\" }} , \" UPDATE\" :{\" Comment:to \" :{ }}}" ;
387+ static final String requestConfig = "{\" Comment\" :{\" REFUSE\" : \" id\" , \" MUST\" : \" userId,momentId,content\" }, \" INSERT\" :{\" @role \" :\" OWNER \" }}" ;
388+ static final String responseConfig = "{\" User\" :{\" REMOVE\" : \" phone\" , \" REPLACE\" :{\" sex\" :2}, \" INSERT\" :{\" name\" :\" api\" }, \" UPDATE\" :{\" verifyURLList-() \" :\" verifyURLList(pictureList) \" }}}" ;
389389 /**
390390 * 测试 Request 和 Response 的数据结构校验
391+ * @throws Exception
391392 */
392- public static void testStructure () {
393+ public static void testStructure () throws Exception {
393394 JSONObject request ;
394395 try {
395396 request = JSON .parseObject ("{\" Comment\" :{\" userId\" :0}}" );
396- Log .d (TAG , "test verifyRequest = " + AbstractVerifier .verifyRequest (RequestMethod .POST , "" , JSON .parseObject (requestString ), request , APIJSON_CREATOR ));
397- } catch (Exception e ) {
398- e .printStackTrace ();
397+ Log .d (TAG , "test verifyRequest = " + AbstractVerifier .verifyRequest (RequestMethod .POST , "" , JSON .parseObject (requestConfig ), request , APIJSON_CREATOR ));
398+ } catch (Throwable e ) {
399+ if (e instanceof IllegalArgumentException == false || "POST请求,Comment 里面不能缺少 momentId 等[userId,momentId,content]内的任何字段!" .equals (e .getMessage ()) == false ) {
400+ throw e ;
401+ }
402+ Log .d (TAG , "测试 Operation.MUST 校验缺少字段:成功" );
399403 }
400- try {
401- request = JSON .parseObject ("{\" Comment\" :{\" userId\" :0, \" momentId\" :0, \" content\" :\" apijson\" }}" );
402- Log .d (TAG , "test verifyRequest = " + AbstractVerifier .verifyRequest (RequestMethod .POST , "" , JSON .parseObject (requestString ), request , APIJSON_CREATOR ));
403- } catch (Exception e ) {
404- e .printStackTrace ();
404+ try {
405+ request = JSON .parseObject ("{\" Comment\" :{\" id\" :0, \" userId\" :0, \" momentId\" :0, \" content\" :\" apijson\" }}" );
406+ Log .d (TAG , "test verifyRequest = " + AbstractVerifier .verifyRequest (RequestMethod .POST , "" , JSON .parseObject (requestConfig ), request , APIJSON_CREATOR ));
407+ } catch (Throwable e ) {
408+ if (e instanceof IllegalArgumentException == false || "POST请求,/Comment 不能传 id !" .equals (e .getMessage ()) == false ) {
409+ throw e ;
410+ }
411+ Log .d (TAG , "测试 Operation.REFUSE 校验不允许传字段:成功" );
405412 }
406413 try {
407- request = JSON .parseObject ("{\" Comment\" :{\" id\" :0, \" userId\" :0, \" momentId\" :0, \" content\" :\" apijson\" }}" );
408- Log .d (TAG , "test verifyRequest = " + AbstractVerifier .verifyRequest (RequestMethod .POST , "" , JSON .parseObject (requestString ), request , APIJSON_CREATOR ));
409- } catch (Exception e ) {
410- e .printStackTrace ();
414+ request = JSON .parseObject ("{\" Comment\" :{\" userId\" :0, \" momentId\" :0, \" content\" :\" apijson\" }}" );
415+ Log .d (TAG , "test verifyRequest = " + AbstractVerifier .verifyRequest (RequestMethod .POST , "" , JSON .parseObject (requestConfig ), request , APIJSON_CREATOR ));
416+ AssertUtil .assertEqual ("OWNER" , request .getString ("@role" ));
417+ Log .d (TAG , "测试 Operation.INSERT 不存在字段时插入:成功" );
418+ } catch (Throwable e ) {
419+ throw e ;
411420 }
412421
413422
423+
414424 JSONObject response ;
415425 try {
416426 response = JSON .parseObject ("{\" User\" :{\" userId\" :0}}" );
417- Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseString ), response , APIJSON_CREATOR , null ));
418- } catch (Exception e ) {
419- e .printStackTrace ();
427+ Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseConfig ), response , APIJSON_CREATOR , null ));
428+ AssertUtil .assertEqual ("verifyURLList(pictureList)" , response .getJSONObject ("User" ).getString ("verifyURLList-()" ));
429+ Log .d (TAG , "测试 Operation.UPDATE 强制插入/替换:成功" );
430+ } catch (Throwable e ) {
431+ throw e ;
420432 }
421433 try {
422434 response = JSON .parseObject ("{\" User\" :{\" userId\" :0, \" phone\" :\" 12345678\" }}" );
423- Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseString ), response , APIJSON_CREATOR , null ));
424- } catch (Exception e ) {
425- e .printStackTrace ();
435+ Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseConfig ), response , APIJSON_CREATOR , null ));
436+ AssertUtil .assertEqual (null , response .getJSONObject ("User" ).get ("phone" ));
437+ Log .d (TAG , "测试 Operation.REMOVE 强制移除:成功" );
438+ } catch (Throwable e ) {
439+ throw e ;
426440 }
427441 try {
428442 response = JSON .parseObject ("{\" User\" :{\" userId\" :0, \" phone\" :\" 12345678\" , \" sex\" :1}}" );
429- Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseString ), response , APIJSON_CREATOR , null ));
430- } catch (Exception e ) {
431- e .printStackTrace ();
443+ Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseConfig ), response , APIJSON_CREATOR , null ));
444+ AssertUtil .assertEqual ("api" , response .getJSONObject ("User" ).get ("name" ));
445+ Log .d (TAG , "测试 Operation.INSERT 不存在字段时插入:成功" );
446+ } catch (Throwable e ) {
447+ throw e ;
432448 }
433449 try {
434450 response = JSON .parseObject ("{\" User\" :{\" id\" :0, \" name\" :\" tommy\" , \" phone\" :\" 12345678\" , \" sex\" :1}}" );
435- Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseString ), response , APIJSON_CREATOR , null ));
436- } catch (Exception e ) {
437- e .printStackTrace ();
451+ Log .d (TAG , "test verifyResponse = " + AbstractVerifier .verifyResponse (RequestMethod .GET , "" , JSON .parseObject (responseConfig ), response , APIJSON_CREATOR , null ));
452+ AssertUtil .assertEqual (2 , response .getJSONObject ("User" ).get ("sex" ));
453+ Log .d (TAG , "测试 Operation.REPLACE 存在字段时替换:成功" );
454+ } catch (Throwable e ) {
455+ throw e ;
438456 }
439457
440458 }
0 commit comments