Skip to content

Commit 035bc48

Browse files
committed
fix baseViewModel to post on null liveData
Tester with annotations to check Object fields Clone util
1 parent 9a90b9e commit 035bc48

7 files changed

Lines changed: 122 additions & 41 deletions

File tree

app/src/test/java/base_library/utils/test/CoreAnnotationsTest.java renamed to app/src/test/java/base_library/utils/test/TesterTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,48 @@
99
/**
1010
* Created by Galeen on 2019-08-13.
1111
*/
12-
public class CoreAnnotationsTest {
12+
public class TesterTest {
1313
@Test
1414
public void testNonNullsObject() {
1515
TestObjectNulls testObject = new TestObjectNulls(0, "", 0L, false);
16-
String nullFields = CoreAnnotations.getNullFields(testObject, true);
16+
String nullFields = Tester.getNullFields(testObject, true);
1717
assertNull("has null fields", nullFields);
1818
}
1919

2020
@Test
2121
public void testNullsObject() {
2222
TestObjectNulls testObject = new TestObjectNulls(0, null, null, false);
23-
String nullFields = CoreAnnotations.getNullFields(testObject, true);
23+
String nullFields = Tester.getNullFields(testObject, true);
2424
assertNotNull("must have errors", nullFields);
2525
System.out.println("testNullsObject : " + nullFields);
2626
}
2727

2828
@Test
2929
public void testPrimitivesObject() {
3030
TestObjectPrimitives testObject = new TestObjectPrimitives(0, "", 1L, false);
31-
String nullFields = CoreAnnotations.getNullFields(testObject, true);
31+
String nullFields = Tester.getNullFields(testObject, true);
3232
assertNotNull("must have errors", nullFields);
3333
System.out.println("testPrimitivesObject : " + nullFields);
3434
}
3535

3636
@Test
3737
public void testExcludeObject() {
3838
TestObjectExcludes testObject = new TestObjectExcludes(0, "", null, false);
39-
String nullFields = CoreAnnotations.getNullFields(testObject, true);
39+
String nullFields = Tester.getNullFields(testObject, true);
4040
assertNull("time is not excluded from check", nullFields);
4141
}
4242

4343
@Test
4444
public void testMandatoryObject() {
4545
TestObjectMandatory testObject = new TestObjectMandatory(0, "1", null, false);
46-
String nullFields = CoreAnnotations.getNullFields(testObject, false);
46+
String nullFields = Tester.getNullFields(testObject, false);
4747
assertNull("time is check when is not mandatory", nullFields);
4848
}
4949

5050
@Test
5151
public void testMandatoryEmptyObject() {
5252
TestObjectMandatory testObject = new TestObjectMandatory(0, "", null, false);
53-
String nullFields = CoreAnnotations.getNullFields(testObject, false);
53+
String nullFields = Tester.getNullFields(testObject, false);
5454
assertNotNull("id is empty and mandatory", nullFields);
5555
System.out.println("testMandatoryEmptyObject : " + nullFields);
5656
}

core_library/src/main/java/com/futurist_labs/android/base_library/ui/BaseViewModel.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,34 +77,35 @@ LiveData<InfoMessage> shouldShowMessage() {
7777
}
7878

7979
public void showError(String msg) {
80-
showError.setValue(new NetworkResponse(msg));
80+
if (showError != null) showError.setValue(new NetworkResponse(msg));
8181
}
8282

8383
public void showError(int msg) {
84-
showError.setValue(new NetworkResponse(msg));
84+
if (showError != null) showError.setValue(new NetworkResponse(msg));
8585
}
8686

8787
public void showErrorDialog(int msg) {
88-
showErrorDialog.setValue(new NetworkResponse(msg));
88+
if (showErrorDialog != null) showErrorDialog.setValue(new NetworkResponse(msg));
8989
}
9090

9191
public void setShowErrorDialog(String msg) {
92-
showErrorDialog.setValue(new NetworkResponse(msg));
92+
if (showErrorDialog != null) showErrorDialog.setValue(new NetworkResponse(msg));
9393
}
9494

9595
public void showMessage(String msg) {
96-
showMessage.setValue(msg==null?null:new InfoMessage(msg));
96+
if (showMessage != null) showMessage.setValue(msg == null ? null : new InfoMessage(msg));
9797
}
9898

9999
public void showMessageDialog(String msg, DialogInterface.OnClickListener listener) {
100-
showMessage.setValue(msg==null?null:new InfoMessage(msg,0,listener));
100+
if (showMessage != null)
101+
showMessage.setValue(msg == null ? null : new InfoMessage(msg, 0, listener));
101102
}
102103

103104
public void showMessage(int msg) {
104-
showMessage.setValue(new InfoMessage(msg));
105+
if (showMessage != null) showMessage.setValue(new InfoMessage(msg));
105106
}
106107

107-
public BaseEvents getNetworkCallback(){
108+
public BaseEvents getNetworkCallback() {
108109
return baseEvents;
109110
}
110111

@@ -123,32 +124,34 @@ public void addCallback(MainCallback callback) {
123124

124125
@Override
125126
public void hideProgressBar() {
126-
showProgressBar.setValue(false);
127+
if (showProgressBar != null) showProgressBar.setValue(false);
127128
}
128129

129130
@Override
130131
public void hideProgressDialog() {
131-
showProgressDialog.postValue(null);
132+
if (showProgressDialog != null) showProgressDialog.postValue(null);
132133
}
133134

134135
@Override
135136
public void showProgressBar() {
136-
showProgressBar.setValue(true);
137+
if (showProgressBar != null) showProgressBar.setValue(true);
137138
}
138139

139140
@Override
140141
public void showProgressDialog(String loadingMsg, DialogInterface.OnCancelListener onCancelListener) {
141-
showProgressDialog.setValue(new ProgressData(loadingMsg, onCancelListener));
142+
if (showProgressDialog != null) {
143+
showProgressDialog.setValue(new ProgressData(loadingMsg, onCancelListener));
144+
}
142145
}
143146

144147
@Override
145148
public void showNoNetwork() {
146-
showNoNetwork.setValue(true);
149+
if (showNoNetwork != null) showNoNetwork.setValue(true);
147150
}
148151

149152
@Override
150153
public void showError(String msg, NetworkResponse networkResponse) {
151-
showError.setValue(networkResponse);
154+
if (showError != null) showError.setValue(networkResponse);
152155
}
153156
};
154157

@@ -157,7 +160,7 @@ protected void onCleared() {
157160
super.onCleared();
158161
//stop all background processes
159162
for (MainCallback callback : callbacks) {
160-
if(callback!=null){
163+
if (callback != null) {
161164
callback.cancelBackgroundOperation();
162165
LogUtils.d(this.getClass().getSimpleName(), "cancelBackgroundOperation");
163166
}
@@ -168,7 +171,7 @@ protected void onCleared() {
168171
public boolean isSameCallRunning(MainCallback callbackToRun) {
169172
//check if there is no running call already
170173
for (MainCallback callback : callbacks) {
171-
if(callbackToRun.getClass().isInstance(callback)) return true;
174+
if (callbackToRun.getClass().isInstance(callback)) return true;
172175
}
173176
return false;
174177
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.futurist_labs.android.base_library.utils;
2+
3+
import java.lang.reflect.Field;
4+
5+
/**
6+
* Created by Galeen on 8/21/2019.
7+
*/
8+
public class CloneUtil {
9+
// TODO: 8/23/2019 check transient fields
10+
public static <T> T clone(T t) {
11+
Class<?> clazzRoot = t.getClass();
12+
13+
Object newInstance = null;
14+
try {
15+
newInstance = clazzRoot.newInstance();
16+
Field[] fieldsClone = newInstance.getClass().getDeclaredFields();
17+
for (Field fieldClone : fieldsClone) {
18+
fieldClone.setAccessible(true);
19+
fieldClone.set(newInstance, getContent(t, fieldClone.getName()));
20+
}
21+
} catch (IllegalAccessException e) {
22+
e.printStackTrace();
23+
return null;
24+
} catch (InstantiationException e) {
25+
e.printStackTrace();
26+
return null;
27+
} catch (NoSuchFieldException e) {
28+
e.printStackTrace();
29+
return null;
30+
}
31+
return (T) newInstance;
32+
}
33+
34+
private static Object getContent(Object aClass, String name) throws NoSuchFieldException, IllegalArgumentException,
35+
IllegalAccessException {
36+
Field declaredField = aClass.getClass().getDeclaredField(name);
37+
declaredField.setAccessible(true);
38+
return declaredField.get(aClass);
39+
}
40+
}

core_library/src/main/java/com/futurist_labs/android/base_library/utils/test/TestObjectExcludes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class TestObjectExcludes {
77
private int position;
88
private String id;
9-
@CoreAnnotations.ExcludeFromTests
9+
@Tester.ExcludeFromTests
1010
private Long time;
1111
private boolean isFree;
1212

core_library/src/main/java/com/futurist_labs/android/base_library/utils/test/TestObjectMandatory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66
public class TestObjectMandatory {
77
private int position;
8-
@CoreAnnotations.MandatoryForTests
9-
@CoreAnnotations.CheckEmptyStringTests
8+
@Tester.MandatoryForTests
9+
@Tester.CheckEmptyStringTests
1010
private String id;
1111
private Long time;
1212
private boolean isFree;

core_library/src/main/java/com/futurist_labs/android/base_library/utils/test/TestObjectPrimitives.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* Created by Galeen on 2019-08-13.
55
*/
66
public class TestObjectPrimitives {
7-
@CoreAnnotations.CheckDefaultPrimitiveInTests
7+
@Tester.CheckDefaultPrimitiveInTests
88
private int position;
99
private String id;
1010
private Long time;
11-
@CoreAnnotations.CheckDefaultPrimitiveInTests
11+
@Tester.CheckDefaultPrimitiveInTests
1212
private boolean isFree;
1313

1414
public TestObjectPrimitives(int position, String id, Long time, boolean isFree) {

core_library/src/main/java/com/futurist_labs/android/base_library/utils/test/CoreAnnotations.java renamed to core_library/src/main/java/com/futurist_labs/android/base_library/utils/test/Tester.java

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
11
package com.futurist_labs.android.base_library.utils.test;
22

3+
import com.futurist_labs.android.base_library.utils.Texter;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
39
import java.lang.annotation.Retention;
410
import java.lang.annotation.RetentionPolicy;
511
import java.lang.reflect.Field;
612
import java.util.ArrayList;
713
import java.util.Arrays;
14+
import java.util.HashSet;
815
import java.util.List;
16+
import java.util.Set;
917

1018
/**
1119
* Created by Galeen on 2019-08-13.
1220
* Helper class to test an object for NULL/Empty fields.
1321
* This way you can easily test server response for mandatory fields that are null or if a field
1422
* is removed from the server response.
15-
*
16-
* Todo check for extra fields added form the server
23+
* <p>
24+
* Todo check for extra fields added form the server, check transient fields
25+
* https://stackoverflow.com/questions/19551882/jsr-303-compatible-bean-validator-for-android
1726
*/
18-
public class CoreAnnotations {
27+
public class Tester {
1928

2029
/**
2130
* Fields with this annotation must be checked
2231
*/
2332
@Retention(RetentionPolicy.RUNTIME)
2433
public @interface MandatoryForTests {
2534
}
35+
2636
/**
2737
* Fields with this annotation must not be checked
2838
*/
2939
@Retention(RetentionPolicy.RUNTIME)
3040
public @interface ExcludeFromTests {
3141
}
42+
3243
/**
3344
* Fields with this annotation will be checked if are primitive type and if is default value
3445
*/
3546
@Retention(RetentionPolicy.RUNTIME)
3647
public @interface CheckDefaultPrimitiveInTests {
3748
}
49+
3850
/**
3951
* String Fields with this annotation will be checked for empty string also String.length == 0
4052
*/
@@ -43,26 +55,20 @@ public class CoreAnnotations {
4355
}
4456

4557
/**
46-
*
47-
* @param t Object to test
58+
* @param t Object to test
4859
* @param fieldsWithoutAnnotationAreMandatory if true any field that has no annotation @ExcludeFromTests
4960
* will be treated as with @MandatoryForTests, else only fields that has
50-
* @MandatoryForTests annotation will be checked
5161
* @return Null if all is good , else will be a string where each line represents the field and msg if is NULL , 0 or Empty
62+
* @MandatoryForTests annotation will be checked
5263
*/
5364
public static <T> String getNullFields(T t, boolean fieldsWithoutAnnotationAreMandatory) {
54-
List<Field> fields = new ArrayList<>();
55-
Class clazz = t.getClass();
56-
while (clazz != null && clazz != Object.class) {
57-
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
58-
clazz = clazz.getSuperclass();
59-
}
65+
List<Field> fields = getAllFields(t);
6066
int size = fields.size();
6167
StringBuilder sb = new StringBuilder();
6268
for (int i = 0; i < size; i++) {
6369
Field field = fields.get(i);
6470
if (!field.isAnnotationPresent(ExcludeFromTests.class) &&
65-
(fieldsWithoutAnnotationAreMandatory || field.isAnnotationPresent(MandatoryForTests.class))) {
71+
(fieldsWithoutAnnotationAreMandatory || field.isAnnotationPresent(MandatoryForTests.class))) {
6672
field.setAccessible(true);
6773
try {
6874
if (field.get(t) == null) {
@@ -87,6 +93,38 @@ public static <T> String getNullFields(T t, boolean fieldsWithoutAnnotationAreMa
8793
return sb.length() > 0 ? sb.toString() : null;
8894
}
8995

96+
private static <T> List<Field> getAllFields(T t) {
97+
List<Field> fields = new ArrayList<>();
98+
Class clazz = t.getClass();
99+
while (clazz != null && clazz != Object.class) {
100+
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
101+
clazz = clazz.getSuperclass();
102+
}
103+
return fields;
104+
}
105+
106+
public static <T> Set<String> getExtraFields(T t, String jsonObjectString,
107+
boolean fieldsWithoutAnnotationAreMandatory) throws JSONException {
108+
if (t == null || Texter.isEmpty(jsonObjectString)) {
109+
return null;
110+
}
111+
Set<String> errors = null;
112+
JSONObject jsonObject = new JSONObject(jsonObjectString);
113+
JSONArray names = jsonObject.names();
114+
int size = names == null ? 0 : names.length();
115+
if(size > 0) {
116+
List<Field> fields = getAllFields(t);
117+
for (int i = 0; i < size; i++) {
118+
String name = names.getString(i);
119+
// TODO: 8/20/2019 check if name is field
120+
}
121+
}else{
122+
errors = new HashSet<>();
123+
errors.add("Empty json");
124+
}
125+
return errors;
126+
}
127+
90128
private static <T> void checkDefaultPrimitive(final T t, final StringBuilder sb, final Field field) throws IllegalAccessException {
91129
Object o = field.get(t);
92130
// any number type is fine.

0 commit comments

Comments
 (0)