Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exctract on JSONObject #484

Closed
falkoner opened this issue Sep 7, 2015 · 4 comments
Closed

Exctract on JSONObject #484

falkoner opened this issue Sep 7, 2015 · 4 comments

Comments

@falkoner
Copy link

falkoner commented Sep 7, 2015

Hi.
As per #258 I should be able to extract values by keys in Map objects.

I'm wondering now if I can do something with the same level of convenience with JSONObjects as well, since they behave to some extent in a very similar way.

Assuming I have following JSON Array of JSON Objects:

JSONArray symbolList;

// [{"symbol":"AAPL","companyName":"Apple Inc"},{"symbol":"AA","companyName":"Alcoa Inc."},{"symbol":"AAL","companyName":"American Airlines Group Inc."},{"symbol":"AAP","companyName":"Advance Auto Parts Inc."}]

I'd like to iterate (which AssertJ does) and verify extracting values by key name:

assertThat(symbolList).extracting("symbol").contains("AAPL");

Currently it results in following exception:

(org.assertj.core.util.introspection.IntrospectionError: 
Can't find any field or property with name 'symbol'.
Error when introspecting fields was :
- No getter for property 'symbol' in org.json.simple.JSONObject 
Error when introspecting properties was :
- Unable to obtain the value of the field <'symbol'> from <{"symbol":"AAPL","companyName":"Apple Inc"}>)

Is there any way to make it work given the fact that getting the value is like that:

        JSONObject symbolObject = (JSONObject) symbolList.get(1);
        String symbol = (String) symbolObject.get("symbol");
        assertThat(symbol).contains("AAPL");

Note: I'm using com.googlecode.json-simple 1.1.1 now to work with JSON.

In general I would also reconsider decision on #75 What's currently recommended way/library to work with AssertJ and JSON objects?

@joel-costigliola
Copy link
Member

there is one problem for that : assertj-core does not use any dependencies so it can't know JSONObjects.

You can try to use an extractor (specially handy with java 8 lambdas) that implements the logic of extracting the data you need to check.

@falkoner
Copy link
Author

falkoner commented Sep 8, 2015

Thanks, @joel-costigliola That's right, my intention was mostly to confirm that I can't achieve this goal by other means other than custom assertions and custom extractors.

So just to confirm that I'm on the right path - I should use the ByMapKeyExtractor.java class as template, but use JSONObject instead of Map<K, V>, right? Here is the reference to this class: 2dcc837

I realize that assertj-core is not a proper place for that. But do you have any recommended pattern to build out and organize a library like the one for #75 to keep all the extractors etc in place for json-simple object? I anticipate that I'll have to create quite a number of them eventually.

@joel-costigliola
Copy link
Member

You are in the right track with ByMapKeyExtractor.

Regarding the JSON assertion module, follow what has been done for assertj-guava.

cheers

@joel-costigliola
Copy link
Member

I'm closing this issue as it can't be solved in assertj-core.
Feel free to add comments if you need additional help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants