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.List; 020 021import org.junit.jupiter.api.Test; 022 023import static org.hamcrest.CoreMatchers.both; 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.assertThrows; 028import static org.mockito.Mockito.mock; 029 030public class InPredicateTest { 031 032 private static final OccurrenceSearchParameter TEST_KEY = 033 OccurrenceSearchParameter.CATALOG_NUMBER; 034 035 @Test 036 public void testBasics() { 037 List<String> list = new ArrayList<>(); 038 list.add("foo"); 039 list.add("bar"); 040 041 InPredicate<OccurrenceSearchParameter> p = new InPredicate<>(TEST_KEY, list, false); 042 043 assertThat(p.getKey(), equalTo(TEST_KEY)); 044 assertThat(p.getValues(), equalTo(list)); 045 046 list.add("oink"); 047 assertThat(p.getValues(), not(equalTo(list))); 048 } 049 050 @Test 051 public void testEmptyConstructor() { 052 assertThrows( 053 NullPointerException.class, () -> new InPredicate<>(null, new ArrayList<>(), false)); 054 } 055 056 @Test 057 public void testEmptyConstructor2() { 058 assertThrows( 059 IllegalArgumentException.class, 060 () -> 061 new InPredicate<>(OccurrenceSearchParameter.CATALOG_NUMBER, new ArrayList<>(), false)); 062 } 063 064 @Test 065 public void testEquals() { 066 List<String> list1 = new ArrayList<>(); 067 list1.add("foo"); 068 list1.add("bar"); 069 070 List<String> list2 = new ArrayList<>(); 071 list2.add("foo"); 072 list2.add("bar"); 073 074 Predicate ip1 = new InPredicate<>(TEST_KEY, list1, false); 075 Predicate ip2 = new InPredicate<>(TEST_KEY, list2, false); 076 077 assertThat(ip1, both(equalTo(ip1)).and(equalTo(ip2))); 078 079 Predicate p = mock(Predicate.class); 080 assertThat(p, not(equalTo(ip1))); 081 082 list2.add("oink"); 083 ip2 = new InPredicate<>(TEST_KEY, list2, false); 084 assertThat(ip2, not(equalTo(ip1))); 085 } 086 087 @Test 088 public void testHashcode() { 089 List<String> list = new ArrayList<>(); 090 list.add("foo"); 091 list.add("bar"); 092 Predicate ip1 = new InPredicate<>(TEST_KEY, list, false); 093 Predicate ip2 = new InPredicate<>(TEST_KEY, list, false); 094 095 assertThat(ip1.hashCode(), both(equalTo(ip1.hashCode())).and(equalTo(ip2.hashCode()))); 096 097 Predicate p = mock(Predicate.class); 098 assertThat(p.hashCode(), not(equalTo(ip1.hashCode()))); 099 100 list.add("oink"); 101 ip2 = new InPredicate<>(TEST_KEY, list, false); 102 assertThat(ip2.hashCode(), not(equalTo(ip1.hashCode()))); 103 } 104 105 @Test 106 public void testNullConstructor() { 107 assertThrows(NullPointerException.class, () -> new InPredicate<>(null, null, false)); 108 } 109 110 @Test 111 public void testNullConstructor2() { 112 assertThrows( 113 NullPointerException.class, 114 () -> new InPredicate<>(OccurrenceSearchParameter.CATALOG_NUMBER, null, false)); 115 } 116 117 @Test 118 public void testNullValue() { 119 List<String> list = new ArrayList<>(); 120 list.add("foo"); 121 list.add(null); 122 list.add("bar"); 123 124 assertThrows( 125 NullPointerException.class, 126 () -> new InPredicate<>(TEST_KEY, list, false)); 127 } 128}