/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
package org.apache.sling.validation.impl.model;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.sling.validation.impl.util.ChildResourceNameRegexMatcher;
import org.apache.sling.validation.impl.util.ResourcePropertyNameRegexMatcher;
import org.apache.sling.validation.model.ChildResource;
import org.apache.sling.validation.model.ResourceProperty;
import org.apache.sling.validation.model.ValidationModel;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class MergedValidationModelTest {

    private ValidationModelBuilder modelBuilder;
    private ResourcePropertyBuilder propertyBuilder;
    private ChildResourceBuilder childResourceBuilder;

    @Before
    public void setup() {
        modelBuilder = new ValidationModelBuilder();
        propertyBuilder = new ResourcePropertyBuilder();
        // each model needs at least one property or childresource
        modelBuilder.resourceProperty(propertyBuilder.build("nameToOverwrite"));
        childResourceBuilder = new ChildResourceBuilder();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testMoreSpecificApplicationPathInModelToMerge() {
        modelBuilder.addApplicablePath("/base/path").addApplicablePath("/base/path2");
        ValidationModel baseValidationModel = modelBuilder.build("base", "some source");
        modelBuilder.setApplicablePath("/base/path3");
        new MergedValidationModel(baseValidationModel, modelBuilder.build("superType", "some source"));
    }

    @Test
    public void testLessSpecificApplicationPathInModelToMerge() {
        modelBuilder.addApplicablePath("/base/path").addApplicablePath("/base/path2");
        ValidationModel baseValidationModel = modelBuilder.build("base", "some source");
        modelBuilder.setApplicablePath("/base");
        ValidationModel mergedModel = new MergedValidationModel(baseValidationModel, modelBuilder.build("superType", "some source"));
        Assert.assertThat(mergedModel.getApplicablePaths(),
                Matchers.contains("/base/path", "/base/path2"));
    }

    @Test
    public void testOverwritingChildrenAndResourceProperties() {
        modelBuilder.resourceProperty(propertyBuilder.nameRegex("overwrittenNameToOverwrite").build("nameToOverwrite"));
        modelBuilder.childResource(childResourceBuilder.nameRegex("overwrittenNameToOverwrite")
                .build("nameToOverwrite"));
        ValidationModel baseValidationModel = modelBuilder.build("base", "some source");
        modelBuilder = new ValidationModelBuilder();
        modelBuilder.resourceProperty(propertyBuilder.nameRegex("originalNameToOverwrite").build("nameToOverwrite"));
        modelBuilder.childResource(childResourceBuilder.nameRegex("originalNameToOverwrite").build("nameToOverwrite"));
        modelBuilder.resourceProperty(propertyBuilder.nameRegex("originalNameNotOverwritten").build(
                "nameNotOverwritten"));
        modelBuilder.childResource(childResourceBuilder.nameRegex("originalNameNotOverwritten").build(
                "nameNotOverwritten"));
        ValidationModel mergedModel = new MergedValidationModel(baseValidationModel, modelBuilder.build("superType", "some source"));
        Collection<Matcher<ResourceProperty>> resourcePropertyMatchers = new ArrayList<>();
        resourcePropertyMatchers.add(new ResourcePropertyNameRegexMatcher("overwrittenNameToOverwrite"));
        resourcePropertyMatchers.add(new ResourcePropertyNameRegexMatcher("originalNameNotOverwritten"));
        Assert.assertThat(mergedModel.getResourceProperties(), Matchers.containsInAnyOrder(resourcePropertyMatchers));
        Collection<Matcher<ChildResource>> childMatchers = new ArrayList<>();
        childMatchers.add(new ChildResourceNameRegexMatcher("overwrittenNameToOverwrite"));
        childMatchers.add(new ChildResourceNameRegexMatcher("originalNameNotOverwritten"));
        Assert.assertThat(mergedModel.getChildren(), Matchers.containsInAnyOrder(childMatchers));
    }

    @Test
    public void testValidatedResourceTypes() {
        ValidationModel mergedModel = new MergedValidationModel(modelBuilder.build("base", "some source"),
                modelBuilder.build("superType", "some source"));
        Assert.assertThat(mergedModel.getValidatedResourceType(), Matchers.equalTo("base"));
    }

}

@@ -874,41 +874,9 @@
 rray
-List;%0Aimport java.util.Collection
+s
 ;%0A%0Ai
@@ -1074,167 +1074,23 @@
 del.
-ChildResource;%0Aimport org.apache.sling.validation.model.ResourceProperty;%0Aimport org.apache.sling.validation.model.ValidationModel;%0Aimport org.hamcrest.Matcher
+ValidationModel
 ;%0Aim
@@ -3927,47 +3927,42 @@
     
-Collection%3CMatcher%3CResourceProperty%3E%3E r
+Assert.assertThat(mergedModel.getR
 esou
@@ -3971,25 +3971,31 @@
 ePropert
-y
+ies(), 
 Matchers
  = new A
@@ -3990,67 +3990,59 @@
 hers
- = new ArrayList%3C%3E();%0A        resourcePropertyMatchers.add(
+.containsInAnyOrder(Arrays.asList(%0A                
 new 
@@ -4095,34 +4095,34 @@
 ameToOverwrite%22)
-);
+, 
 %0A        resourc
@@ -4118,37 +4118,16 @@
     
-resourcePropertyMatchers.add(
+        
 new 
@@ -4181,32 +4181,34 @@
 otOverwritten%22))
+))
 ;%0A        Assert
@@ -4234,34 +4234,24 @@
 odel.get
-ResourceProperties
+Children
 (), Matc
@@ -4278,140 +4278,39 @@
 der(
-resourcePropertyMatchers));%0A        Collection%3CMatcher%3CChildResource%3E%3E childMatchers = new ArrayList%3C%3E();%0A        childMatchers.add(
+Arrays.asList(%0A                
 new 
@@ -4360,34 +4360,34 @@
 ameToOverwrite%22)
-);
+, 
 %0A        childMa
@@ -4383,26 +4383,16 @@
     
-childMatchers.add(
+        
 new 
@@ -4455,104 +4455,8 @@
 n%22))
-;%0A        Assert.assertThat(mergedModel.getChildren(), Matchers.containsInAnyOrder(childMatchers
 ));%0A

fix tests