package productcatalog.services;

import common.categories.CategoryUtils;
import common.products.ProductUtils;
import io.sphere.sdk.categories.Category;
import io.sphere.sdk.categories.CategoryTree;
import io.sphere.sdk.products.ProductProjection;
import org.junit.Test;
import productcatalog.exceptions.ProductNotFoundException;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class CategoryServiceTest {

    private final CategoryTree categories = CategoryTree.of(CategoryUtils.getQueryResult("categoryQueryResult.json").getResults());
    private final List<ProductProjection> products = ProductUtils.getQueryResult("productProjectionQueryResult.json").getResults();
    private final CategoryService categrieService = new CategoryService(categories);

    @Test
    public void getSiblingCategories() throws ProductNotFoundException {
        final ProductProjection suttonBag = products.stream()
                .filter(projection -> projection.getId().equals("254f1c0e-67bc-4aa6-992d-9a0fea1846b5"))
                .findFirst().get();

        final Category clutches = categories.findById("a9c9ebd8-e6ff-41a6-be8e-baa07888c9bd").get();
        final Category satchels = categories.findById("30d79426-a17a-4e63-867e-ec31a1a33416").get();
        final Category shoppers = categories.findById("bd83e288-77de-4c3a-a26c-8384af715bbb").get();
        final Category handBags = categories.findById("9a584ee8-a45a-44e8-b9ec-e11439084687").get();
        final Category wallets = categories.findById("d2f9a2da-db3e-4ee8-8192-134ebbc7fe4a").get();
        final Category backpacks = categories.findById("46249239-8f0f-48a9-b0a0-d29b37fc617f").get();
        final Category shoulderBags = categories.findById("8e052705-7810-4528-ba77-00094b87a69a").get();

        final List<Category> result = categrieService.getSiblingCategories(suttonBag);

        assertThat(result).containsExactly(clutches, satchels, shoppers, handBags, wallets, backpacks, shoulderBags);
    }

    @Test
    public void getBreadCrumbCategories() {
        final ProductProjection suttonBag = products.stream()
                .filter(projection -> projection.getId().equals("254f1c0e-67bc-4aa6-992d-9a0fea1846b5"))
                .findFirst().get();

        final Category woman = categories.findById("33339d11-0e7b-406b-899b-60f4c34c2948").get();
        final Category bags = categories.findById("32952779-d916-4f2b-b1d5-9efd7f7b9f58").get();
        final Category handBags = categories.findById("9a584ee8-a45a-44e8-b9ec-e11439084687").get();

        final List<Category> result = categrieService.getBreadCrumbCategories(suttonBag);

        assertThat(result).containsExactly(woman, bags, handBags);
    }
}

@@ -71,45 +71,8 @@
 ls;%0A
-import common.products.ProductUtils;%0A
 impo
@@ -231,92 +231,151 @@
 st;%0A
+%0A
 import 
-productcatalog.exceptions.ProductNotFoundException;%0A%0Aimport java.util.Lis
+java.util.List;%0A%0Aimport static common.products.ProductUtils.getProductById;%0Aimport static common.products.ProductUtils.getQueryResul
 t;%0A
-%0A
 impo
@@ -650,29 +650,16 @@
 ducts = 
-ProductUtils.
 getQuery
@@ -862,23 +862,8 @@
 ows 
-ProductNotFound
 Exce
@@ -918,90 +918,33 @@
 g = 
-p
+getP
 roduct
-s.stream()%0A                .filter(projection -%3E projection.getId().equals(
+ById(products, 
 %22254
@@ -982,44 +982,8 @@
 b5%22)
-)%0A                .findFirst().get()
 ;%0A%0A 
@@ -2008,90 +2008,33 @@
 g = 
-p
+getP
 roduct
-s.stream()%0A                .filter(projection -%3E projection.getId().equals(
+ById(products, 
 %22254
@@ -2072,44 +2072,8 @@
 b5%22)
-)%0A                .findFirst().get()
 ;%0A%0A 

Use util methods to get Product