|
2 | 2 |
|
3 | 3 | import androidx.test.core.app.ApplicationProvider; |
4 | 4 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 5 | +import java.util.Arrays; |
5 | 6 | import java.util.HashMap; |
| 7 | +import java.util.List; |
6 | 8 | import java.util.Map; |
7 | 9 | import java.util.Random; |
8 | 10 | import java.util.concurrent.ConcurrentHashMap; |
| 11 | +import org.json.JSONArray; |
9 | 12 | import org.json.JSONException; |
10 | 13 | import org.json.JSONObject; |
11 | 14 | import org.junit.After; |
12 | 15 | import org.junit.Assert; |
13 | 16 | import org.junit.Before; |
14 | 17 | import org.junit.Test; |
15 | 18 | import org.junit.runner.RunWith; |
| 19 | +import org.mockito.Mockito; |
| 20 | +import org.mockito.internal.util.collections.Sets; |
16 | 21 |
|
17 | 22 | @RunWith(AndroidJUnit4.class) |
18 | 23 | public class ModuleUserProfileTests { |
@@ -693,6 +698,193 @@ public void setUserProperties_null() throws JSONException { |
693 | 698 | validateUserProfileRequest(new HashMap<>(), new HashMap<>()); |
694 | 699 | } |
695 | 700 |
|
| 701 | + /** |
| 702 | + * "setProperties" with Array properties |
| 703 | + * Validate that all primitive types arrays are successfully recorded |
| 704 | + * And validate that Object arrays are not recorded |
| 705 | + * But Generic type of Object array which its values are only primitive types are recorded |
| 706 | + * |
| 707 | + * @throws JSONException if the JSON is not valid |
| 708 | + */ |
| 709 | + @Test |
| 710 | + public void setProperties_validateSupportedArrays() throws JSONException { |
| 711 | + int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 712 | + boolean[] arrB = { true, false, true, false, true, false, true, false, true, false }; |
| 713 | + String[] arrS = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; |
| 714 | + long[] arrL = { Long.MAX_VALUE, Long.MIN_VALUE }; |
| 715 | + double[] arrD = { Double.MAX_VALUE, Double.MIN_VALUE }; |
| 716 | + Long[] arrLO = { Long.MAX_VALUE, Long.MIN_VALUE }; |
| 717 | + Double[] arrDO = { Double.MAX_VALUE, Double.MIN_VALUE }; |
| 718 | + Boolean[] arrBO = { Boolean.TRUE, Boolean.FALSE }; |
| 719 | + Integer[] arrIO = { Integer.MAX_VALUE, Integer.MIN_VALUE }; |
| 720 | + Object[] arrObj = { "1", 1, 1.1d, true, 1.1f, Long.MAX_VALUE }; |
| 721 | + Object[] arrObjStr = { "1", "1", "1.1d", "true", "1.1f", "Long.MAX_VALUE" }; |
| 722 | + |
| 723 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 724 | + Countly countly = new Countly().init(countlyConfig); |
| 725 | + |
| 726 | + Map<String, Object> userProperties = TestUtils.map( |
| 727 | + "arr", arr, |
| 728 | + "arrB", arrB, |
| 729 | + "arrS", arrS, |
| 730 | + "arrL", arrL, |
| 731 | + "arrD", arrD, |
| 732 | + "arrLO", arrLO, |
| 733 | + "arrDO", arrDO, |
| 734 | + "arrBO", arrBO, |
| 735 | + "arrIO", arrIO, |
| 736 | + "arrObj", arrObj, |
| 737 | + "arrObjStr", arrObjStr |
| 738 | + ); |
| 739 | + |
| 740 | + countly.userProfile().setProperties(userProperties); |
| 741 | + countly.userProfile().save(); |
| 742 | + |
| 743 | + Map<String, Object> expectedCustomProperties = TestUtils.map( |
| 744 | + "arr", new JSONArray(arr), |
| 745 | + "arrB", new JSONArray(arrB), |
| 746 | + "arrS", new JSONArray(arrS), |
| 747 | + "arrL", new JSONArray(arrL), |
| 748 | + "arrD", new JSONArray(arrD), |
| 749 | + "arrLO", new JSONArray(arrLO), |
| 750 | + "arrDO", new JSONArray(arrDO), |
| 751 | + "arrBO", new JSONArray(arrBO), |
| 752 | + "arrIO", new JSONArray(arrIO) |
| 753 | + ); |
| 754 | + |
| 755 | + validateUserProfileRequest(new HashMap<>(), expectedCustomProperties); |
| 756 | + } |
| 757 | + |
| 758 | + /** |
| 759 | + * "setProperties" with List properties |
| 760 | + * Validate that all primitive types Lists are successfully recorded |
| 761 | + * And validate that List of Objects is not recorded |
| 762 | + * But Generic type of Object list which its values are only primitive types are recorded |
| 763 | + * |
| 764 | + * @throws JSONException if the JSON is not valid |
| 765 | + */ |
| 766 | + @Test |
| 767 | + public void setProperties_validateSupportedLists() throws JSONException { |
| 768 | + List<Integer> arr = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 769 | + List<Boolean> arrB = Arrays.asList(true, false, true, false, true, false, true, false, true, false); |
| 770 | + List<String> arrS = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); |
| 771 | + List<Long> arrLO = Arrays.asList(Long.MAX_VALUE, Long.MIN_VALUE); |
| 772 | + List<Double> arrDO = Arrays.asList(Double.MAX_VALUE, Double.MIN_VALUE); |
| 773 | + List<Boolean> arrBO = Arrays.asList(Boolean.TRUE, Boolean.FALSE); |
| 774 | + List<Integer> arrIO = Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE); |
| 775 | + List<Object> arrObj = Arrays.asList("1", 1, 1.1d, true, Long.MAX_VALUE); |
| 776 | + List<Object> arrObjStr = Arrays.asList("1", "1", "1.1d", "true", "Long.MAX_VALUE"); |
| 777 | + |
| 778 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 779 | + Countly countly = new Countly().init(countlyConfig); |
| 780 | + |
| 781 | + Map<String, Object> userProperties = TestUtils.map( |
| 782 | + "arr", arr, |
| 783 | + "arrB", arrB, |
| 784 | + "arrS", arrS, |
| 785 | + "arrLO", arrLO, |
| 786 | + "arrDO", arrDO, |
| 787 | + "arrBO", arrBO, |
| 788 | + "arrIO", arrIO, |
| 789 | + "arrObj", arrObj, |
| 790 | + "arrObjStr", arrObjStr |
| 791 | + ); |
| 792 | + |
| 793 | + countly.userProfile().setProperties(userProperties); |
| 794 | + countly.userProfile().save(); |
| 795 | + |
| 796 | + Map<String, Object> expectedCustomProperties = TestUtils.map( |
| 797 | + "arr", new JSONArray(arr), |
| 798 | + "arrB", new JSONArray(arrB), |
| 799 | + "arrS", new JSONArray(arrS), |
| 800 | + "arrLO", new JSONArray(arrLO), |
| 801 | + "arrDO", new JSONArray(arrDO), |
| 802 | + "arrBO", new JSONArray(arrBO), |
| 803 | + "arrIO", new JSONArray(arrIO), |
| 804 | + "arrObjStr", new JSONArray(arrObjStr), |
| 805 | + "arrObj", new JSONArray(arrObj) |
| 806 | + ); |
| 807 | + |
| 808 | + validateUserProfileRequest(new HashMap<>(), expectedCustomProperties); |
| 809 | + } |
| 810 | + |
| 811 | + /** |
| 812 | + * "setProperties" with JSONArray properties |
| 813 | + * Validate that all primitive types JSONArrays are successfully recorded |
| 814 | + * And validate and JSONArray of Objects is not recorded |
| 815 | + * |
| 816 | + * @throws JSONException if the JSON is not valid |
| 817 | + */ |
| 818 | + @Test |
| 819 | + public void setProperties_validateSupportedJSONArrays() throws JSONException { |
| 820 | + JSONArray arr = new JSONArray(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
| 821 | + JSONArray arrB = new JSONArray(Arrays.asList(true, false, true, false, true, false, true, false, true, false)); |
| 822 | + JSONArray arrS = new JSONArray(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")); |
| 823 | + JSONArray arrL = new JSONArray(Arrays.asList(Long.MAX_VALUE, Long.MIN_VALUE)); |
| 824 | + JSONArray arrD = new JSONArray(Arrays.asList(Double.MAX_VALUE, Double.MIN_VALUE)); |
| 825 | + JSONArray arrBO = new JSONArray(Arrays.asList(Boolean.TRUE, Boolean.FALSE)); |
| 826 | + JSONArray arrIO = new JSONArray(Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE)); |
| 827 | + JSONArray arrObj = new JSONArray(Arrays.asList("1", 1, 1.1d, true, Long.MAX_VALUE)); |
| 828 | + |
| 829 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 830 | + Countly countly = new Countly().init(countlyConfig); |
| 831 | + |
| 832 | + Map<String, Object> userProperties = TestUtils.map( |
| 833 | + "arr", arr, |
| 834 | + "arrB", arrB, |
| 835 | + "arrS", arrS, |
| 836 | + "arrL", arrL, |
| 837 | + "arrD", arrD, |
| 838 | + "arrBO", arrBO, |
| 839 | + "arrIO", arrIO, |
| 840 | + "arrObj", arrObj |
| 841 | + ); |
| 842 | + |
| 843 | + // Record event with the created segmentation |
| 844 | + countly.userProfile().setProperties(userProperties); |
| 845 | + countly.userProfile().save(); |
| 846 | + |
| 847 | + // Prepare expected segmentation with JSONArrays |
| 848 | + Map<String, Object> expectedCustomProperties = TestUtils.map( |
| 849 | + "arr", arr, |
| 850 | + "arrB", arrB, |
| 851 | + "arrS", arrS, |
| 852 | + "arrL", arrL, |
| 853 | + "arrD", arrD, |
| 854 | + "arrBO", arrBO, |
| 855 | + "arrIO", arrIO, |
| 856 | + "arrObj", arrObj |
| 857 | + ); |
| 858 | + |
| 859 | + validateUserProfileRequest(new HashMap<>(), expectedCustomProperties); |
| 860 | + } |
| 861 | + |
| 862 | + /** |
| 863 | + * "setProperties" with invalid data types |
| 864 | + * Validate that unsupported data types are not recorded |
| 865 | + * |
| 866 | + * @throws JSONException if the JSON is not valid |
| 867 | + */ |
| 868 | + @Test |
| 869 | + public void setProperties_unsupportedDataTypes() throws JSONException { |
| 870 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 871 | + countlyConfig.setEventQueueSizeToSend(1); |
| 872 | + Countly countly = new Countly().init(countlyConfig); |
| 873 | + |
| 874 | + Map<String, Object> userProperties = TestUtils.map( |
| 875 | + "a", TestUtils.map(), |
| 876 | + "b", TestUtils.json(), |
| 877 | + "c", new Object(), |
| 878 | + "d", Sets.newSet(), |
| 879 | + "e", Mockito.mock(ModuleLog.class) |
| 880 | + ); |
| 881 | + |
| 882 | + countly.userProfile().setProperties(userProperties); |
| 883 | + countly.userProfile().save(); |
| 884 | + |
| 885 | + validateUserProfileRequest(new HashMap<>(), new HashMap<>()); |
| 886 | + } |
| 887 | + |
696 | 888 | protected static void validateUserProfileRequest(String deviceId, int idx, int size, Map<String, Object> predefined, Map<String, Object> custom) throws JSONException { |
697 | 889 | Map<String, String>[] RQ = TestUtils.getCurrentRQ(); |
698 | 890 | Assert.assertEquals(size, RQ.length); |
|
0 commit comments