RoboVMWebService { public void getProducts(Action<List<Product>> completion) { Objects.requireNonNull(completion); if (products != null) { ActionWrapper.WRAPPER.invoke(completion, products); } else { api.products().enqueue(new Callback<ProductsResponse>() { @Override public void onResponse(Response<ProductsResponse> response, Retrofit retrofit) { List<Product> products = null; if (response.isSuccess()) { ProductsResponse body = response.body(); if (body.isSuccess()) { products = body.getProducts(); } } RoboVMWebService.this.products = products; if (products == null) { products = new ArrayList<>(); } ActionWrapper.WRAPPER.invoke(completion, products); } @Override public void onFailure(Throwable t) { t.printStackTrace(); ActionWrapper.WRAPPER.invoke(completion, new ArrayList<>()); } }); } } private  RoboVMWebService(); static RoboVMWebService getInstance(); RoboVMWebService setup(); RoboVMWebService setup(boolean test); void authenticate(String username, String password, Action<Boolean> completion); void getProducts(Action<List<Product>> completion); void placeOrder(User user, Action<APIResponse> completion); void preloadProductImages(); boolean isAuthenticated(); User getCurrentUser(); Basket getBasket(); RoboVMAPI getApi();  }
@Test public void apiProductsShouldReturnValidProducts() throws IOException { Call<ProductsResponse> call = api.products(); ProductsResponse response = call.execute().body(); assertTrue(response.isSuccess()); List<Product> products = response.getProducts(); assertNotNull(products); assertEquals(products.size(), 2); Product menJavaTShirt = products.get(0); assertEquals(menJavaTShirt.getId(), "MenJavaTShirt"); assertEquals(menJavaTShirt.getName(), "Men's Java T-shirt"); assertEquals(menJavaTShirt.getType(), ProductType.TShirt); List<ProductColor> colors = menJavaTShirt.getColors(); assertEquals(colors.size(), 2); assertEquals(colors.get(1).getName(), "Navy"); List<ProductSize> sizes = menJavaTShirt.getSizes(); assertEquals(sizes.size(), 5); assertEquals(sizes.get(4).getId(), "xxl"); }
@Test public void apiOrderShouldReturnSuccessOrNoMoreShirts() throws IOException { Call<ProductsResponse> call = api.products(); ProductsResponse response = call.execute().body(); List<Product> products = response.getProducts(); assertNotNull(products); Call<AuthResponse> authCall = api.auth(new AuthRequest("dominik@robovm.com", "qwerty")); AuthResponse authResponse = authCall.execute().body(); String authToken = authResponse.getAuthToken(); User user = new User(); user.setFirstName("Clark"); user.setLastName("Kent"); user.setAddress1("344 Clinton St."); user.setAddress2("Apt. #3B"); user.setCity("Metropolis"); user.setZipCode("62960"); user.setPhone("+12015612823"); user.setCountry("US"); Basket basket = new Basket(); Product product = products.get(0); basket.add(new Order(product, product.getSizes().get(0), product.getColors().get(0))); Call<APIResponse> orderCall = api.order(new OrderRequest(new AuthToken(authToken), user, basket)); APIResponse orderResponse = orderCall.execute().body(); assertTrue(orderResponse.isSuccess() || (orderResponse.getErrors().size() == 1 && orderResponse.getErrors().get(0).getMessage() .equals("No more t-shirts for you!"))); }