/** Tests populateDumainNameValues. */
  @Test(expected = PopulationRuntimeException.class)
  public void testPopulateDomainNameValues() {
    final Category mockCategory = context.mock(Category.class);
    final LocaleDependantFields mockDependantFields = context.mock(LocaleDependantFields.class);

    Map<Locale, LocaleDependantFields> localeDependantFieldsMap =
        new HashMap<Locale, LocaleDependantFields>();

    final CategoryDTO categoryDto = new CategoryDTO();
    categoryDto.setNameValues(Arrays.asList(DISPLAY_VALUE_EN_LOCALE, DISPLAY_VALUE_ZUZU_LOCALE));

    context.checking(
        new Expectations() {
          {
            oneOf(mockCategory).getLocaleDependantFieldsWithoutFallBack(LOCALE_EN);
            will(returnValue(mockDependantFields));

            oneOf(mockDependantFields).setDisplayName(DISPLAY_VALUE_EN_LOCALE.getValue());
          }
        });

    localeDependantFieldsMap.put(LOCALE_EN, mockDependantFields);

    // oneOf(mockCategory).setLocaleDependantFieldsMap(localeDependantFieldsMap);

    categoryAdapter.populateDomainNameValues(mockCategory, categoryDto);
  }
  /** Tests populateParentCategory. */
  @Test
  public void testPopulateParentCategory() {
    final Category mockCategory = context.mock(Category.class);

    final CategoryDTO categoryDto = new CategoryDTO();

    categoryDto.setParentCategoryCode(null);
    categoryAdapter.populateParentCategory(mockCategory, categoryDto);

    categoryDto.setParentCategoryCode("");
    categoryAdapter.populateParentCategory(mockCategory, categoryDto);

    context.checking(
        new Expectations() {
          {
            oneOf(mockCachingService).findCategoryByCode(PARENT_CATEGORY_CODE, CATALOG_CODE);
            will(returnValue(null));

            oneOf(mockCategory).setParent(null);
          }
        });

    categoryDto.setParentCategoryCode(PARENT_CATEGORY_CODE);
    categoryDto.setCatalogCode(CATALOG_CODE);
    categoryAdapter.populateParentCategory(mockCategory, categoryDto);
  }
  /** Tests population of category DTO with names when category is held in virtual catalog. */
  @Test
  public void testPopulateDtoNameValuesWhenCatalogIsVirtual() {
    final CategoryDTO categoryDto = new CategoryDTO();
    final Category mockCategory = context.mock(Category.class);
    final LocaleDependantFields mockDependantFields = context.mock(LocaleDependantFields.class);

    final Catalog mockCatalog = context.mock(Catalog.class);
    context.checking(
        new Expectations() {
          {
            oneOf(mockCategory).getCatalog();
            will(returnValue(mockCatalog));
            oneOf(mockCatalog).getSupportedLocales();
            will(returnValue(Arrays.asList(LOCALE_EN)));

            oneOf(mockCategory).getLocaleDependantFieldsWithoutFallBack(LOCALE_EN);
            will(returnValue(mockDependantFields));

            oneOf(mockDependantFields).getDisplayName();
            will(returnValue(DISPLAY_VALUE_EN_LOCALE.getValue()));
          }
        });

    categoryAdapter.populateDtoNameValues(mockCategory, categoryDto);

    final List<DisplayValue> nameValues = categoryDto.getNameValues();
    assertEquals(DISPLAY_VALUE_EN_LOCALE.getLanguage(), nameValues.get(0).getLanguage());
    assertEquals(DISPLAY_VALUE_EN_LOCALE.getValue(), nameValues.get(0).getValue());
    assertEquals(1, nameValues.size());
  }
  /** Tests populateDomain. */
  @Test(expected = PopulationRollbackException.class)
  public void testPopulateDomainThrowsOnWrongCode() {
    final Category mockCategory = context.mock(Category.class);
    final CategoryDTO categoryDto = new CategoryDTO();

    categoryDto.setCategoryCode("");
    categoryAdapter.populateDomain(categoryDto, mockCategory);
  }
  /** Tests populateDTO. */
  @Test
  public void testPopulateDTO() {
    final boolean storeVisible = false;
    final int categoryOrder = 1;
    final String categoryCode = "categoryCode";
    final String categoryTypeCode = "categoryTypeCode";

    final CategoryDTO categoryDto = new CategoryDTO();

    final Catalog mockCatalog = context.mock(Catalog.class);
    final Category mockCategory = context.mock(Category.class);
    final CategoryType mockCategoryType = context.mock(CategoryType.class);
    final Category mockParentCategory = context.mock(Category.class, "parent category");

    context.checking(
        new Expectations() {
          {
            oneOf(mockCategory).getCatalog();
            will(returnValue(mockCatalog));
            oneOf(mockCategory).getParent();
            will(returnValue(mockParentCategory));
            oneOf(mockCategory).getCode();
            will(returnValue(categoryCode));
            oneOf(mockCategory).getCategoryType();
            will(returnValue(mockCategoryType));
            oneOf(mockCategory).getOrdering();
            will(returnValue(categoryOrder));
            oneOf(mockCategory).isHidden();
            will(returnValue(!storeVisible));
            oneOf(mockCategory).getStartDate();
            will(returnValue(START_DATE));
            oneOf(mockCategory).getEndDate();
            will(returnValue(END_DATE));

            oneOf(mockCatalog).getCode();
            will(returnValue(CATALOG_CODE));
            oneOf(mockCategoryType).getName();
            will(returnValue(categoryTypeCode));
            oneOf(mockParentCategory).getCode();
            will(returnValue(PARENT_CATEGORY_CODE));
          }
        });

    categoryAdapter =
        new CategoryAdapter() {
          @Override
          void populateDtoNameValues(final Category category, final CategoryDTO categoryDTO) {
            assertNotNull(category);
            categoryDTO.setNameValues(Arrays.asList(DISPLAY_VALUE_EN_LOCALE));
          }

          @Override
          void populateNestedDto(final Category category, final CategoryDTO categoryDto) {
            assertNotNull(category);
            assertNotNull(categoryDto);
            categoryDto.setAttributeGroupDTO(new AttributeGroupDTO());
            categoryDto.setSeoDto(new SeoDTO());
          }
        };
    categoryAdapter.setElasticPath(mockElasticPath);
    categoryAdapter.setCachingService(mockCachingService);
    categoryAdapter.populateDTO(mockCategory, categoryDto);

    assertEquals(categoryCode, categoryDto.getCategoryCode());
    assertEquals(categoryOrder, categoryDto.getOrder());
    assertEquals(CATALOG_CODE, categoryDto.getCatalogCode());
    assertEquals(PARENT_CATEGORY_CODE, categoryDto.getParentCategoryCode());
    assertEquals(categoryTypeCode, categoryDto.getCategoryType());

    final CategoryAvailabilityDTO categoryAvailabilityDTO =
        categoryDto.getCategoryAvailabilityDTO();
    assertEquals(storeVisible, categoryAvailabilityDTO.isStoreVisible());
    assertEquals(START_DATE, categoryAvailabilityDTO.getStartDate());
    assertEquals(END_DATE, categoryAvailabilityDTO.getEndDate());

    assertNotNull(categoryDto.getAttributeGroupDTO());
    assertNotNull(categoryDto.getSeoDto());
  }
  /** Tests populateDomain. */
  @Test
  public void testPopulateDomain() {
    final boolean storeVisible = false;
    final int categoryOrder = 1;
    final String categoryCode = "categoryCode";
    final String categoryTypeCode = "categoryTypeCode";
    final Map<Locale, LocaleDependantFields> localeDependantFieldsMap =
        new HashMap<Locale, LocaleDependantFields>();
    final Catalog catalog = context.mock(Catalog.class);
    final CategoryType categoryType = context.mock(CategoryType.class);

    final Category mockCategory = context.mock(Category.class);
    final CategoryDTO categoryDto = new CategoryDTO();

    CategoryAvailabilityDTO categoryAvailabilityDTO = new CategoryAvailabilityDTO();
    categoryAvailabilityDTO.setStoreVisible(storeVisible);
    categoryAvailabilityDTO.setStartDate(START_DATE);
    categoryAvailabilityDTO.setEndDate(END_DATE);

    categoryDto.setCategoryCode(categoryCode);
    categoryDto.setOrder(categoryOrder);
    categoryDto.setCatalogCode(CATALOG_CODE);
    categoryDto.setParentCategoryCode(PARENT_CATEGORY_CODE);
    categoryDto.setCategoryType(categoryTypeCode);
    categoryDto.setCategoryAvailabilityDTO(categoryAvailabilityDTO);
    categoryDto.setAttributeGroupDTO(new AttributeGroupDTO());
    categoryDto.setSeoDto(new SeoDTO());

    context.checking(
        new Expectations() {
          {
            oneOf(mockCategory).setCode(categoryCode);
            oneOf(mockCategory).setOrdering(categoryOrder);
            oneOf(mockCategory).setCatalog(catalog);
            oneOf(mockCategory).setParent(null);
            oneOf(mockCategory).setCategoryType(categoryType);
            oneOf(mockCategory).setLocaleDependantFieldsMap(localeDependantFieldsMap);
            oneOf(mockCategory).setHidden(!storeVisible);
            oneOf(mockCategory).setStartDate(START_DATE);
            oneOf(mockCategory).setEndDate(END_DATE);
          }
        });

    categoryAdapter =
        new CategoryAdapter() {
          @Override
          void populateMasterCatalog(final Category category, final String catalogCode) {
            assertEquals(CATALOG_CODE, catalogCode);
            category.setCatalog(catalog);
          }

          @Override
          void populateParentCategory(final Category category, final CategoryDTO categoryDto) {
            assertEquals(PARENT_CATEGORY_CODE, categoryDto.getParentCategoryCode());
            category.setParent(null); // Could not find parent category with code
          }

          @Override
          void populateDomainNameValues(final Category category, final CategoryDTO categoryDto) {
            assertNotNull(categoryDto.getNameValues());
            category.setLocaleDependantFieldsMap(localeDependantFieldsMap);
          }

          @Override
          void populateCategoryType(final Category category, final String categoryTypeName) {
            assertEquals(categoryTypeCode, categoryTypeName);
            category.setCategoryType(categoryType);
          }

          @Override
          void populateAvailability(
              final Category category, final CategoryAvailabilityDTO categoryAvailabilityDTO) {
            category.setHidden(!categoryAvailabilityDTO.isStoreVisible());
            category.setStartDate(categoryAvailabilityDTO.getStartDate());
            category.setEndDate(categoryAvailabilityDTO.getEndDate());
          }

          @Override
          void populateNestedDomainObjects(final Category category, final CategoryDTO categoryDTO) {
            assertNotNull(category);
            assertNotNull(categoryDto);
          }
        };
    categoryAdapter.setElasticPath(mockElasticPath);
    categoryAdapter.setCachingService(mockCachingService);
    categoryAdapter.populateDomain(categoryDto, mockCategory);
  }