AddNonBasicProductsPresenter extends Presenter { public Observable<List<NonBasicProductsViewModel>> getAllNonBasicProductsViewModels(final List<String> selectedProducts) { return Observable.create((Observable.OnSubscribe<List<NonBasicProductsViewModel>>) subscriber -> { try { List<Product> products = productRepository.listNonBasicProducts(); for (Product product : products) { if (selectedProducts.contains(product.getCode())) { continue; } NonBasicProductsViewModel currentModel = new NonBasicProductsViewModel(product); models.add(currentModel); } subscriber.onNext(models); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); } }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()); } AddNonBasicProductsPresenter(); @Override void attachView(BaseView v); Observable<List<NonBasicProductsViewModel>> getAllNonBasicProductsViewModels(final List<String> selectedProducts);  }
@Test public void shouldGenerateNonBasicProductsViewModelsWhenThereAreNotNonBasicProductsSelected() { List<Product> expectedNonBasicProducts = newArrayList(product1, product2); TestSubscriber<List<NonBasicProductsViewModel>> subscriber = new TestSubscriber<>(); when(productRepository.listNonBasicProducts()).thenReturn(expectedNonBasicProducts); Observable<List<NonBasicProductsViewModel>> observable = presenter.getAllNonBasicProductsViewModels(new ArrayList<String>()); observable.subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); List<NonBasicProductsViewModel> actualProductsViewModels = subscriber.getOnNextEvents().get(0); assertThat(expectedNonBasicProducts.size(), is(actualProductsViewModels.size())); for (int index = 0; index < expectedNonBasicProducts.size(); index++) { assertThat(expectedNonBasicProducts.get(index).getCode(), is(actualProductsViewModels.get(index).getProductCode())); assertThat(expectedNonBasicProducts.get(index).getType(), is(actualProductsViewModels.get(index).getProductType())); assertThat(expectedNonBasicProducts.get(index), is(actualProductsViewModels.get(index).getProduct())); assertFalse(actualProductsViewModels.get(index).isChecked()); assertThat(actualProductsViewModels.get(index).getStyledProductName().toString(), is(getStyledProductName(expectedNonBasicProducts.get(index)))); } }
@Test public void shouldNotShowTheAddedNonBasicProductInTheList() { List<Product> allNonBasicProductFromDB = newArrayList(product1, product2, product3, product4); TestSubscriber<List<NonBasicProductsViewModel>> subscriber = new TestSubscriber<>(); when(productRepository.listNonBasicProducts()).thenReturn(allNonBasicProductFromDB); Observable<List<NonBasicProductsViewModel>> observable = presenter.getAllNonBasicProductsViewModels(newArrayList(addedNonBasicProductCode)); observable.subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); List<NonBasicProductsViewModel> actualProductsViewModel = subscriber.getOnNextEvents().get(0); assertThat(allNonBasicProductFromDB.size(), is(actualProductsViewModel.size() + 1)); for (NonBasicProductsViewModel model : actualProductsViewModel) { assertNotEquals(model.getProductCode(), addedNonBasicProductCode); } }