1
\$\begingroup\$

I want to refactor this in Java to reduce my code duplication.

public HashMap<String,String> setupProductsUid() throws Exception {
    OutputMessage actual = statusUpdaterTest.getOutputMessageWithUidOfNewProductsNode();
    HashMap<String,String> map = new HashMap<>();
    map.put("replaceThisProductsUid", actual.getQuery().get(0).getUid());
    return map;

}
public HashMap<String,String> setupProductsAndProductUid() throws Exception {
    OutputMessage actual = statusUpdaterTest.getOutputMessageWithUidOfNewProductsNode();
    HashMap<String,String> map = new HashMap<>();
    map.put("replaceThisProductsUid", actual.getQuery().get(0).getUid());
    map.put("replaceThisProductUid", actual.getQuery().get(0).getProducts().get(0).getUid());
    return map;

}
public HashMap<String,String> setupProductsAndProductAndOptionsUid() throws Exception {
    OutputMessage actual = statusUpdaterTest.getOutputMessageWithUidOfNewProductsNode();
    HashMap<String,String> map = new HashMap<>();
    map.put("replaceThisProductsUid", actual.getQuery().get(0).getUid());
    map.put("replaceThisProductUid", actual.getQuery().get(0).getProducts().get(0).getUid());
    map.put("replaceThisOptionUid1", actual.getQuery().get(0).getProducts().get(0).getOptions().get(0).getUid());
    return map;

}
public HashMap<String,String> setupProductsAndProductAndOptionsMultipleUids() throws Exception {
    OutputMessage actual = statusUpdaterTest.getOutputMessageWithUidOfNewProductsNode();
    HashMap<String,String> map = new HashMap<>();
    map.put("replaceThisProductsUid", actual.getQuery().get(0).getUid());
    map.put("replaceThisProductUid", actual.getQuery().get(0).getProducts().get(0).getUid());
    map.put("replaceThisOptionUid1", actual.getQuery().get(0).getProducts().get(0).getOptions().get(0).getUid());
    map.put("replaceThisOptionUid2", actual.getQuery().get(0).getProducts().get(0).getOptions().get(1).getUid());
    map.put("replaceThisOptionUid3", actual.getQuery().get(0).getProducts().get(0).getOptions().get(2).getUid());
    return map;
}

Edit: To provide some context, OutputMessage is a deserialized representation of a JSON data response from Dgraph. The data coming back from Dgraph looks like this:

{
    "uid": "0x2938",
    "collectionId": 1,
    "products": [
      {
        "uid": "0x345",
        "productId": 19610626,
        "options": [
          {
            "uid": "0x45256",
            "optionId": 32661491,
            "datetime": "2018-10-31T10:19:32.242",
            "expected": true
          }
        ]
      }
    ]
}

I have no way to prevent Dgraph from creating arrays for single-object parent-child relationships. It's just the way they have architected the node-edge structure in their graph.

Edit 2: These functions are getting called by a method that substitutes the keys for the values in the map in test JSON data. The reason for the substitution is that I need a way to obtain UIDs from Dgraph (because the IDs are managed by Dgraph, so I need to query Dgraph after creating the Dgraph nodes in order to find out what UIDs Dgraph will give me for the nodes).

Here's the method that replaces placeholders in the JSON data:

public String updateIncomingJsonByReplacingMapValues(HashMap<String, String> map, String incomingJsonWithoutUids) {
    for (Entry<String, String> entry : map.entrySet()) {
        incomingJsonWithoutUids = incomingJsonWithoutUids.replace(entry.getKey(), entry.getValue());
    }
    return incomingJsonWithoutUids;
}

Here's an example of a test method that calls the function that replaces placeholders with test data:

@Test
public void incomingJsonIsEmptyProductsCollectionWithOnlyProductsUid() throws Exception{
    HashMap<String,String> map = setupProductsUid();
    String incomingJsonWithoutUids = statusUpdaterTest.readTestFile("dgraph_writer_test/02_empty_products_node_in_dgraph.json");

    // Update placeholders with Uid's
    String updatedJson = updateIncomingJsonByReplacingMapValues(map, incomingJsonWithoutUids);
    // Then, write the updated Json to Dgraph and check that the returned UIDs are as expected.
    DgraphClient dgraphClient = statusUpdaterTest.createDgraphClient(false);
    Map<String, String> uidMap = mutate(dgraphClient, updatedJson);
    Assert.assertNotNull(uidMap);
    Assert.assertTrue(uidMap.isEmpty());
}

The file "dgraph_writer_test/02_empty_products_node_in_dgraph.json" contains this data:

{
    "uid": "replaceThisProductsUid",
    "collectionId": 1,
    "products": [
    ]
}

Other JSON examples have a lot more data than this one.

\$\endgroup\$
  • 1
    \$\begingroup\$ Please provide more information so that we can understand your situation and give you proper advice. What does the code for statusUpdaterTest look like? Do these methods really need to return HashMaps, or would any Map<String,String> suffice? \$\endgroup\$ – 200_success Aug 7 '19 at 7:01
  • 2
    \$\begingroup\$ @devinbost Your question was "How can I refactor this with functional programming in Java to reduce my duplication?" Which goes beyond the scope of Code Review. WRT the question, your code did not work. There is no need for you to get defensive. Just move the question to Stack Overflow. \$\endgroup\$ – TorbenPutkonen Aug 8 '19 at 5:20
  • 3
    \$\begingroup\$ @TorbenPutkonen How much more would I need to provide for the code to be considered "working code"? The code does not throw exceptions, it completes the desired task, and it runs without errors. The question is how to improve the code, plain and simple. \$\endgroup\$ – devinbost Aug 14 '19 at 4:57
  • 2
    \$\begingroup\$ @devinbost The "working code" needs to be taken in the context of what you are asking for. You were asking about functional programming and your code had no FP. Therefore it could not be considered working. It's correct place would thus have been Stack Overflow. Once you have it implemented using FP, you can come here to ask for review of your FP implementation. \$\endgroup\$ – TorbenPutkonen Aug 14 '19 at 7:57
  • 1
    \$\begingroup\$ @TorbenPutkonen Is that actually stated in the instructions for codereview.stackexchange.com? \$\endgroup\$ – devinbost Aug 15 '19 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.