/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2017 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.qa.galenium.verification;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebElement;

import io.wcm.qa.galenium.selectors.Selector;

/**
 * Make sure a certain CSS class is set on an element.
 */
public class CssClassVerification extends AttributeVerification {

  private static final String ATTRIBUTE_NAME_CLASS = "class";
  private String expectedValue;

  /**
   * Constructor for {@link Selector}.
   * @param selector to identify element
   * @param cssClass to verify against
   */
  public CssClassVerification(Selector selector, String cssClass) {
    super(selector, ATTRIBUTE_NAME_CLASS, cssClass);
    setExpectedValue(cssClass);
  }

  /**
   * Constructor for resolved {@link WebElement}.
   * @param elementName name of element for reporting
   * @param element resolved element to test
   * @param cssClass to verify
   */
  public CssClassVerification(String elementName, WebElement element, String cssClass) {
    super(elementName, element, ATTRIBUTE_NAME_CLASS, cssClass);
  }

  @Override
  protected Boolean doVerification() {
    String cssClasses = getActualValue();
    if (StringUtils.isBlank(cssClasses)) {
      return false;
    }
    String[] splitCssClasses = cssClasses.split(" ");
    return ArrayUtils.contains(splitCssClasses, getExpectedValue());
  }

  @Override
  protected String getExpectedValue() {
    return expectedValue;
  }

  @Override
  protected String getFailureMessage() {
    return getElementName() + " should have CSS class '" + getExpectedValue() + "', but only found '" + getActualValue() + "'";
  }

  @Override
  protected String getSuccessMessage() {
    return getElementName() + " has CSS class '" + getExpectedValue() + "'";
  }

  @Override
  protected void setExpectedValue(String expectedValue) {
    this.expectedValue = expectedValue;
  }

}

@@ -1064,16 +1064,207 @@
 Value;%0A%0A
+  /**%0A   * Constructor for %7B@link Selector%7D.%0A   * @param selector to identify element%0A   */%0A  public CssClassVerification(Selector selector) %7B%0A    super(selector, ATTRIBUTE_NAME_CLASS);%0A  %7D%0A%0A
   /**%0A  

add constructor to use CSS verification with sampling