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.List;
30  import java.util.concurrent.atomic.AtomicInteger;
31  import java.util.stream.Stream;
32  import org.synchronoss.cpo.core.CpoAdapter;
33  import org.synchronoss.cpo.core.CpoAdapterFactoryManager;
34  import org.synchronoss.cpo.core.CpoOrderBy;
35  import org.synchronoss.cpo.core.CpoQuery;
36  import org.synchronoss.cpo.jdbc.ValueObject;
37  import org.testng.annotations.AfterClass;
38  import org.testng.annotations.BeforeClass;
39  import org.testng.annotations.Test;
40  
41  /**
42   * BlobTest is a test class for testing the JdbcAdapter class Constructors
43   *
44   * @author david berry
45   */
46  public class OrderByTest {
47  
48    // unique id base so this class's rows never collide with another test class's
49    private static final int IDB = 1800000;
50  
51    private CpoAdapter cpoAdapter = null;
52    private final ArrayList<ValueObject> al = new ArrayList<>();
53  
54    public OrderByTest() {}
55  
56    /**
57     * <code>setUp</code> Load the datasource from the properties in the property file
58     * jdbc_en_US.properties
59     */
60    @BeforeClass
61    public void setUp() {
62      String method = "setUp:";
63  
64      try {
65        cpoAdapter = CpoAdapterFactoryManager.getCpoAdapter(JdbcStatics.ADAPTER_CONTEXT_JDBC);
66        assertNotNull(cpoAdapter, method + "cpoAdapter is null");
67        // Add the test valueObjects
68      } catch (Exception e) {
69        fail(method + e.getMessage());
70      }
71  
72      al.add(ValueObjectFactory.createValueObject(IDB + 1));
73      al.add(ValueObjectFactory.createValueObject(IDB + 2));
74      al.add(ValueObjectFactory.createValueObject(IDB + 3));
75      al.add(ValueObjectFactory.createValueObject(IDB + 4));
76      al.add(ValueObjectFactory.createValueObject(IDB + 5));
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      } catch (Exception e) {
91        fail(method + e.getMessage());
92      }
93      cpoAdapter = null;
94    }
95  
96    /** DOCUMENT ME! */
97    @Test
98    public void testNewOrderBy() {
99      String method = "testNewOrderBy:";
100     Collection<ValueObject> col;
101     String marker = "MY_MARKER";
102     String attribute = "MY_ATTRIBUTE";
103     String function = "MY_FUNCTION";
104     boolean ascending = false;
105 
106     try {
107       CpoOrderBy cob = cpoAdapter.newOrderBy(marker, attribute, ascending, function);
108       assertEquals(cob.getMarker(), marker);
109       assertEquals(cob.getAttribute(), attribute);
110       assertEquals(cob.getAscending(), ascending);
111       assertEquals(cob.getFunction(), function);
112     } catch (Exception e) {
113       fail(method + e.getMessage());
114     }
115   }
116 
117   /** DOCUMENT ME! */
118   @Test
119   public void testOrderByAscending() {
120     String method = "testOrderByAscending:";
121     Collection<ValueObject> col;
122 
123     try {
124       CpoOrderBy cob = cpoAdapter.newOrderBy(ValueObject.ATTR_ID, true);
125       CpoOrderBy cob1 =
126           cpoAdapter.newOrderBy(CpoOrderBy.DEFAULT_MARKER, ValueObject.ATTR_ATTRVARCHAR, true);
127       Collection<CpoOrderBy> colCob = new ArrayList<>();
128       colCob.add(cob);
129       colCob.add(cob1);
130       ValueObject valObj = ValueObjectFactory.createValueObject();
131       try (Stream<ValueObject> beans =
132           cpoAdapter.retrieveBeans(
133               CpoQuery.group(ValueObject.FG_LIST_TESTORDERBYRETRIEVE).orderBys(colCob), valObj); ) {
134         AtomicInteger id = new AtomicInteger(IDB + 1);
135         beans
136             .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
137             .forEach(bean -> assertEquals(bean.getId(), id.getAndIncrement()));
138       }
139     } catch (Exception e) {
140       fail(method + e.getMessage());
141     }
142   }
143 
144   /** DOCUMENT ME! */
145   @Test
146   public void testOrderByDescending() {
147     String method = "testOrderByDescending:";
148     List<ValueObject> col;
149 
150     try {
151       CpoOrderBy cob = cpoAdapter.newOrderBy(ValueObject.ATTR_ID, false, null);
152       CpoOrderBy cob2 =
153           cpoAdapter.newOrderBy(
154               CpoOrderBy.DEFAULT_MARKER, ValueObject.ATTR_ATTRVARCHAR, false, null);
155       Collection<CpoOrderBy> colCob = new ArrayList<>();
156       colCob.add(cob);
157       colCob.add(cob2);
158       ValueObject valObj = ValueObjectFactory.createValueObject();
159       try (Stream<ValueObject> beans =
160           cpoAdapter.retrieveBeans(
161               CpoQuery.group(ValueObject.FG_LIST_TESTORDERBYRETRIEVE).orderBys(colCob), valObj); ) {
162         AtomicInteger id = new AtomicInteger(IDB + 5);
163         beans
164             .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
165             .forEach(bean -> assertEquals(bean.getId(), id.getAndDecrement()));
166       }
167     } catch (Exception e) {
168       fail(method + e.getMessage());
169     }
170   }
171 
172   @Test
173   public void testOrderByFunction() {
174     String method = "testOrderByFunction:";
175     Collection<ValueObject> col;
176 
177     ValueObject vobj = ValueObjectFactory.createValueObject(-(IDB + 6));
178     try {
179       cpoAdapter.insertBean(ValueObject.FG_CREATE_TESTORDERBYINSERT, vobj);
180       CpoOrderBy cob = cpoAdapter.newOrderBy(ValueObject.ATTR_ID, true, "ABS(id)");
181       Collection<CpoOrderBy> colCob = new ArrayList<>();
182       colCob.add(cob);
183       ValueObject valObj = ValueObjectFactory.createValueObject();
184       try (Stream<ValueObject> beans =
185           cpoAdapter.retrieveBeans(
186               CpoQuery.group(ValueObject.FG_LIST_TESTORDERBYRETRIEVE).orderBys(colCob), valObj); ) {
187         AtomicInteger id = new AtomicInteger(IDB + 1);
188         beans
189             .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
190             .forEach(bean -> assertEquals(Math.abs(bean.getId()), id.getAndIncrement()));
191       }
192     } catch (Exception e) {
193       fail(method + e.getMessage());
194     } finally {
195       try {
196         cpoAdapter.deleteBean(ValueObject.FG_DELETE_TESTORDERBYDELETE, vobj);
197       } catch (Exception e) {
198         fail(method + e.getMessage());
199       }
200     }
201   }
202 }