GrandExchangePlugin extends Plugin { @VisibleForTesting void submitTrade(int slot, GrandExchangeOffer offer) { GrandExchangeOfferState state = offer.getState(); if (state != GrandExchangeOfferState.CANCELLED_BUY && state != GrandExchangeOfferState.CANCELLED_SELL && state != GrandExchangeOfferState.BUYING && state != GrandExchangeOfferState.SELLING) { return; } SavedOffer savedOffer = getOffer(slot); if (savedOffer == null && (state == GrandExchangeOfferState.BUYING || state == GrandExchangeOfferState.SELLING) && offer.getQuantitySold() == 0) { GrandExchangeTrade grandExchangeTrade = new GrandExchangeTrade(); grandExchangeTrade.setBuy(state == GrandExchangeOfferState.BUYING); grandExchangeTrade.setItemId(offer.getItemId()); grandExchangeTrade.setTotal(offer.getTotalQuantity()); grandExchangeTrade.setOffer(offer.getPrice()); grandExchangeTrade.setSlot(slot); grandExchangeTrade.setWorldType(getGeWorldType()); grandExchangeTrade.setLogin(loginBurstGeUpdates); log.debug("Submitting new trade: {}", grandExchangeTrade); grandExchangeClient.submit(grandExchangeTrade); return; } if (savedOffer == null || savedOffer.getItemId() != offer.getItemId() || savedOffer.getPrice() != offer.getPrice() || savedOffer.getTotalQuantity() != offer.getTotalQuantity()) { return; } if (savedOffer.getState() == offer.getState() && savedOffer.getQuantitySold() == offer.getQuantitySold()) { return; } if (state == GrandExchangeOfferState.CANCELLED_BUY || state == GrandExchangeOfferState.CANCELLED_SELL) { GrandExchangeTrade grandExchangeTrade = new GrandExchangeTrade(); grandExchangeTrade.setBuy(state == GrandExchangeOfferState.CANCELLED_BUY); grandExchangeTrade.setCancel(true); grandExchangeTrade.setItemId(offer.getItemId()); grandExchangeTrade.setQty(offer.getQuantitySold()); grandExchangeTrade.setTotal(offer.getTotalQuantity()); grandExchangeTrade.setSpent(offer.getSpent()); grandExchangeTrade.setOffer(offer.getPrice()); grandExchangeTrade.setSlot(slot); grandExchangeTrade.setWorldType(getGeWorldType()); grandExchangeTrade.setLogin(loginBurstGeUpdates); log.debug("Submitting cancelled: {}", grandExchangeTrade); grandExchangeClient.submit(grandExchangeTrade); return; } final int qty = offer.getQuantitySold() - savedOffer.getQuantitySold(); final int dspent = offer.getSpent() - savedOffer.getSpent(); if (qty <= 0 || dspent <= 0) { return; } GrandExchangeTrade grandExchangeTrade = new GrandExchangeTrade(); grandExchangeTrade.setBuy(state == GrandExchangeOfferState.BUYING); grandExchangeTrade.setItemId(offer.getItemId()); grandExchangeTrade.setQty(offer.getQuantitySold()); grandExchangeTrade.setDqty(qty); grandExchangeTrade.setTotal(offer.getTotalQuantity()); grandExchangeTrade.setDspent(dspent); grandExchangeTrade.setSpent(offer.getSpent()); grandExchangeTrade.setOffer(offer.getPrice()); grandExchangeTrade.setSlot(slot); grandExchangeTrade.setWorldType(getGeWorldType()); grandExchangeTrade.setLogin(loginBurstGeUpdates); log.debug("Submitting trade: {}", grandExchangeTrade); grandExchangeClient.submit(grandExchangeTrade); }  @Subscribe void onSessionOpen(SessionOpen sessionOpen); @Subscribe void onSessionClose(SessionClose sessionClose); @Subscribe void onConfigChanged(ConfigChanged event); @Subscribe void onGrandExchangeOfferChanged(GrandExchangeOfferChanged offerEvent); @Subscribe void onChatMessage(ChatMessage event); @Subscribe void onGameStateChanged(GameStateChanged gameStateChanged); @Subscribe void onMenuEntryAdded(MenuEntryAdded event); @Subscribe void onFocusChanged(FocusChanged focusChanged); @Subscribe void onWidgetLoaded(WidgetLoaded event); @Subscribe void onScriptPostFired(ScriptPostFired event); @Subscribe void onGrandExchangeSearched(GrandExchangeSearched event); @Subscribe void onScriptCallbackEvent(ScriptCallbackEvent event);  }
@Test public void testSubmitTrade() { SavedOffer savedOffer = new SavedOffer(); savedOffer.setItemId(ItemID.ABYSSAL_WHIP); savedOffer.setQuantitySold(1); savedOffer.setTotalQuantity(10); savedOffer.setPrice(1000); savedOffer.setSpent(25); savedOffer.setState(GrandExchangeOfferState.BUYING); when(configManager.getConfiguration("geoffer.adam", "0")).thenReturn(GSON.toJson(savedOffer)); GrandExchangeOffer grandExchangeOffer = mock(GrandExchangeOffer.class); when(grandExchangeOffer.getQuantitySold()).thenReturn(1 + 2); when(grandExchangeOffer.getItemId()).thenReturn(ItemID.ABYSSAL_WHIP); when(grandExchangeOffer.getTotalQuantity()).thenReturn(10); when(grandExchangeOffer.getPrice()).thenReturn(1000); when(grandExchangeOffer.getSpent()).thenReturn(25 + 10 * 2); when(grandExchangeOffer.getState()).thenReturn(GrandExchangeOfferState.BUYING); grandExchangePlugin.submitTrade(0, grandExchangeOffer); ArgumentCaptor<GrandExchangeTrade> captor = ArgumentCaptor.forClass(GrandExchangeTrade.class); verify(grandExchangeClient).submit(captor.capture()); GrandExchangeTrade trade = captor.getValue(); assertTrue(trade.isBuy()); assertEquals(ItemID.ABYSSAL_WHIP, trade.getItemId()); assertEquals(2, trade.getDqty()); assertEquals(10, trade.getTotal()); assertEquals(45, trade.getSpent()); assertEquals(20, trade.getDspent()); }
@Test public void testDuplicateTrade() { SavedOffer savedOffer = new SavedOffer(); savedOffer.setItemId(ItemID.ABYSSAL_WHIP); savedOffer.setQuantitySold(1); savedOffer.setTotalQuantity(10); savedOffer.setPrice(1000); savedOffer.setSpent(25); savedOffer.setState(GrandExchangeOfferState.BUYING); when(configManager.getConfiguration("geoffer.adam", "0")).thenReturn(GSON.toJson(savedOffer)); GrandExchangeOffer grandExchangeOffer = mock(GrandExchangeOffer.class); when(grandExchangeOffer.getQuantitySold()).thenReturn(1); when(grandExchangeOffer.getItemId()).thenReturn(ItemID.ABYSSAL_WHIP); when(grandExchangeOffer.getTotalQuantity()).thenReturn(10); when(grandExchangeOffer.getPrice()).thenReturn(1000); lenient().when(grandExchangeOffer.getSpent()).thenReturn(25); when(grandExchangeOffer.getState()).thenReturn(GrandExchangeOfferState.BUYING); grandExchangePlugin.submitTrade(0, grandExchangeOffer); verify(grandExchangeClient, never()).submit(any(GrandExchangeTrade.class)); }
@Test public void testCancelTrade() { SavedOffer savedOffer = new SavedOffer(); savedOffer.setItemId(ItemID.ABYSSAL_WHIP); savedOffer.setQuantitySold(1); savedOffer.setTotalQuantity(10); savedOffer.setPrice(1000); savedOffer.setSpent(25); savedOffer.setState(GrandExchangeOfferState.BUYING); when(configManager.getConfiguration("geoffer.adam", "0")).thenReturn(GSON.toJson(savedOffer)); GrandExchangeOffer grandExchangeOffer = mock(GrandExchangeOffer.class); when(grandExchangeOffer.getQuantitySold()).thenReturn(1); when(grandExchangeOffer.getItemId()).thenReturn(ItemID.ABYSSAL_WHIP); when(grandExchangeOffer.getTotalQuantity()).thenReturn(10); when(grandExchangeOffer.getPrice()).thenReturn(1000); when(grandExchangeOffer.getSpent()).thenReturn(25); when(grandExchangeOffer.getState()).thenReturn(GrandExchangeOfferState.CANCELLED_BUY); grandExchangePlugin.submitTrade(0, grandExchangeOffer); ArgumentCaptor<GrandExchangeTrade> captor = ArgumentCaptor.forClass(GrandExchangeTrade.class); verify(grandExchangeClient).submit(captor.capture()); GrandExchangeTrade trade = captor.getValue(); assertTrue(trade.isBuy()); assertTrue(trade.isCancel()); assertEquals(ItemID.ABYSSAL_WHIP, trade.getItemId()); assertEquals(1, trade.getQty()); assertEquals(10, trade.getTotal()); assertEquals(25, trade.getSpent()); }