Home > Back-end >  java: Can't check this string
java: Can't check this string

Time:04-06

Can’t convert String into json, and it seems that it will be superfluous for the entire string.
Was thinking maybe json might have helped me out here, but it doesn't seem to give me what I want or I don't know how it will be work.
How I can check the string?

I need to check:

  • METHOD: GET and URL: http://google.com/

  • also to check the BODY contains the fields userId, replId and view (no values, only keys)

I was trying to find a way to check that:

if (msg.contains("METHOD: GET") && msg.contains("URL: http://google.com/") && msg.contains("BODY: etc...")) {
        System.out.println("ok");
    }

It doesn't work. Some values from BODY that are dynamic and that's why for BODY the check won't pass if it’s so hardcoded String. And I guess there're any better ways to do that.

I'd like to have something like:

Assert.assertEquals(
   msg, 
   the expected value for METHOD, which contains GET); // same here for URL: http://google.com/                          

Assert.assertEquals(
   msg, 
   the expected value for BODY that has userId, replId, and view fields); // or make this assertion for each field separately, such as there is an assertion for the userId field, the same assertions for replId and view

And here's the String:

String msg = "METHOD: GET\n"  
                 "URL: http://google.com/\n"  
                 "token: 32Asdd1QQdsdsg$ff\n"  
                 "code: 200\n"  
                 "stand: test\n"  
                 "BODY: {\"userId\":\"11022:7\",\"bdaId\":\"110220\",\"replId\":\"fffDss0400rDF\",\"local\":\"not\",\"ttpm\":\"000\",\"view\":true}";

I can't think of any way to check that. Any ideas?

CodePudding user response:

You can use the java.util.List Interface (of type String) and place the string contents into that list. Then you can use the List#contains() method, for example:

String msg = "METHOD: GET\n"  
         "URL: http://google.com/\n"  
         "token: 32Asdd1QQdsdsg$ff\n"  
         "code: 200\n"  
         "stand: test\n"  
         "BODY: {\"userId\":\"11022:7\",\"bdaId\":\"110220\",\"replId\":\"fffDss0400rDF\",\"local\":\"not\",\"ttpm\":\"000\",\"view\":true}";

// Split contents of msg into list.
java.util.List<String> list = Arrays.asList(msg.split("\n"));
    
if (list.contains("METHOD: GET")) {
    System.out.println("YUP! Got: --> 'METHOD: GET'");
}
else { 
    System.out.println("NOPE! Don't have: --> 'METHOD: GET'");
}

CodePudding user response:

I've tried to use Assert:

String[] arr1 = msg.split("\n");
        Map<String, String> allFieldsMessage = new HashMap<>();
        for (String s : arr1) {
            String key = s.trim().split(": ")[0];
            String value = s.trim().split(": ")[1];
            allFieldsMessage.put(key, value);
        }
        Assert.assertEquals(
                allFieldsMessage.get("METHOD"),
                "GET"
        );

And the same for URL. But my problem is in BODY part. I thought maybe try to parse this particular part of String into json and then only check the necessary keys.

  • Related