/**
 * 
 */
package eu.dzhw.fdz.metadatamanagement.data.variablemanagement.documents.validation;

import static org.junit.Assert.assertEquals;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import eu.dzhw.fdz.metadatamanagement.data.variablemanagement.documents.VariableDocument;
import eu.dzhw.fdz.metadatamanagement.data.variablemanagement.documents.VariableSurvey;
import eu.dzhw.fdz.metadatamanagement.data.variablemanagement.repositories.VariableRepository;
import eu.dzhw.fdz.metadatamanagement.web.AbstractWebTest;

/**
 * @author Daniel Katzberg
 *
 */
public class UniqueVariableAliasTest extends AbstractWebTest{
  
  private static final Logger LOGGER = LoggerFactory.getLogger(UniqueVariableAliasTest.class);
  
  @Autowired
  private VariableRepository repository;

  @Autowired
  private Validator validator;
  
  //TODO
  @Test
  public void testInvalidVariableSecondDocumentSurvey() {

    //Arrange
    VariableDocument variableDocument1 = new VariableDocument();
    variableDocument1.setId("ThisIDisOkay");
    variableDocument1.setName("ThisNameIsOkay.");

    VariableSurvey variableSurvey1 = new VariableSurvey();
    variableSurvey1.setSurveyId("SurveyIdIsOkay.");
    variableSurvey1.setTitle("TitleIsOkay.");
    variableSurvey1.setVariableAlias(variableDocument1.getName());
    
    variableDocument1.setVariableSurvey(variableSurvey1);
    this.repository.save(variableDocument1);
    
    VariableDocument variableDocument = new VariableDocument();
    variableDocument.setId("ThisIDisOkay2");
    variableDocument.setName("ThisNameIsOkay2.");

    VariableSurvey variableSurvey = new VariableSurvey();
    variableSurvey.setSurveyId("SurveyIdIsOkay.");
    variableSurvey.setTitle("TitleIsOkay.");
    //here is the error, double used alias
    variableSurvey.setVariableAlias(variableDocument1.getName());
    
    variableDocument.setVariableSurvey(variableSurvey);
    
    
    // Act
    Set<ConstraintViolation<VariableDocument>> variableViolations =
        this.validator.validate(variableDocument);

    // Assert
    assertEquals(1, variableViolations.size());
    for (ConstraintViolation<VariableDocument> variableVialation : variableViolations) {

      LOGGER.debug("testInalidVariableSecondDocumentSurvey() " + variableVialation.getMessageTemplate() + " -> "
          + variableVialation.getMessage());

      assertEquals(
          "{eu.dzhw.fdz.metadatamanagement.data.variablemanagement.documents.validation.uniquevariablealias.message}",
          variableVialation.getMessageTemplate());
    }
    
    //Delete
    this.repository.delete(variableDocument.getId());
  }
  
  
  @Test
  public void testValidVariableSecondDocumentSurvey() {

    //Arrange
    VariableDocument variableDocument1 = new VariableDocument();
    variableDocument1.setId("ThisIDisOkay");
    variableDocument1.setName("ThisNameIsOkay.");

    VariableSurvey variableSurvey1 = new VariableSurvey();
    variableSurvey1.setSurveyId("SurveyIdIsOkay.");
    variableSurvey1.setTitle("TitleIsOkay.");
    variableSurvey1.setVariableAlias(variableDocument1.getName());
    
    variableDocument1.setVariableSurvey(variableSurvey1);
    this.repository.save(variableDocument1);
    
    VariableDocument variableDocument = new VariableDocument();
    variableDocument.setId("ThisIDisOkay2");
    variableDocument.setName("ThisNameIsOkay2.");

    VariableSurvey variableSurvey = new VariableSurvey();
    variableSurvey.setSurveyId("SurveyIdIsOkay.");
    variableSurvey.setTitle("TitleIsOkay.");
    variableSurvey.setVariableAlias(variableDocument.getName());
    
    variableDocument.setVariableSurvey(variableSurvey);
    
    
    // Act
    Set<ConstraintViolation<VariableDocument>> variableViolations =
        this.validator.validate(variableDocument);

    // Assert
    assertEquals(0, variableViolations.size());
    
    //Delete
    this.repository.delete(variableDocument.getId());
  }
}

@@ -1025,17 +1025,8 @@
 %0A  %0A
-  //TODO%0A
   @T
@@ -3420,32 +3420,33 @@
 variableDocument
+2
  = new VariableD
@@ -3468,32 +3468,33 @@
 variableDocument
+2
 .setId(%22ThisIDis
@@ -3514,32 +3514,33 @@
 variableDocument
+2
 .setName(%22ThisNa
@@ -3579,32 +3579,33 @@
 y variableSurvey
+2
  = new VariableS
@@ -3623,32 +3623,33 @@
   variableSurvey
+2
 .setSurveyId(%22Su
@@ -3675,32 +3675,33 @@
   variableSurvey
+2
 .setTitle(%22Title
@@ -3721,32 +3721,33 @@
   variableSurvey
+2
 .setVariableAlia
@@ -3760,24 +3760,25 @@
 ableDocument
+2
 .getName());
@@ -3795,32 +3795,33 @@
 variableDocument
+2
 .setVariableSurv
@@ -3829,32 +3829,33 @@
 y(variableSurvey
+2
 );%0A    %0A    %0A   
@@ -3970,32 +3970,33 @@
 variableDocument
+2
 );%0A%0A    // Asser
@@ -4098,32 +4098,33 @@
 variableDocument
+1
 .getId());%0A  %7D%0A%7D

fix a unit test and the build