001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.model.predicate;
015
016import org.gbif.api.model.occurrence.search.OccurrenceSearchParameter;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021
022import org.junit.jupiter.api.Test;
023
024import static org.hamcrest.CoreMatchers.equalTo;
025import static org.hamcrest.CoreMatchers.not;
026import static org.hamcrest.MatcherAssert.assertThat;
027import static org.junit.jupiter.api.Assertions.assertNotNull;
028import static org.junit.jupiter.api.Assertions.assertThrows;
029import static org.mockito.Mockito.mock;
030
031public class CompoundPredicateTest {
032
033  @Test
034  public void testEmptyPredicates() {
035    assertThrows(IllegalArgumentException.class, () -> new ConjunctionPredicate(new ArrayList<>()));
036  }
037
038  @Test
039  public void testEquals() {
040    List<Predicate> mocks = new ArrayList<>();
041    mocks.add(mock(Predicate.class));
042    mocks.add(mock(Predicate.class));
043
044    // Coming from the same list so should be the same
045    Predicate cp1 = new ConjunctionPredicate(mocks);
046    Predicate cp2 = new ConjunctionPredicate(mocks);
047    assertThat(cp1, equalTo(cp2));
048    assertThat(cp1, equalTo(cp1));
049
050    // Now add one to the list, the underlying list for cp1 should not change so cp1 != cp2
051    mocks.add(mock(Predicate.class));
052    cp2 = new ConjunctionPredicate(mocks);
053    assertThat(cp1, not(equalTo(cp2)));
054
055    // Compare a Conjunction with a Disjunction
056    cp1 = new ConjunctionPredicate(mocks);
057    Predicate dp1 = new DisjunctionPredicate(mocks);
058    assertThat(cp1, not(equalTo(dp1)));
059    assertThat(dp1, not(equalTo(cp1)));
060    assertThat(dp1, equalTo(dp1));
061
062    Predicate dp2 = new DisjunctionPredicate(mocks);
063    assertThat(dp1, equalTo(dp2));
064  }
065
066  @Test
067  public void testToString() {
068    Predicate p =
069        new ConjunctionPredicate(
070            Arrays.asList(
071                new EqualsPredicate<>(
072                    OccurrenceSearchParameter.BASIS_OF_RECORD, "FOSSIL_SPECIMEN", false),
073                new NotPredicate(
074                    new DisjunctionPredicate(
075                        Arrays.asList(
076                            new EqualsPredicate<>(OccurrenceSearchParameter.YEAR, "2001", false),
077                            new EqualsPredicate<>(OccurrenceSearchParameter.YEAR, "2000", false),
078                            new EqualsPredicate<>(OccurrenceSearchParameter.YEAR, "1999", false)))),
079                new EqualsPredicate<>(OccurrenceSearchParameter.TAXON_KEY, "212", false)));
080
081    // make this doesn't throw an exception
082    assertNotNull(p.toString());
083  }
084
085  @Test
086  public void testGetPredicates() {
087    List<Predicate> mocks = new ArrayList<>();
088    mocks.add(mock(Predicate.class));
089    mocks.add(mock(Predicate.class));
090    org.gbif.api.model.predicate.ConjunctionPredicate predicate = new ConjunctionPredicate(mocks);
091    assertThat(mocks, equalTo(predicate.getPredicates()));
092  }
093
094  @Test
095  public void testHashcode() {
096    List<Predicate> mocks = new ArrayList<>();
097    mocks.add(mock(Predicate.class));
098    mocks.add(mock(Predicate.class));
099
100    Predicate p1 = new ConjunctionPredicate(mocks);
101    Predicate p2 = new DisjunctionPredicate(mocks);
102
103    assertThat(p1.hashCode(), equalTo(p2.hashCode()));
104
105    mocks.add(mock(Predicate.class));
106    p2 = new DisjunctionPredicate(mocks);
107    assertThat(p1.hashCode(), not(equalTo(p2.hashCode())));
108  }
109
110  @Test
111  public void testNullPredicate() {
112    assertThrows(NullPointerException.class, () -> new ConjunctionPredicate(null));
113  }
114}