Browse Source

dbo; tests

tags/v1.0.0
Robin Thoni 7 years ago
parent
commit
c935a8cbb9
23 changed files with 379 additions and 285 deletions
  1. 3
    4
      app/src/main/java/com/rthoni/stssaguenay/business/STSBusiness.java
  2. 1
    4
      app/src/main/java/com/rthoni/stssaguenay/business/UsersSettingsBusiness.java
  3. 4
    23
      app/src/main/java/com/rthoni/stssaguenay/dbo/RoutesDbo.java
  4. 4
    24
      app/src/main/java/com/rthoni/stssaguenay/dbo/SchedulesDbo.java
  5. 4
    32
      app/src/main/java/com/rthoni/stssaguenay/dbo/StopsDbo.java
  6. 0
    21
      app/src/main/java/com/rthoni/stssaguenay/dbo/UserDbo.java
  7. 0
    33
      app/src/main/java/com/rthoni/stssaguenay/dbo/UserFavouriteStopsDbo.java
  8. 1
    28
      app/src/main/java/com/rthoni/stssaguenay/dbo/UsersSettingsDbo.java
  9. 2
    3
      app/src/main/java/com/rthoni/stssaguenay/ui/activities/MainActivity.java
  10. 1
    1
      app/src/main/res/values/strings.xml
  11. 2
    0
      luticateutils/build.gradle
  12. 11
    18
      luticateutils/src/main/java/com/luticate/utils/dbo/LuBoolDbo.java
  13. 1
    18
      luticateutils/src/main/java/com/luticate/utils/dbo/LuDataAccessConfigDbo.java
  14. 8
    1
      luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java
  15. 27
    0
      luticateutils/src/main/java/com/luticate/utils/dbo/LuDoubleDbo.java
  16. 12
    18
      luticateutils/src/main/java/com/luticate/utils/dbo/LuIntDbo.java
  17. 27
    0
      luticateutils/src/main/java/com/luticate/utils/dbo/LuLongDbo.java
  18. 16
    15
      luticateutils/src/main/java/com/luticate/utils/dbo/LuPaginatedDbo.java
  19. 12
    23
      luticateutils/src/main/java/com/luticate/utils/dbo/LuStringDbo.java
  20. 0
    14
      luticateutils/src/main/java/com/luticate/utils/dbo/LuVoidDbo.java
  21. 222
    0
      luticateutils/src/test/java/com/luticate/utils/BasicDboTest.java
  22. 10
    0
      luticateutils/src/test/java/com/luticate/utils/TestDbo.java
  23. 11
    5
      luticateutils/src/test/java/com/luticate/utils/TestDboTest.java

+ 3
- 4
app/src/main/java/com/rthoni/stssaguenay/business/STSBusiness.java View File

@@ -10,7 +10,6 @@ import com.rthoni.stssaguenay.dbo.UserDbo;
10 10
 import com.rthoni.stssaguenay.dbo.UsersSettingsDbo;
11 11
 
12 12
 import org.json.JSONArray;
13
-import org.json.JSONException;
14 13
 import org.json.JSONObject;
15 14
 
16 15
 import java.util.List;
@@ -29,8 +28,8 @@ public class STSBusiness {
29 28
             _config = new LuDataAccessConfigDbo();
30 29
             try {
31 30
                 String str = ctx.getString(R.string.luticate_config);
32
-                _config.fromJson(new JSONObject(str));
33
-            } catch (JSONException e) {
31
+                _config.fromJson(str);
32
+            } catch (Exception e) {
34 33
                 e.printStackTrace();
35 34
                 _config = null;
36 35
                 return null;
@@ -73,7 +72,7 @@ public class STSBusiness {
73 72
         if (str != null) {
74 73
             UserDbo user = new UserDbo();
75 74
             try {
76
-                user.fromString(str);
75
+                user.fromJson(str);
77 76
                 return user;
78 77
             } catch (Exception e) {
79 78
                 e.printStackTrace();

+ 1
- 4
app/src/main/java/com/rthoni/stssaguenay/business/UsersSettingsBusiness.java View File

@@ -6,9 +6,6 @@ import com.luticate.utils.dbo.LuVoidDbo;
6 6
 import com.rthoni.stssaguenay.dataaccess.UsersSettingsDataAccess;
7 7
 import com.rthoni.stssaguenay.dbo.UsersSettingsDbo;
8 8
 
9
-import org.json.JSONException;
10
-import org.json.JSONObject;
11
-
12 9
 /**
13 10
  * Created by robin on 10/3/16.
14 11
  */
@@ -29,7 +26,7 @@ public class UsersSettingsBusiness {
29 26
 
30 27
         UsersSettingsDbo settings = new UsersSettingsDbo();
31 28
         try {
32
-            settings.fromString(data);
29
+            settings.fromJson(data);
33 30
         } catch (Exception e) {
34 31
             e.printStackTrace();
35 32
             return null;

+ 4
- 23
app/src/main/java/com/rthoni/stssaguenay/dbo/RoutesDbo.java View File

@@ -3,11 +3,6 @@ package com.rthoni.stssaguenay.dbo;
3 3
 import com.luticate.utils.dbo.LuDbo;
4 4
 import com.luticate.utils.dbo.LuPaginatedDbo;
5 5
 
6
-import org.json.JSONException;
7
-import org.json.JSONObject;
8
-
9
-import java.util.HashMap;
10
-
11 6
 /**
12 7
  * Created by robin on 9/30/16.
13 8
  */
@@ -16,6 +11,10 @@ public class RoutesDbo extends LuDbo {
16 11
 
17 12
     public static class PaginatedRoutesDbo extends LuPaginatedDbo<RoutesDbo>
18 13
     {
14
+        public PaginatedRoutesDbo()
15
+        {
16
+            super(RoutesDbo.class);
17
+        }
19 18
     }
20 19
 
21 20
     protected String _id;
@@ -26,24 +25,6 @@ public class RoutesDbo extends LuDbo {
26 25
 
27 26
     protected String _fgColor;
28 27
 
29
-//    @Override
30
-//    public void fromJson(JSONObject json) throws JSONException {
31
-//        _id = json.getString("id");
32
-//        _name = json.getString("name");
33
-//        _bgColor = json.getString("bgColor");
34
-//        _fgColor = json.getString("fgColor");
35
-//    }
36
-//
37
-//    @Override
38
-//    public HashMap<String, Object> toArray() {
39
-//        HashMap<String, Object> map = new HashMap<>();
40
-//        map.put("id", _id);
41
-//        map.put("name", _name);
42
-//        map.put("bgColor", _bgColor);
43
-//        map.put("fgColor", _fgColor);
44
-//        return map;
45
-//    }
46
-
47 28
     public String getId() {
48 29
         return _id;
49 30
     }

+ 4
- 24
app/src/main/java/com/rthoni/stssaguenay/dbo/SchedulesDbo.java View File

@@ -4,16 +4,8 @@ import com.luticate.utils.dbo.LuDbo;
4 4
 import com.luticate.utils.dbo.LuPaginatedDbo;
5 5
 
6 6
 import org.joda.time.LocalDateTime;
7
-import org.joda.time.LocalTime;
8
-import org.joda.time.format.DateTimeFormat;
9
-import org.joda.time.format.DateTimeFormatter;
10
-import org.json.JSONArray;
11
-import org.json.JSONException;
12
-import org.json.JSONObject;
13 7
 
14
-import java.util.HashMap;
15 8
 import java.util.List;
16
-import java.util.Vector;
17 9
 
18 10
 /**
19 11
  * Created by robin on 10/1/16.
@@ -23,6 +15,10 @@ public class SchedulesDbo extends LuDbo {
23 15
 
24 16
     public static class SchedulesDboArray extends LuPaginatedDbo<SchedulesDbo>
25 17
     {
18
+        public SchedulesDboArray()
19
+        {
20
+            super(SchedulesDbo.class);
21
+        }
26 22
     }
27 23
 
28 24
     protected String _stopId;
@@ -31,22 +27,6 @@ public class SchedulesDbo extends LuDbo {
31 27
 
32 28
     protected List<LocalDateTime> _schedules;
33 29
 
34
-//    @Override
35
-//    public void fromJson(JSONObject json) throws JSONException {
36
-//        _stopId = json.getString("stopId");
37
-//        _routeId = json.getString("routeId");
38
-//        _schedules = new Vector<>();
39
-//        JSONArray schedules = json.getJSONArray("schedules");
40
-//        for (int i = 0; i < schedules.length(); ++i) {
41
-//            _schedules.add(LocalDateTime.parse(schedules.getString(i), DateTimeFormat.forPattern("y-M-d H:m:s")));
42
-//        }
43
-//    }
44
-//
45
-//    @Override
46
-//    public HashMap<String, Object> toArray() {
47
-//        return null;
48
-//    }
49
-
50 30
     public String getStopId() {
51 31
         return _stopId;
52 32
     }

+ 4
- 32
app/src/main/java/com/rthoni/stssaguenay/dbo/StopsDbo.java View File

@@ -1,17 +1,9 @@
1 1
 package com.rthoni.stssaguenay.dbo;
2 2
 
3
-import android.util.Pair;
4
-
5 3
 import com.luticate.utils.dbo.LuDbo;
6 4
 import com.luticate.utils.dbo.LuPaginatedDbo;
7 5
 
8
-import org.json.JSONArray;
9
-import org.json.JSONException;
10
-import org.json.JSONObject;
11
-
12
-import java.util.HashMap;
13 6
 import java.util.List;
14
-import java.util.Vector;
15 7
 
16 8
 /**
17 9
  * Created by robin on 9/29/16.
@@ -20,32 +12,12 @@ public class StopsDbo extends LuDbo {
20 12
 
21 13
     public static class PaginatedStopsDbo extends LuPaginatedDbo<StopsDbo>
22 14
     {
15
+        public PaginatedStopsDbo()
16
+        {
17
+            super(StopsDbo.class);
18
+        }
23 19
     }
24 20
 
25
-//    @Override
26
-//    public void fromJson(JSONObject json) throws JSONException {
27
-//        _name = json.getString("name");
28
-//        _routes = new Vector<>();
29
-//        JSONArray routes = json.getJSONArray("routes");
30
-//        for (int i = 0; i < routes.length(); ++i) {
31
-//            _routes.add(routes.getString(i));
32
-//        }
33
-//        _id = json.getString("id");
34
-//        _posX = json.getDouble("posX");
35
-//        _posY = json.getDouble("posY");
36
-//    }
37
-//
38
-//    @Override
39
-//    public HashMap<String, Object> toArray() {
40
-//        HashMap<String, Object> map = new HashMap<>();
41
-//        map.put("name", _name);
42
-//        map.put("routes", _routes);
43
-//        map.put("id", _id);
44
-//        map.put("posX", _posX);
45
-//        map.put("posY", _posY);
46
-//        return map;
47
-//    }
48
-
49 21
     protected String _name;
50 22
 
51 23
     protected List<String> _routes;

+ 0
- 21
app/src/main/java/com/rthoni/stssaguenay/dbo/UserDbo.java View File

@@ -2,11 +2,6 @@ package com.rthoni.stssaguenay.dbo;
2 2
 
3 3
 import com.luticate.utils.dbo.LuDbo;
4 4
 
5
-import org.json.JSONException;
6
-import org.json.JSONObject;
7
-
8
-import java.util.HashMap;
9
-
10 5
 /**
11 6
  * Created by robin on 10/3/16.
12 7
  */
@@ -19,22 +14,6 @@ public class UserDbo extends LuDbo {
19 14
 
20 15
     protected String _email;
21 16
 
22
-//    @Override
23
-//    public void fromJson(JSONObject json) throws JSONException {
24
-//        _id = json.getString("id");
25
-//        _name = json.getString("name");
26
-//        _email = json.getString("email");
27
-//    }
28
-//
29
-//    @Override
30
-//    public HashMap<String, Object> toArray() {
31
-//        HashMap<String, Object> map = new HashMap<>();
32
-//        map.put("id", _id);
33
-//        map.put("name", _name);
34
-//        map.put("email", _email);
35
-//        return map;
36
-//    }
37
-
38 17
     public String getId() {
39 18
         return _id;
40 19
     }

+ 0
- 33
app/src/main/java/com/rthoni/stssaguenay/dbo/UserFavouriteStopsDbo.java View File

@@ -2,13 +2,7 @@ package com.rthoni.stssaguenay.dbo;
2 2
 
3 3
 import com.luticate.utils.dbo.LuDbo;
4 4
 
5
-import org.json.JSONArray;
6
-import org.json.JSONException;
7
-import org.json.JSONObject;
8
-
9
-import java.util.HashMap;
10 5
 import java.util.List;
11
-import java.util.Vector;
12 6
 
13 7
 /**
14 8
  * Created by robin on 10/1/16.
@@ -22,33 +16,6 @@ public class UserFavouriteStopsDbo extends LuDbo {
22 16
 
23 17
     protected String _name;
24 18
 
25
-//    @Override
26
-//    public void fromJson(JSONObject json) throws JSONException {
27
-//        _stop = new StopsDbo();
28
-//        _stop.fromJson(json.getJSONObject("stop"));
29
-//        _routes = new Vector<>();
30
-//        JSONArray routes = json.getJSONArray("routes");
31
-//        for (int i = 0; i < routes.length(); ++i) {
32
-//            RoutesDbo route = new RoutesDbo();
33
-//            route.fromJson(routes.getJSONObject(i));
34
-//            _routes.add(route);
35
-//        }
36
-//        _name = json.getString("name");
37
-//    }
38
-//
39
-//    @Override
40
-//    public HashMap<String, Object> toArray() {
41
-//        HashMap<String, Object> map = new HashMap<>();
42
-//        map.put("stop", _stop.toArray());
43
-//        List<Object> routes = new Vector<>();
44
-//        for (RoutesDbo route : _routes) {
45
-//            routes.add(route.toArray());
46
-//        }
47
-//        map.put("routes", routes);
48
-//        map.put("name", _name);
49
-//        return map;
50
-//    }
51
-
52 19
     public StopsDbo getStop() {
53 20
         return _stop;
54 21
     }

+ 1
- 28
app/src/main/java/com/rthoni/stssaguenay/dbo/UsersSettingsDbo.java View File

@@ -2,42 +2,15 @@ package com.rthoni.stssaguenay.dbo;
2 2
 
3 3
 import com.luticate.utils.dbo.LuDbo;
4 4
 
5
-import org.json.JSONArray;
6
-import org.json.JSONException;
7
-import org.json.JSONObject;
8
-
9
-import java.util.HashMap;
10 5
 import java.util.List;
11
-import java.util.Vector;
12 6
 
13 7
 /**
14 8
  * Created by robin on 10/3/16.
15 9
  */
16 10
 
17 11
 public class UsersSettingsDbo extends LuDbo {
18
-    protected List<UserFavouriteStopsDbo> _favouriteStops;
19 12
 
20
-//    @Override
21
-//    public void fromJson(JSONObject json) throws JSONException {
22
-//        _favouriteStops = new Vector<>();
23
-//        JSONArray stops = json.getJSONArray("favouriteStops");
24
-//        for (int i = 0; i < stops.length(); ++i) {
25
-//            UserFavouriteStopsDbo stop = new UserFavouriteStopsDbo();
26
-//            stop.fromJson(stops.getJSONObject(i));
27
-//            _favouriteStops.add(stop);
28
-//        }
29
-//    }
30
-//
31
-//    @Override
32
-//    public HashMap<String, Object> toArray() {
33
-//        HashMap<String, Object> map = new HashMap<>();
34
-//        List<HashMap<String, Object>> stops = new Vector<>();
35
-//        for (UserFavouriteStopsDbo stop : _favouriteStops) {
36
-//            stops.add(stop.toArray());
37
-//        }
38
-//        map.put("favouriteStops", stops);
39
-//        return map;
40
-//    }
13
+    protected List<UserFavouriteStopsDbo> _favouriteStops;
41 14
 
42 15
     public List<UserFavouriteStopsDbo> getFavouriteStops() {
43 16
         return _favouriteStops;

+ 2
- 3
app/src/main/java/com/rthoni/stssaguenay/ui/activities/MainActivity.java View File

@@ -48,7 +48,6 @@ import com.rthoni.stssaguenay.dbo.UsersSettingsDbo;
48 48
 import com.rthoni.stssaguenay.ui.fragments.HomeFragment;
49 49
 import com.rthoni.stssaguenay.ui.fragments.StopViewerFragment;
50 50
 
51
-import org.json.JSONException;
52 51
 import org.json.JSONObject;
53 52
 
54 53
 import java.util.List;
@@ -279,7 +278,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
279 278
         if (requestCode == ADD_FAVOURITE_STOP_REQUEST_CODE && resultCode == RESULT_OK) {
280 279
             UserFavouriteStopsDbo favouriteStopDbo = new UserFavouriteStopsDbo();
281 280
             try {
282
-                favouriteStopDbo.fromString(data.getStringExtra(StopPickerActivity.FAVOURITE_STOP_EXTRA_NAME));
281
+                favouriteStopDbo.fromJson(data.getStringExtra(StopPickerActivity.FAVOURITE_STOP_EXTRA_NAME));
283 282
             } catch (Exception e) {
284 283
                 e.printStackTrace();
285 284
                 return;
@@ -297,7 +296,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
297 296
         else if (requestCode == SEARCH_STOP_REQUEST_CODE && resultCode == RESULT_OK) {
298 297
             final StopsDbo stopDbo = new StopsDbo();
299 298
             try {
300
-                stopDbo.fromString(data.getStringExtra(StopPickerActivity.STOP_EXTRA_NAME));
299
+                stopDbo.fromJson(data.getStringExtra(StopPickerActivity.STOP_EXTRA_NAME));
301 300
             } catch (Exception e) {
302 301
                 e.printStackTrace();
303 302
                 return;

+ 1
- 1
app/src/main/res/values/strings.xml View File

@@ -4,7 +4,7 @@
4 4
     <string name="ad_init">ca-app-pub-2016581167110949~3084832113</string>
5 5
     <string name="facebook_app_id">1463583156990249</string>
6 6
     <string name="map_api_key">AIzaSyD4FmpkQmk2t61Lx9spzpTx2CAy2CmijX0</string>
7
-    <string name="luticate_config">{\"baseURL\": \"https://sts.rthoni.com/api/v1/\"}</string>
7
+    <string name="luticate_config">{\"baseUrl\": \"https://sts.rthoni.com/api/v1/\"}</string>
8 8
 
9 9
 
10 10
     <string name="navigation_drawer_open">Open navigation drawer</string>

+ 2
- 0
luticateutils/build.gradle View File

@@ -24,6 +24,8 @@ unMock {
24 24
     downloadFrom 'https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar'
25 25
 
26 26
     keepStartingWith "org.json."
27
+
28
+    keep "android.util.Base64"
27 29
 }
28 30
 
29 31
 dependencies {

+ 11
- 18
luticateutils/src/main/java/com/luticate/utils/dbo/LuBoolDbo.java View File

@@ -1,33 +1,26 @@
1 1
 package com.luticate.utils.dbo;
2 2
 
3
-import org.json.JSONException;
4
-import org.json.JSONObject;
5
-
6
-import java.util.HashMap;
7
-
8 3
 /**
9 4
  * Created by robin on 11/27/15.
10 5
  */
11 6
 public class LuBoolDbo extends LuDbo {
12
-    public boolean isValue() {
13
-        return _value;
7
+
8
+    private Boolean _value;
9
+
10
+    public LuBoolDbo()
11
+    {
14 12
     }
15 13
 
16
-    public void setValue(boolean value) {
14
+    public LuBoolDbo(Boolean value)
15
+    {
17 16
         _value = value;
18 17
     }
19 18
 
20
-    private boolean _value;
21
-
22
-    @Override
23
-    public void fromJson(JSONObject json) throws JSONException {
24
-        _value = json.getBoolean("value");
19
+    public Boolean getBool() {
20
+        return _value;
25 21
     }
26 22
 
27
-    @Override
28
-    public HashMap<String, Object> toArray() {
29
-        HashMap<String, Object> array = new HashMap<>();
30
-        array.put("value", _value);
31
-        return array;
23
+    public void setBool(Boolean value) {
24
+        _value = value;
32 25
     }
33 26
 }

+ 1
- 18
luticateutils/src/main/java/com/luticate/utils/dbo/LuDataAccessConfigDbo.java View File

@@ -49,26 +49,9 @@ public class LuDataAccessConfigDbo extends LuDbo {
49 49
         Map<String, String> map = new HashMap<>();
50 50
         if (_httpUsername != null) {
51 51
             map.put("Authorization", "Basic " + Base64.encodeToString(
52
-                    String.format("%s:%s", _httpUsername, _httpPassword).getBytes(), Base64.DEFAULT));
52
+                    String.format("%s:%s", _httpUsername, _httpPassword).getBytes(), Base64.NO_WRAP));
53 53
         }
54 54
         return map;
55 55
     }
56 56
 
57
-    @Override
58
-    public void fromJson(JSONObject json) throws JSONException {
59
-        _baseUrl = json.getString("baseURL");
60
-        if (!json.isNull("httpUsername")) {
61
-            _httpUsername = json.getString("httpUsername");
62
-            _httpPassword = json.getString("httpPassword");
63
-        }
64
-        else {
65
-            _httpUsername = null;
66
-            _httpPassword = null;
67
-        }
68
-    }
69
-
70
-    @Override
71
-    public HashMap<String, Object> toArray() {
72
-        return null;
73
-    }
74 57
 }

+ 8
- 1
luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java View File

@@ -14,6 +14,7 @@ import org.json.JSONObject;
14 14
 
15 15
 import java.lang.reflect.Array;
16 16
 import java.lang.reflect.Field;
17
+import java.lang.reflect.Modifier;
17 18
 import java.lang.reflect.ParameterizedType;
18 19
 import java.util.AbstractMap;
19 20
 import java.util.HashMap;
@@ -171,7 +172,7 @@ public abstract class LuDbo {
171 172
         return obj == null ? null : obj.getClass();
172 173
     }
173 174
 
174
-    public void fromString(String data) throws Exception
175
+    public void fromJson(String data) throws Exception
175 176
     {
176 177
         JSONObject json = new JSONObject(data);
177 178
         fromJson(json);
@@ -180,6 +181,9 @@ public abstract class LuDbo {
180 181
     public void fromJson(JSONObject json) throws Exception
181 182
     {
182 183
         for (Field field : getClass().getDeclaredFields()) {
184
+            if (Modifier.isTransient(field.getModifiers())) {
185
+                continue;
186
+            }
183 187
             String key = getJsonFromField(field.getName());
184 188
             Class clazz = field.getType();
185 189
             if (json.has(key)) {
@@ -234,6 +238,9 @@ public abstract class LuDbo {
234 238
         HashMap<String, Object> map = new HashMap<>();
235 239
 
236 240
         for (Field field : getClass().getDeclaredFields()) {
241
+            if (Modifier.isTransient(field.getModifiers())) {
242
+                continue;
243
+            }
237 244
             String key = getJsonFromField(field.getName());
238 245
             Class clazz = field.getType();
239 246
             field.setAccessible(true);

+ 27
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/LuDoubleDbo.java View File

@@ -0,0 +1,27 @@
1
+package com.luticate.utils.dbo;
2
+
3
+/**
4
+ * Created by robin on 10/22/16.
5
+ */
6
+
7
+public class LuDoubleDbo extends LuDbo {
8
+
9
+    protected Double _value;
10
+
11
+    public LuDoubleDbo()
12
+    {
13
+    }
14
+
15
+    public LuDoubleDbo(Double value)
16
+    {
17
+        _value = value;
18
+    }
19
+
20
+    public Double getDouble() {
21
+        return _value;
22
+    }
23
+
24
+    public void setDouble(Double aDouble) {
25
+        _value = aDouble;
26
+    }
27
+}

+ 12
- 18
luticateutils/src/main/java/com/luticate/utils/dbo/LuIntDbo.java View File

@@ -1,33 +1,27 @@
1 1
 package com.luticate.utils.dbo;
2 2
 
3
-import org.json.JSONException;
4
-import org.json.JSONObject;
5
-
6
-import java.util.HashMap;
7
-
8 3
 /**
9 4
  * Created by robin on 11/27/15.
10 5
  */
11 6
 public class LuIntDbo extends LuDbo {
12
-    public int getInt() {
13
-        return _value;
7
+
8
+    private Integer _value;
9
+
10
+    public LuIntDbo()
11
+    {
14 12
     }
15 13
 
16
-    public void setInt(int value) {
14
+    public LuIntDbo(Integer value)
15
+    {
17 16
         _value = value;
18 17
     }
19 18
 
20
-    private int _value;
21
-
22
-    @Override
23
-    public void fromJson(JSONObject json) throws JSONException {
24
-        _value = json.getInt("value");
19
+    public Integer getInt() {
20
+        return _value;
25 21
     }
26 22
 
27
-    @Override
28
-    public HashMap<String, Object> toArray() {
29
-        HashMap<String, Object> array = new HashMap<>();
30
-        array.put("value", _value);
31
-        return array;
23
+    public void setInt(Integer value) {
24
+        _value = value;
32 25
     }
26
+
33 27
 }

+ 27
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/LuLongDbo.java View File

@@ -0,0 +1,27 @@
1
+package com.luticate.utils.dbo;
2
+
3
+/**
4
+ * Created by robin on 10/22/16.
5
+ */
6
+
7
+public class LuLongDbo extends LuDbo {
8
+
9
+    protected Long _value;
10
+
11
+    public LuLongDbo()
12
+    {
13
+    }
14
+
15
+    public LuLongDbo(Long value)
16
+    {
17
+        _value = value;
18
+    }
19
+
20
+    public Long getLong() {
21
+        return _value;
22
+    }
23
+
24
+    public void setLong(Long aLong) {
25
+        _value = aLong;
26
+    }
27
+}

+ 16
- 15
luticateutils/src/main/java/com/luticate/utils/dbo/LuPaginatedDbo.java View File

@@ -1,10 +1,8 @@
1 1
 package com.luticate.utils.dbo;
2 2
 
3 3
 import org.json.JSONArray;
4
-import org.json.JSONException;
5 4
 import org.json.JSONObject;
6 5
 
7
-import java.lang.reflect.ParameterizedType;
8 6
 import java.util.HashMap;
9 7
 import java.util.List;
10 8
 import java.util.Vector;
@@ -15,6 +13,17 @@ import java.util.Vector;
15 13
  */
16 14
 public class LuPaginatedDbo<T extends LuDbo> extends LuDbo {
17 15
 
16
+    private transient Class<T> _clazz;
17
+
18
+    private List<T> _data = new Vector<>();
19
+
20
+    private int _count;
21
+
22
+    public LuPaginatedDbo(Class<T> clazz)
23
+    {
24
+        _clazz = clazz;
25
+    }
26
+
18 27
     public List<T> getData() {
19 28
         return _data;
20 29
     }
@@ -31,23 +40,15 @@ public class LuPaginatedDbo<T extends LuDbo> extends LuDbo {
31 40
         _count = count;
32 41
     }
33 42
 
34
-    private List<T> _data = new Vector<>();
35
-
36
-    private int _count;
37
-
38 43
     @Override
39
-    public void fromJson(JSONObject json) throws JSONException {
44
+    public void fromJson(JSONObject json) throws Exception {
40 45
         _count = json.getInt("count");
41 46
         JSONArray array = json.getJSONArray("data");
47
+
42 48
         for (int i = 0; i < array.length(); ++i) {
43
-            try {
44
-                T dbo = (T)((Class)((ParameterizedType)this.getClass().
45
-                        getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
46
-                dbo.fromJson(array.getJSONObject(i));
47
-                _data.add(dbo);
48
-            } catch (Exception e) {
49
-                e.printStackTrace();
50
-            }
49
+            T dbo = _clazz.newInstance();
50
+            dbo.fromJson(array.getJSONObject(i));
51
+            _data.add(dbo);
51 52
         }
52 53
     }
53 54
 

+ 12
- 23
luticateutils/src/main/java/com/luticate/utils/dbo/LuStringDbo.java View File

@@ -1,38 +1,27 @@
1 1
 package com.luticate.utils.dbo;
2 2
 
3
-import org.json.JSONException;
4
-import org.json.JSONObject;
5
-
6
-import java.util.HashMap;
7
-
8 3
 /**
9 4
  * Created by robin on 11/27/15.
10 5
  */
11 6
 public class LuStringDbo extends LuDbo {
12
-    public String getString() {
13
-        return _value;
7
+
8
+    private String _value;
9
+
10
+    public LuStringDbo()
11
+    {
14 12
     }
15 13
 
16
-    public void setString(String value) {
14
+    public LuStringDbo(String value)
15
+    {
17 16
         _value = value;
18 17
     }
19 18
 
20
-    private String _value;
21
-
22
-    @Override
23
-    public void fromJson(JSONObject json) throws JSONException {
24
-        if (json.isNull("value")) {
25
-            _value = null;
26
-        }
27
-        else {
28
-            _value = json.getString("value");
29
-        }
19
+    public String getString() {
20
+        return _value;
30 21
     }
31 22
 
32
-    @Override
33
-    public HashMap<String, Object> toArray() {
34
-        HashMap<String, Object> array = new HashMap<>();
35
-        array.put("value", _value);
36
-        return array;
23
+    public void setString(String value) {
24
+        _value = value;
37 25
     }
26
+
38 27
 }

+ 0
- 14
luticateutils/src/main/java/com/luticate/utils/dbo/LuVoidDbo.java View File

@@ -1,21 +1,7 @@
1 1
 package com.luticate.utils.dbo;
2 2
 
3
-import org.json.JSONException;
4
-import org.json.JSONObject;
5
-
6
-import java.util.HashMap;
7
-
8 3
 /**
9 4
  * Created by robin on 11/27/15.
10 5
  */
11 6
 public class LuVoidDbo extends LuDbo {
12
-    @Override
13
-    public void fromJson(JSONObject json) throws JSONException {
14
-
15
-    }
16
-
17
-    @Override
18
-    public HashMap<String, Object> toArray() {
19
-        return null;
20
-    }
21 7
 }

+ 222
- 0
luticateutils/src/test/java/com/luticate/utils/BasicDboTest.java View File

@@ -0,0 +1,222 @@
1
+package com.luticate.utils;
2
+
3
+import com.luticate.utils.dbo.LuBoolDbo;
4
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
5
+import com.luticate.utils.dbo.LuDoubleDbo;
6
+import com.luticate.utils.dbo.LuIntDbo;
7
+import com.luticate.utils.dbo.LuLongDbo;
8
+import com.luticate.utils.dbo.LuPaginatedDbo;
9
+import com.luticate.utils.dbo.LuStringDbo;
10
+import com.luticate.utils.dbo.LuVoidDbo;
11
+
12
+import org.junit.Test;
13
+
14
+import java.util.HashMap;
15
+import java.util.List;
16
+import java.util.Map;
17
+import java.util.Vector;
18
+
19
+import static org.junit.Assert.*;
20
+
21
+/**
22
+ * Created by robin on 10/22/16.
23
+ */
24
+
25
+public class BasicDboTest {
26
+
27
+    @Test
28
+    public void boolTest() throws Exception {
29
+        LuBoolDbo dbo = new LuBoolDbo();
30
+        dbo.fromJson("{\"value\": true}");
31
+        assertTrue(dbo.getBool());
32
+
33
+        dbo = new LuBoolDbo();
34
+        dbo.fromJson("{\"value\": null}");
35
+        assertNull(dbo.getBool());
36
+
37
+        dbo = new LuBoolDbo();
38
+        dbo.fromJson("{\"value\": false}");
39
+        assertFalse(dbo.getBool());
40
+        dbo.setBool(true);
41
+
42
+        LuBoolDbo dbo2 = new LuBoolDbo();
43
+        dbo2.fromJson(dbo.toString());
44
+        assertTrue(dbo2.getBool());
45
+
46
+        dbo = new LuBoolDbo(false);
47
+        assertFalse(dbo.getBool());
48
+
49
+        dbo = new LuBoolDbo(true);
50
+        assertTrue(dbo.getBool());
51
+    }
52
+
53
+    @Test
54
+    public void intTest() throws Exception {
55
+        LuIntDbo dbo = new LuIntDbo();
56
+        dbo.fromJson("{\"value\": 42}");
57
+        assertEquals(42, dbo.getInt().intValue());
58
+
59
+        dbo = new LuIntDbo();
60
+        dbo.fromJson("{\"value\": null}");
61
+        assertNull(dbo.getInt());
62
+
63
+        dbo = new LuIntDbo();
64
+        dbo.fromJson("{\"value\": 24}");
65
+        assertEquals(24, dbo.getInt().intValue());
66
+        dbo.setInt(4242);
67
+
68
+        LuIntDbo dbo2 = new LuIntDbo();
69
+        dbo2.fromJson(dbo.toString());
70
+        assertEquals(4242, dbo2.getInt().intValue());
71
+
72
+        dbo = new LuIntDbo(42);
73
+        assertEquals(42, dbo.getInt().intValue());
74
+
75
+        dbo = new LuIntDbo(24);
76
+        assertEquals(24, dbo.getInt().intValue());
77
+    }
78
+
79
+    @Test
80
+    public void longTest() throws Exception {
81
+        LuLongDbo dbo = new LuLongDbo();
82
+        dbo.fromJson("{\"value\": 42}");
83
+        assertEquals(42, dbo.getLong().longValue());
84
+
85
+        dbo = new LuLongDbo();
86
+        dbo.fromJson("{\"value\": null}");
87
+        assertNull(dbo.getLong());
88
+
89
+        dbo = new LuLongDbo();
90
+        dbo.fromJson("{\"value\": 24}");
91
+        assertEquals(24, dbo.getLong().longValue());
92
+        dbo.setLong((long) 4242);
93
+
94
+        LuLongDbo dbo2 = new LuLongDbo();
95
+        dbo2.fromJson(dbo.toString());
96
+        assertEquals(4242, dbo2.getLong().longValue());
97
+
98
+        dbo = new LuLongDbo((long) 42);
99
+        assertEquals(42, dbo.getLong().longValue());
100
+
101
+        dbo = new LuLongDbo((long) 24);
102
+        assertEquals(24, dbo.getLong().longValue());
103
+    }
104
+
105
+    @Test
106
+    public void doubleTest() throws Exception {
107
+        LuDoubleDbo dbo = new LuDoubleDbo();
108
+        dbo.fromJson("{\"value\": 42.125}");
109
+        assertEquals(42.125, dbo.getDouble(), 0);
110
+
111
+        dbo = new LuDoubleDbo();
112
+        dbo.fromJson("{\"value\": null}");
113
+        assertNull(dbo.getDouble());
114
+
115
+        dbo = new LuDoubleDbo();
116
+        dbo.fromJson("{\"value\": 24.125}");
117
+        assertEquals(24.125, dbo.getDouble(), 0);
118
+        dbo.setDouble(4242.125);
119
+
120
+        LuDoubleDbo dbo2 = new LuDoubleDbo();
121
+        dbo2.fromJson(dbo.toString());
122
+        assertEquals(4242.125, dbo2.getDouble(), 0);
123
+
124
+        dbo = new LuDoubleDbo(42.125);
125
+        assertEquals(42.125, dbo.getDouble(), 0);
126
+
127
+        dbo = new LuDoubleDbo(24.125);
128
+        assertEquals(24.125, dbo.getDouble(), 0);
129
+    }
130
+
131
+    @Test
132
+    public void stringTest() throws Exception {
133
+        LuStringDbo dbo = new LuStringDbo();
134
+        dbo.fromJson("{\"value\": \"42\"}");
135
+        assertEquals("42", dbo.getString());
136
+
137
+        dbo = new LuStringDbo();
138
+        dbo.fromJson("{\"value\": null}");
139
+        assertNull(dbo.getString());
140
+
141
+        dbo = new LuStringDbo();
142
+        dbo.fromJson("{\"value\": \"24\"}");
143
+        assertEquals("24", dbo.getString());
144
+        dbo.setString("4242");
145
+
146
+        LuStringDbo dbo2 = new LuStringDbo();
147
+        dbo2.fromJson(dbo.toString());
148
+        assertEquals("4242", dbo2.getString());
149
+
150
+        dbo = new LuStringDbo("42 42");
151
+        assertEquals("42 42", dbo.getString());
152
+
153
+        dbo = new LuStringDbo("24 24");
154
+        assertEquals("24 24", dbo.getString());
155
+    }
156
+
157
+    @Test
158
+    public void paginatedTest() throws Exception {
159
+        LuPaginatedDbo<LuIntDbo> dbo = new LuPaginatedDbo<>(LuIntDbo.class);
160
+        dbo.fromJson("{\"count\": 42, \"data\": [{\"value\": 1}, {\"value\": 2}, {\"value\": 3}]}");
161
+        assertEquals(42, dbo.getCount());
162
+        assertNotNull(dbo.getData());
163
+        assertEquals(3, dbo.getData().size());
164
+        assertEquals(1, dbo.getData().get(0).getInt().intValue());
165
+        assertEquals(2, dbo.getData().get(1).getInt().intValue());
166
+        assertEquals(3, dbo.getData().get(2).getInt().intValue());
167
+
168
+        dbo.setCount(24);
169
+        List<LuIntDbo> list = new Vector<>();
170
+        list.add(new LuIntDbo(10));
171
+        list.add(new LuIntDbo(20));
172
+        dbo.setData(list);;
173
+        assertEquals(24, dbo.getCount());
174
+        assertNotNull(dbo.getData());
175
+        assertEquals(2, dbo.getData().size());
176
+        assertEquals(10, dbo.getData().get(0).getInt().intValue());
177
+        assertEquals(20, dbo.getData().get(1).getInt().intValue());
178
+
179
+        HashMap<String, Object> array = dbo.toArray();
180
+        assertEquals(2, array.size());
181
+        assertTrue(array.containsKey("count"));
182
+        assertEquals(24, array.get("count"));
183
+        assertTrue(array.containsKey("data"));
184
+        assertEquals(2, ((List)array.get("data")).size());
185
+    }
186
+
187
+    @Test
188
+    public void dataAccessConfigTest() throws Exception {
189
+        LuDataAccessConfigDbo dbo = new LuDataAccessConfigDbo();
190
+        dbo.fromJson("{\"baseUrl\": \"http://api/\", \"httpUsername\": \"root\", \"httpPassword\": \"toor42\"}");
191
+        assertEquals("http://api/", dbo.getBaseUrl());
192
+        assertEquals("root", dbo.getHttpUsername());
193
+        assertEquals("toor42", dbo.getHttpPassword());
194
+        Map<String, String> headers = dbo.getHeaders();
195
+        assertEquals(1, headers.size());
196
+        assertTrue(headers.containsKey("Authorization"));
197
+        assertEquals("Basic cm9vdDp0b29yNDI=", headers.get("Authorization"));
198
+
199
+        dbo = new LuDataAccessConfigDbo();
200
+        dbo.fromJson("{\"baseUrl\": \"http://api2/\"}");
201
+        assertEquals("http://api2/", dbo.getBaseUrl());
202
+        assertNull(dbo.getHttpUsername());
203
+        assertNull(dbo.getHttpPassword());
204
+        headers = dbo.getHeaders();
205
+        assertEquals(0, headers.size());
206
+        dbo.setBaseUrl("http://api/");
207
+        dbo.setHttpUsername("root");
208
+        dbo.setHttpPassword("toor42");
209
+        assertEquals("http://api/", dbo.getBaseUrl());
210
+        assertEquals("root", dbo.getHttpUsername());
211
+        assertEquals("toor42", dbo.getHttpPassword());
212
+
213
+        HashMap<String, Object> array = dbo.toArray();
214
+        assertEquals(3, array.size());
215
+        assertTrue(array.containsKey("baseUrl"));
216
+        assertEquals("http://api/", array.get("baseUrl"));
217
+        assertTrue(array.containsKey("httpUsername"));
218
+        assertEquals("root", array.get("httpUsername"));
219
+        assertTrue(array.containsKey("httpPassword"));
220
+        assertEquals("toor42", array.get("httpPassword"));
221
+    }
222
+}

+ 10
- 0
luticateutils/src/test/java/com/luticate/utils/TestDbo.java View File

@@ -48,6 +48,8 @@ public class TestDbo extends LuDbo implements Serializable {
48 48
     protected String _aNullString;
49 49
     protected Class _object = TestDbo.class;
50 50
 
51
+    protected transient String _transientField;
52
+
51 53
     public byte getByte() {
52 54
         return _byte;
53 55
     }
@@ -264,6 +266,14 @@ public class TestDbo extends LuDbo implements Serializable {
264 266
         _object = object;
265 267
     }
266 268
 
269
+    public String getTransientField() {
270
+        return _transientField;
271
+    }
272
+
273
+    public void setTransientField(String transientField) {
274
+        _transientField = transientField;
275
+    }
276
+
267 277
     @Override
268 278
     public boolean equals(Object o) {
269 279
         if (this == o) return true;

+ 11
- 5
luticateutils/src/test/java/com/luticate/utils/TestDboTest.java View File

@@ -75,7 +75,9 @@ public class TestDboTest {
75 75
 
76 76
             "\"aNullString\": null," +
77 77
 
78
-            "\"object\": {}" +
78
+            "\"object\": {}," +
79
+
80
+            "\"transientField\": \"a transient field\"" +
79 81
 
80 82
             "}";
81 83
 
@@ -145,13 +147,15 @@ public class TestDboTest {
145 147
         assertNull(test.getaNullString());
146 148
         assertNull(test.getObject());
147 149
 
150
+        assertNull(test.getTransientField());
151
+
148 152
         assertEquals(test, test);
149 153
     }
150 154
 
151 155
     @Test
152 156
     public void deserializeTest() throws Exception {
153 157
         TestDbo test = new TestDbo();
154
-        test.fromString(TEST_DBO_JSON);
158
+        test.fromJson(TEST_DBO_JSON);
155 159
         testTestDbo(test);
156 160
     }
157 161
 
@@ -159,9 +163,11 @@ public class TestDboTest {
159 163
     public void serializeTest() throws Exception
160 164
     {
161 165
         TestDbo test = new TestDbo();
162
-        test.fromString(TEST_DBO_JSON);
166
+        test.fromJson(TEST_DBO_JSON);
163 167
         JSONObject json = test.toJson();
164 168
 
169
+        assertFalse(json.has("transientField"));
170
+
165 171
         TestDbo test2 = new TestDbo();
166 172
         test2.fromJson(json);
167 173
 
@@ -172,10 +178,10 @@ public class TestDboTest {
172 178
     public void serializeStringTest() throws Exception
173 179
     {
174 180
         TestDbo test = new TestDbo();
175
-        test.fromString(TEST_DBO_JSON);
181
+        test.fromJson(TEST_DBO_JSON);
176 182
 
177 183
         TestDbo test2 = new TestDbo();
178
-        test2.fromString(test.toString());
184
+        test2.fromJson(test.toString());
179 185
 
180 186
         testTestDbo(test2);
181 187
     }

Loading…
Cancel
Save