0

For unit tests it would be nice to get a product from JSON data.

[The question originated from a company internal support request.]

edit:

A normal JSON mapper would not work due to some special formats. The question is not about Jackson vs. Gson or sth.

Dalek
  • 148
  • 8
  • It is considered off topic to ask a question recommending a product, so I am not sure this answer fit here either. – Peter Lawrey Feb 22 '16 at 12:55
  • It is not a trivial Jackson/Gson question, using a plain Jackson mapper would work to deserialize commercetools products, since there are some custom Jackson modules involved (for time and money) as well as modules which are not enabled by default like jackson-module-parameter-names. Without it a normal Jackson mapper just fails due to missing annotations in the product class. – Dalek Feb 22 '16 at 13:34

1 Answers1

1

It is documented here: http://sphereio.github.io/sphere-jvm-sdk/javadoc/master/io/sphere/sdk/meta/TestingDocumentation.html#object-test-doubles

But here is an example for a simple case:

final String pathToJsonFile = "path/to/file/in/resource/folder/product.json";
final Product product = SphereJsonUtils.
   readObjectFromResource(pathToJsonFile, Product.class);

To get the JSON you can create a product in the platform and then query it via https://impex.sphere.io/login?url=/playground (use your Merchant Center credentials) by querying products.

Keep in Mind to select "products" and not "product projections" in case you want to create "Product" instances. This approach also works for the "ProductDraft" class.

Dalek
  • 148
  • 8
  • This is just one amongst many. The most popular one is Jackson and Gson. We (Chronicle) have our own low latency one. – Peter Lawrey Feb 22 '16 at 12:55
  • It is not about general JSON mapping it is about deserializing a specific classes of the commercetools JVM SDK. Normal Jackson/Gson mappers or even your mapper would not work since a lot of weird things are coded as String and need an explicit mapping to a class/interface/enum instance. See also the comment on the question, – Dalek Feb 22 '16 at 13:38