/*
* Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.query.impl.predicates;
import com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.VisitablePredicate;
import com.hazelcast.query.impl.Indexes;
import com.hazelcast.query.impl.QueryEntry;
import com.hazelcast.query.impl.getters.Extractors;
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import java.util.Map;
import java.util.UUID;
import static com.hazelcast.instance.TestUtil.toData;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
/**
* Convenient utility methods to create mock predicates
*/
public class PredicateTestUtils {
/**
* Create a negatable mock predicate. The created mock predicate returns passed the predicate
* passed as negation argument
*
* @param negation predicate to be return by created mock predicate on negate()
* @return negatable predicate.
*/
static Predicate createMockNegatablePredicate(Predicate negation) {
NegatablePredicate negatablePredicate = mock(NegatablePredicate.class, withSettings().extraInterfaces(Predicate.class));
when(negatablePredicate.negate()).thenReturn(negation);
return (Predicate) negatablePredicate;
}
static Predicate createMockVisitablePredicate() {
VisitablePredicate visitablePredicate = mock(VisitablePredicate.class, withSettings().extraInterfaces(Predicate.class));
when(visitablePredicate.accept((Visitor) anyObject(), (Indexes) anyObject())).thenReturn((Predicate) visitablePredicate);
return (Predicate) visitablePredicate;
}
static Predicate createMockVisitablePredicate(Predicate transformed) {
VisitablePredicate visitablePredicate = mock(VisitablePredicate.class, withSettings().extraInterfaces(Predicate.class));
when(visitablePredicate.accept((Visitor) anyObject(), (Indexes) anyObject())).thenReturn(transformed);
return (Predicate) visitablePredicate;
}
static Visitor createPassthroughVisitor() {
Visitor visitor = mock(Visitor.class);
when(visitor.visit((AndPredicate) anyObject(), (Indexes) anyObject())).thenAnswer(new ReturnsArgumentAt(0));
when(visitor.visit((OrPredicate) anyObject(), (Indexes) anyObject())).thenAnswer(new ReturnsArgumentAt(0));
when(visitor.visit((NotPredicate) anyObject(), (Indexes) anyObject())).thenAnswer(new ReturnsArgumentAt(0));
return visitor;
}
static Visitor createDelegatingVisitor(Predicate delegate) {
Visitor visitor = mock(Visitor.class);
when(visitor.visit((AndPredicate) anyObject(), (Indexes) anyObject())).thenReturn(delegate);
when(visitor.visit((OrPredicate) anyObject(), (Indexes) anyObject())).thenReturn(delegate);
when(visitor.visit((NotPredicate) anyObject(), (Indexes) anyObject())).thenReturn(delegate);
return visitor;
}
public static String getAttributeName(AbstractPredicate predicate) {
return predicate.attributeName;
}
public static String setAttributeName(AbstractPredicate predicate, String attributeName) {
return predicate.attributeName = attributeName;
}
public static Map.Entry entry(Object value) {
return new QueryEntry(new DefaultSerializationServiceBuilder().build(), toData(UUID.randomUUID().toString()),
value, Extractors.empty());
}
public static Map.Entry entry(Object key, Object value) {
return new QueryEntry(new DefaultSerializationServiceBuilder().build(), toData(key), value, Extractors.empty());
}
}