/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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.
*/
package org.optaplanner.core.impl.heuristic.selector.value.decorator;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.optaplanner.core.impl.heuristic.selector.SelectorTestUtils;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionFilter;
import org.optaplanner.core.impl.heuristic.selector.value.EntityIndependentValueSelector;
import org.optaplanner.core.impl.phase.scope.AbstractPhaseScope;
import org.optaplanner.core.impl.phase.scope.AbstractStepScope;
import org.optaplanner.core.impl.solver.scope.DefaultSolverScope;
import org.optaplanner.core.impl.testdata.domain.TestdataEntity;
import org.optaplanner.core.impl.testdata.domain.TestdataSolution;
import org.optaplanner.core.impl.testdata.domain.TestdataValue;
import static org.mockito.Mockito.*;
import static org.optaplanner.core.impl.testdata.util.PlannerAssert.*;
public class EntityIndependentFilteringValueSelectorTest {
@Test
public void filterEntityIndependent() {
EntityIndependentValueSelector childValueSelector = SelectorTestUtils.mockEntityIndependentValueSelector(
TestdataEntity.class, "value",
new TestdataValue("v1"), new TestdataValue("v2"), new TestdataValue("v3"), new TestdataValue("v4"));
SelectionFilter<TestdataSolution, TestdataValue> filter
= (scoreDirector, value) -> !value.getCode().equals("v3");
List<SelectionFilter> filterList = Arrays.asList(filter);
EntityIndependentValueSelector valueSelector = new EntityIndependentFilteringValueSelector(
childValueSelector, filterList);
DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
valueSelector.solvingStarted(solverScope);
AbstractPhaseScope phaseScopeA = mock(AbstractPhaseScope.class);
when(phaseScopeA.getSolverScope()).thenReturn(solverScope);
valueSelector.phaseStarted(phaseScopeA);
AbstractStepScope stepScopeA1 = mock(AbstractStepScope.class);
when(stepScopeA1.getPhaseScope()).thenReturn(phaseScopeA);
valueSelector.stepStarted(stepScopeA1);
assertAllCodesOfValueSelector(valueSelector, 4L, "v1", "v2", "v4");
valueSelector.stepEnded(stepScopeA1);
AbstractStepScope stepScopeA2 = mock(AbstractStepScope.class);
when(stepScopeA2.getPhaseScope()).thenReturn(phaseScopeA);
valueSelector.stepStarted(stepScopeA2);
assertAllCodesOfValueSelector(valueSelector, 4L, "v1", "v2", "v4");
valueSelector.stepEnded(stepScopeA2);
valueSelector.phaseEnded(phaseScopeA);
AbstractPhaseScope phaseScopeB = mock(AbstractPhaseScope.class);
when(phaseScopeB.getSolverScope()).thenReturn(solverScope);
valueSelector.phaseStarted(phaseScopeB);
AbstractStepScope stepScopeB1 = mock(AbstractStepScope.class);
when(stepScopeB1.getPhaseScope()).thenReturn(phaseScopeB);
valueSelector.stepStarted(stepScopeB1);
assertAllCodesOfValueSelector(valueSelector, 4L, "v1", "v2", "v4");
valueSelector.stepEnded(stepScopeB1);
AbstractStepScope stepScopeB2 = mock(AbstractStepScope.class);
when(stepScopeB2.getPhaseScope()).thenReturn(phaseScopeB);
valueSelector.stepStarted(stepScopeB2);
assertAllCodesOfValueSelector(valueSelector, 4L, "v1", "v2", "v4");
valueSelector.stepEnded(stepScopeB2);
AbstractStepScope stepScopeB3 = mock(AbstractStepScope.class);
when(stepScopeB3.getPhaseScope()).thenReturn(phaseScopeB);
valueSelector.stepStarted(stepScopeB3);
assertAllCodesOfValueSelector(valueSelector, 4L, "v1", "v2", "v4");
valueSelector.stepEnded(stepScopeB3);
valueSelector.phaseEnded(phaseScopeB);
valueSelector.solvingEnded(solverScope);
verifyPhaseLifecycle(childValueSelector, 1, 2, 5);
verify(childValueSelector, times(5)).iterator();
verify(childValueSelector, times(5)).getSize();
}
}