how to retrieve orders from ordertools component in atg or how to test orderlookup droplet api

2.2k views Asked by At

iam trying to orderlookup droplet API by passing some parameters.I assume that the parameters which are mandatory is userId and organisationIds which i have passed and additionally i have also passed "state" parameter.All these params are passed thru request and then the service method of droplet is invoked.But the service method returns nothing.My goal is to check whether this droplet this retrieving the expected set of orders or not.We can use droplet invoker but i tried that way but it didnt work may be i missed something.Please help me out!! this is my code when i tried to use OrderLookUp API

DynamoHttpServletRequest request = ServletUtil.getCurrentRequest();

    mTestService.setCurrentRequest(request);
    if (request == null) {
        mTestService.vlogError("Request is null.");
        Assert.fail("Request is null ");
    }
    else
    {
        Object droplet = mTestService
                .getRequestScopedComponent("OrderLookupDroplet");

        OrderLookupDroplet=(OrderLookup) droplet;
        request.setParameter("state", "submitted");
        request.setParameter("organisationIds", organizationIds);
        request.setParameter("userId", userId);
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        DynamoHttpServletRequest dynRequest = (DynamoHttpServletRequest) request;
        TestingDynamoHttpServletRequest wrappedRequest = new TestingDynamoHttpServletRequest(
                dynRequest, buffer);
        TestingDynamoHttpServletResponse wrappedResponce = new TestingDynamoHttpServletResponse(
                dynRequest.getResponse());
         OrderLookupDroplet.service(wrappedRequest, wrappedResponce);
    }

the above sample is only part of the code..

this is the code when i tried using droplet invoker

DropletInvoker invoker = new DropletInvoker(mNucleus);
        invoker.getRequest().setParameter("state", "submitted");
      //  String [] siteIds = {"siteA", "siteB"};
      //  invoker.getRequest().setParameter("siteIds", Arrays.asList(siteIds));
        String [] organizationIds = {"OrgA", "OrgB"};
        invoker.getRequest().setParameter("organizationIds", organizationIds);
        String [] orderIds = {"orderautouser001OrgA" , "orderautouser001OrgB"};
        invokeDroplet(invoker, "autouser001", orderIds);
......

protected void invokeDroplet(DropletInvoker pInvoker, String pUserId, String[] pOrderIds) throws Exception
  {
    Map<String, Object> localParams = new HashMap();
    localParams.put("userId", pUserId);
    DropletResult result = pInvoker.invokeDroplet("/atg/commerce/order/OrderLookup", localParams);
    RenderedOutputParameter oparam = result.getRenderedOutputParameter("output", 0);

    assertNotNull("'output' oparam was not rendered", oparam);

    assertEquals("Check totalCount.", pOrderIds.length, oparam.getFrameParameter("totalCount"));

    List<Order> orders = (List<Order>)oparam.getFrameParameter("result");
    assertEquals("Check order array length.", pOrderIds.length, orders.size());
    for (int index = 0; index < pOrderIds.length; index++) {
      boolean found = false;
      for (Order order: orders) {
        if (pOrderIds[index].equals(order.getId())) {
          found = true;
          break;
        }
      }
      assertTrue("Expected orderId " + pOrderIds[index] + " not found in result array", found);
    }

in first case i donno how to retrieve the orders by directly using orderlookup api....and in second case though i know how to use it ,iam still failing!! please help me out..thanks in advance

3

There are 3 answers

1
Vihung On

I assume from your code example, and the fact that you mention DropletInvoker that you are writing a unit test, and that this is not functional code.

If it is functional code, you really, really, should not invoke a droplet from another Nucleus component. A droplet exists solely to be used in a JSP page. If you need the functionality of the droplet in Java code, you should refactor the droplet into a service that holds the main logic, and a droplet that simply acts as a façade to the service to allow it to be invoked from a page.

In the case of the OrderLookup look droplet, you don't need to refactor anything. The service to use should be OrderManager or OrderTools depending on what you need. Note, there is a difference between Order objects and Order repository items, and you should prefer to use order objects - so only use the Order Repository directly if you really need to.

0
Mani manasa mylavarapu On

Thanks for giving your opinions.Good news is we can invoke droplets from any other API

OrderLookup droplet = (OrderLookup) sNucleus.resolveName("/atg/commerce/order/OrderLookup");
        ServletTestUtils utils = new ServletTestUtils();
        mRequest = utils.createDynamoHttpServletRequestForSession(sNucleus, null, null);
        ServletUtil.setCurrentRequest(mRequest);
        mResponse = new DynamoHttpServletResponse();
        mRequest.setResponse(mResponse);
        mResponse.setRequest(mRequest);
        mResponse.setResponse(new GenericHttpServletResponse());
        mRequest.setParameter("userId", "publishing");

        droplet.setSearchByUserId(true);
        droplet.service(mRequest, mResponse);
        ArrayList<Order> orders = (ArrayList<Order>) mRequest.getObjectParameter("result");

here the "result" param is output param which this droplet sets.and the userId i have hardcoded as "publishing" which i have created.Ignore servletTestUtils class that is created by me which has not much to do with droplet theory here :)

1
hpruszyn On

You should't use droplets in java classes they should be used only inside jsp pages. Documentation of OrderLookup with example hot to use it on jsp page is here.

If you want to get orders or any other data stored in a repository you should use repository API with RQL (Repository Query Language). Example how to get data from repository you can find here and RQL grammar here.