|
20 | 20 | import org.junit.rules.ExpectedException; |
21 | 21 | import org.junit.runner.RunWith; |
22 | 22 | import org.robolectric.RobolectricTestRunner; |
| 23 | +import org.robolectric.RuntimeEnvironment; |
23 | 24 | import org.robolectric.annotation.Config; |
24 | 25 |
|
| 26 | +import java.net.URL; |
25 | 27 | import java.util.ArrayList; |
26 | 28 | import java.util.Arrays; |
27 | 29 | import java.util.Collections; |
@@ -64,6 +66,7 @@ public void setUp() { |
64 | 66 | @After |
65 | 67 | public void tearDown() { |
66 | 68 | ParseCorePlugins.getInstance().reset(); |
| 69 | + ParsePlugins.reset(); |
67 | 70 | } |
68 | 71 |
|
69 | 72 | @Test |
@@ -761,6 +764,82 @@ public void testParcelWhileDeletingWithLDSEnabled() throws Exception { |
761 | 764 |
|
762 | 765 | //endregion |
763 | 766 |
|
| 767 | + //region testFailingDelete |
| 768 | + |
| 769 | + @Test |
| 770 | + public void testFailingDelete() throws Exception { |
| 771 | + |
| 772 | + ParseRESTCommand.server = new URL("https://api.parse.com/1"); |
| 773 | + |
| 774 | + Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application) |
| 775 | + .build(); |
| 776 | + ParsePlugins plugins = mock(ParsePlugins.class); |
| 777 | + when(plugins.configuration()).thenReturn(configuration); |
| 778 | + when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); |
| 779 | + ParsePlugins.set(plugins); |
| 780 | + |
| 781 | + JSONObject mockResponse = new JSONObject(); |
| 782 | + mockResponse.put("code", 141); |
| 783 | + mockResponse.put("error", "Delete is not allowed"); |
| 784 | + ParseHttpClient restClient = |
| 785 | + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); |
| 786 | + when(plugins.restClient()).thenReturn(restClient); |
| 787 | + |
| 788 | + ParseObject.State state = mock(ParseObject.State.class); |
| 789 | + when(state.className()).thenReturn("TestObject"); |
| 790 | + when(state.objectId()).thenReturn("test_id"); |
| 791 | + when(state.keySet()).thenReturn(Collections.singleton("key")); |
| 792 | + when(state.get("key")).thenReturn("data"); |
| 793 | + ParseObject object = ParseObject.from(state); |
| 794 | + |
| 795 | + thrown.expect(ParseException.class); |
| 796 | + thrown.expectMessage("Delete is not allowed"); |
| 797 | + |
| 798 | + object.delete(); |
| 799 | + } |
| 800 | + |
| 801 | + //endregion |
| 802 | + |
| 803 | + //region testFailingSave |
| 804 | + |
| 805 | + @Test |
| 806 | + public void testFailingSave() throws Exception { |
| 807 | + |
| 808 | + ParseRESTCommand.server = new URL("https://api.parse.com/1"); |
| 809 | + |
| 810 | + ParseObject.registerSubclass(ParseUser.class); |
| 811 | + |
| 812 | + Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application) |
| 813 | + .build(); |
| 814 | + ParsePlugins plugins = mock(ParsePlugins.class); |
| 815 | + when(plugins.configuration()).thenReturn(configuration); |
| 816 | + when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); |
| 817 | + ParsePlugins.set(plugins); |
| 818 | + |
| 819 | + JSONObject mockResponse = new JSONObject(); |
| 820 | + mockResponse.put("code", 141); |
| 821 | + mockResponse.put("error", "Save is not allowed"); |
| 822 | + ParseHttpClient restClient = |
| 823 | + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); |
| 824 | + when(plugins.restClient()).thenReturn(restClient); |
| 825 | + |
| 826 | + ParseObject.State state = mock(ParseObject.State.class); |
| 827 | + when(state.className()).thenReturn("TestObject"); |
| 828 | + when(state.objectId()).thenReturn("test_id"); |
| 829 | + when(state.keySet()).thenReturn(Collections.singleton("key")); |
| 830 | + when(state.get("key")).thenReturn("data"); |
| 831 | + ParseObject object = ParseObject.from(state); |
| 832 | + |
| 833 | + object.put("key", "other data"); |
| 834 | + |
| 835 | + thrown.expect(ParseException.class); |
| 836 | + thrown.expectMessage("Save is not allowed"); |
| 837 | + |
| 838 | + object.save(); |
| 839 | + } |
| 840 | + |
| 841 | + //endregion |
| 842 | + |
764 | 843 | private static void mockCurrentUserController() { |
765 | 844 | ParseCurrentUserController userController = mock(ParseCurrentUserController.class); |
766 | 845 | when(userController.getCurrentSessionTokenAsync()).thenReturn(Task.forResult("token")); |
|
0 commit comments