WebPositionResource extends AbstractWebPositionResource { @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public Response putJSON(@FormParam("quantity") String quantityStr, @FormParam("tradesJson") String tradesJson, @FormParam("type") String type, @FormParam(POSITION_XML) String positionXml) { PositionDocument doc = data().getPosition(); if (doc.isLatest() == false) { return Response.status(Status.FORBIDDEN).entity(getHTML()).build(); } type = defaultString(trimToNull(type)); switch (type) { case "xml": updatePosition(trimToEmpty(positionXml)); break; default: quantityStr = replace(trimToNull(quantityStr), ",", ""); tradesJson = trimToNull(tradesJson); Collection<ManageableTrade> trades = null; if (tradesJson != null) { trades = parseTrades(tradesJson); } else { trades = Collections.<ManageableTrade>emptyList(); } BigDecimal quantity = quantityStr != null && NumberUtils.isNumber(quantityStr) ? new BigDecimal(quantityStr) : null; updatePosition(doc, quantity, trades); } return Response.ok().build(); } WebPositionResource(final AbstractWebPositionResource parent); @GET @Produces(MediaType.TEXT_HTML) String getHTML(); @GET @Produces(MediaType.APPLICATION_JSON) String getJSON(); @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_HTML) Response putHTML(@FormParam(POSITION_XML) String positionXml); @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) Response putJSON(@FormParam("quantity") String quantityStr, 
      @FormParam("tradesJson") String tradesJson, 
      @FormParam("type") String type, 
      @FormParam(POSITION_XML) String positionXml); @DELETE @Produces(MediaType.TEXT_HTML) Response deleteHTML(); @DELETE @Produces(MediaType.APPLICATION_JSON) Response deleteJSON(); @Path("versions") WebPositionVersionsResource findVersions(); static URI uri(final WebPositionsData data); static URI uri(final WebPositionsData data, final UniqueId overridePositionId);  }
@Test public void testUpdatePositionWithTrades() throws Exception { final UniqueId uid = addPosition(); final WebPositionResource positionResource = _webPositionsResource.findPosition(uid.toString()); final Response response = positionResource.putJSON(QUANTITY.toString(), getTradesJson(), null, null); assertNotNull(response); assertEquals(200, response.getStatus()); final PositionDocument positionDocument = _positionMaster.get(uid); assertNotNull(positionDocument); final ManageablePosition position = positionDocument.getPosition(); assertEquals(BigDecimal.valueOf(QUANTITY), position.getQuantity()); final List<ManageableTrade> trades = position.getTrades(); assertEquals(3, trades.size()); for (final ManageableTrade trade : trades) { assertEquals(SECURITY_LINK, trade.getSecurityLink()); trade.setUniqueId(null); trade.setSecurityLink(new ManageableSecurityLink(SEC_ID)); trade.setParentPositionId(null); assertTrue(_trades.contains(trade)); } }
@Test public void testUpdatePositionWithoutTrades() throws Exception { final UniqueId uid = addPosition(); final WebPositionResource positionResource = _webPositionsResource.findPosition(uid.toString()); final Response response = positionResource.putJSON(QUANTITY.toString(), null, null, null); assertNotNull(response); assertEquals(200, response.getStatus()); final PositionDocument positionDocument = _positionMaster.get(uid); assertNotNull(positionDocument); final ManageablePosition position = positionDocument.getPosition(); assertEquals(BigDecimal.valueOf(QUANTITY), position.getQuantity()); final List<ManageableTrade> trades = position.getTrades(); assertTrue(trades.isEmpty()); }
@Test public void testUpdatePositionWithEmptyTrades() throws Exception { final UniqueId uid = addPosition(); final WebPositionResource positionResource = _webPositionsResource.findPosition(uid.toString()); final Response response = positionResource.putJSON(QUANTITY.toString(), EMPTY_TRADES, null, null); assertNotNull(response); assertEquals(200, response.getStatus()); final PositionDocument positionDocument = _positionMaster.get(uid); assertNotNull(positionDocument); final ManageablePosition position = positionDocument.getPosition(); assertEquals(BigDecimal.valueOf(QUANTITY), position.getQuantity()); final List<ManageableTrade> trades = position.getTrades(); assertTrue(trades.isEmpty()); }