/** Tests populateAvailability. */
  @Test
  public void testPopulateAvailability() {
    final boolean storeVisible = true;
    CategoryAvailabilityDTO categoryAvailabilityDTO = new CategoryAvailabilityDTO();
    categoryAvailabilityDTO.setStoreVisible(storeVisible);
    categoryAvailabilityDTO.setStartDate(START_DATE);
    categoryAvailabilityDTO.setEndDate(END_DATE);

    final Category mockCategory = context.mock(Category.class);
    context.checking(
        new Expectations() {
          {
            oneOf(mockCategory).setHidden(!storeVisible);
            oneOf(mockCategory).setStartDate(START_DATE);
            oneOf(mockCategory).setEndDate(END_DATE);
          }
        });

    categoryAdapter.populateAvailability(mockCategory, categoryAvailabilityDTO);
    categoryAdapter.populateAvailability(mockCategory, null); // should work
  }
  /** 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);
  }