skip to Main Content

I want to know json path for "/v1/test/anySales" first element in consume node. I dont want list of items from that content node but the very first element like [0].
Basically i want to use assertj java lib and want to check if first content is "application/json" or not.

  {
   "openapi":"3.0.1",
   "info":{
      "title":"swagger-app-name",
      "description":"swagger test",
      "version":"a version"
   },
   "servers":[
      {
         "url":"http://localhost",
         "description":"Generated server url"
      }
   ],
   "security":[
      {
         "bearer-jwt":[
            
         ]
      }
   ],
   "paths":{
      "/v1/test/sales":{
         "get":{
            "tags":[
               "user-controller"
            ],
            "description":"This api is **public**",
            "operationId":"getSales",
            "parameters":[
               {
                  "name":"id",
                  "in":"query",
                  "description":"sale's id",
                  "required":true,
                  "schema":{
                     "type":"string",
                     "description":"sale's id"
                  }
               }
            ],
            "responses":{
               "200":{
                  "description":"OK",
                  "content":{
                     "application/json":{
                        "schema":{
                           "$ref":"#/components/schemas/Sale"
                        }
                     }
                  }
               }
            }
         },
         "post":{
            "tags":[
               "user-controller"
            ],
            "description":"This api requires `hasAuthority('write')` permission.",
            "operationId":"updateSale",
            "requestBody":{
               "content":{
                  "application/json":{
                     "schema":{
                        "$ref":"#/components/schemas/Sale"
                     }
                  }
               }
            },
            "responses":{
               "200":{
                  "description":"OK"
               }
            }
         }
      },
      "/v1/test/anySales":{
         "post":{
            "tags":[
               "user-controller"
            ],
            "description":"This api requires `hasAuthority('write')` permission.",
            "operationId":"updateAnySale",
            "requestBody":{
               "content":{
                  "application/json":{
                     "schema":{
                        "$ref":"#/components/schemas/Sale"
                     }
                  },
                  "application/vnd.smbc.mdl+json":{
                     "schema":{
                        "$ref":"#/components/schemas/Sale"
                     }
                  },
                  "application/vnd.smbc.mdl+jsonb":{
                     "schema":{
                        "$ref":"#/components/schemas/Sale"
                     }
                  }
               }
            },
            "responses":{
               "200":{
                  "description":"OK"
               }
            }
         }
      }
   },
   "components":{
      "schemas":{
         "Sale":{
            "type":"object"
         }
      },
      "securitySchemes":{
         "bearer-jwt":{
            "type":"http",
            "name":"Authorization",
            "in":"header",
            "scheme":"bearer",
            "bearerFormat":"JWT"
         }
      }
   }
}

2

Answers


  1. Chosen as BEST ANSWER

    I am getting what i am looking for by : mapper.readTree(json).path("paths").path( "/v1/test/anySales").path("post").path("requestBody").path("content")

    any better approach use path expression?

      // Collect keys from contentNode while preserving order
        List<String> actualKeys = new ArrayList<>();
        if (contentNode.isObject()) {
            Iterator<String> fieldNamesIterator = contentNode.fieldNames();
            fieldNamesIterator.forEachRemaining(actualKeys::add);
        }
    
        // Define the expected keys in the correct order
        List<String> expectedKeys = List.of("application/json", "application/vnd.smbc.mdl+json", "application/vnd.smbc.mdl+jsonb");
    
        // Validate the keys and their order using AssertJ
        Assertions.assertThat(actualKeys)
                .as("Check if contentNode has expected keys in the correct order")
                .containsExactlyElementsOf(expectedKeys);
            assertThat(((Map<String, Object>) node.getValue()).entrySet().iterator().next())
                .isEqualTo(new SimpleEntry<>("b", valueOf(1)));
    

  2.     {
      "$Type" : "com.smbc.cm.polaris.pricer.OnDemandPSResponse",
      "$MdlMeta" : {
        "Version" : "75.54.0"
      },
      "QuoteId" : "2985787805921335133",
      "PricerSessionResults" : {
        "8c662fdf-e1c9-438f-9b5a-620e9fc7189f" : {
          "$Type" : "com.smbc.cm.polaris.pricer.PricerSessionResult",
          "TradeUniqueId" : "8c662fdf-e1c9-438f-9b5a-620e9fc7189f",
          "PricerResult" : {
            "$Object" : "com.smbc.cm.polaris.graph.pricer.PricerResult",
            "Key" : {
              "$Key" : "com.smbc.cm.polaris.graph.pricer.PricerResultKey",
              "Type" : "TRADE",
              "Source" : "Prism",
              "Identifier" : "8c662fdf-e1c9-438f-9b5a-620e9fc7189f"
            },
            "Results" : {
              "Solver" : [ {
                "$Type" : "com.smbc.cm.polaris.graph.pricer.SolverCalcResult",
                "PricerCalcRequestId" : {
                  "$Type" : "com.smbc.cm.polaris.session.PricerCalcRequestId",
                  "RequestId" : 1
                },
                "SolverResults" : {
                  "SolverResult" : 0.03910800164300038,
                  "FixLegBPS" : 83499.97673158761
                }
              } ]
            }
          }
         }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search