View Javadoc
1   package org.synchronoss.cpo.jdbc.adapter;
2   
3   /*-
4    * [[
5    * jdbc
6    * ==
7    * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8    * ==
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ]]
23   */
24  
25  import static org.testng.Assert.*;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.stream.Stream;
30  import org.synchronoss.cpo.core.CpoAdapter;
31  import org.synchronoss.cpo.core.CpoAdapterFactoryManager;
32  import org.synchronoss.cpo.core.CpoNativeFunction;
33  import org.synchronoss.cpo.core.CpoQuery;
34  import org.synchronoss.cpo.jdbc.ValueObject;
35  import org.testng.annotations.AfterClass;
36  import org.testng.annotations.BeforeClass;
37  import org.testng.annotations.Test;
38  
39  /**
40   * BlobTest is a test class for testing the JdbcAdapter class Constructors
41   *
42   * @author david berry
43   */
44  public class NativeExpressionTest {
45  
46    // unique id base so this class's rows never collide with another test class's
47    private static final int IDB = 1700000;
48  
49    private CpoAdapter cpoAdapter = null;
50    private final ArrayList<ValueObject> al = new ArrayList<>();
51  
52    public NativeExpressionTest() {}
53  
54    /**
55     * <code>setUp</code> Load the datasource from the properties in the property file
56     * jdbc_en_US.properties
57     */
58    @BeforeClass
59    public void setUp() {
60      String method = "setUp:";
61  
62      try {
63        cpoAdapter = CpoAdapterFactoryManager.getCpoAdapter(JdbcStatics.ADAPTER_CONTEXT_JDBC);
64        assertNotNull(cpoAdapter, method + "cpoAdapter is null");
65      } catch (Exception e) {
66        fail(method + e.getMessage());
67      }
68      ValueObject vo = ValueObjectFactory.createValueObject(IDB + 1);
69      vo.setAttrVarChar("Test");
70      vo.setAttrSmallInt((short) 1);
71      al.add(vo);
72      al.add(ValueObjectFactory.createValueObject(IDB + 2));
73      al.add(ValueObjectFactory.createValueObject(IDB + 3));
74      al.add(ValueObjectFactory.createValueObject(IDB + 4));
75      al.add(ValueObjectFactory.createValueObject(IDB + 5));
76      al.add(ValueObjectFactory.createValueObject(-(IDB + 6)));
77      try {
78        cpoAdapter.insertBeans(ValueObject.FG_CREATE_TESTORDERBYINSERT, al);
79      } catch (Exception e) {
80        fail(method + e.getMessage());
81      }
82    }
83  
84    /** DOCUMENT ME! */
85    @AfterClass
86    public void tearDown() {
87      String method = "tearDown:";
88      try {
89        cpoAdapter.deleteBeans(ValueObject.FG_DELETE_TESTORDERBYDELETE, al);
90  
91      } catch (Exception e) {
92        fail(method + e.getMessage());
93      }
94      cpoAdapter = null;
95    }
96  
97    @Test
98    public void testNativeInWhere() {
99      String method = "testNativeOrWhere:";
100     Collection<ValueObject> col;
101 
102     try {
103       ArrayList<CpoNativeFunction> cnqAl = new ArrayList<>();
104 
105       cnqAl.add(
106           new CpoNativeFunction(
107               "__CPO_WHERE__", "WHERE id IN (" + (IDB + 2) + "," + (IDB + 3) + ")"));
108 
109       ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
110       try (Stream<ValueObject> beans =
111           cpoAdapter.retrieveBeans(
112               CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).nativeExpressions(cnqAl),
113               valObj,
114               valObj); ) {
115         long count =
116             beans
117                 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
118                 .count();
119         assertEquals(count, 2, "Number of beans is " + count);
120       }
121     } catch (Exception e) {
122       fail(method + e.getMessage());
123     }
124   }
125 
126   /** DOCUMENT ME! */
127   @Test
128   public void testNativeOrWhere() {
129     String method = "testNativeOrWhere:";
130     Collection<ValueObject> col;
131 
132     try {
133       ArrayList<CpoNativeFunction> cnqAl = new ArrayList<>();
134 
135       cnqAl.add(
136           new CpoNativeFunction(
137               "__CPO_WHERE__", "WHERE ID = " + (IDB + 2) + " OR ID = " + (IDB + 3)));
138 
139       ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
140       try (Stream<ValueObject> beans =
141           cpoAdapter.retrieveBeans(
142               CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).nativeExpressions(cnqAl),
143               valObj,
144               valObj); ) {
145         long count =
146             beans
147                 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
148                 .count();
149         assertEquals(count, 2, "Number of beans is " + count);
150       }
151     } catch (Exception e) {
152       fail(method + e.getMessage());
153     }
154   }
155 
156   @Test
157   public void testNullNative() {
158     String method = "testNullNative:";
159     Collection<ValueObject> col;
160 
161     try {
162       ArrayList<CpoNativeFunction> cnqAl = new ArrayList<>();
163 
164       cnqAl.add(new CpoNativeFunction("__CPO_WHERE__", null));
165 
166       ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
167       try (Stream<ValueObject> beans =
168           cpoAdapter.retrieveBeans(
169               CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).nativeExpressions(cnqAl),
170               valObj,
171               valObj); ) {
172         long count =
173             beans
174                 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
175                 .count();
176         assertEquals(count, 6, "Number of beans is " + count);
177       }
178     } catch (Exception e) {
179       fail(method + e.getMessage());
180     }
181   }
182 }