-
Notifications
You must be signed in to change notification settings - Fork 108
/
AppSyncConflictUnhandledErrorTest.java
194 lines (171 loc) · 6.62 KB
/
AppSyncConflictUnhandledErrorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amplifyframework.datastore.appsync;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.amplifyframework.api.graphql.GraphQLResponse;
import com.amplifyframework.core.model.Model;
import com.amplifyframework.core.model.temporal.Temporal;
import com.amplifyframework.testmodels.commentsblog.BlogOwner;
import com.amplifyframework.testutils.Resources;
import com.amplifyframework.util.GsonFactory;
import com.amplifyframework.util.TypeMaker;
import com.google.gson.Gson;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* Tests the {@link AppSyncConflictUnhandledError} class.
*/
public final class AppSyncConflictUnhandledErrorTest {
private Gson gson;
/**
* Obtain a Gson instance to use while arranging cooked GraphQL responses.
*/
@Before
public void setup() {
this.gson = GsonFactory.instance();
}
/**
* When a null error list is provided, the {@link AppSyncConflictUnhandledError#findFirst(Class, List)}
* factory should return null, meaning that there was no conflict error found in the
* provided list.
*/
@Test
public void noConflictErrorFoundInNullErrorList() {
AppSyncConflictUnhandledError<BlogOwner> conflictUnhandledError =
AppSyncConflictUnhandledError.findFirst(BlogOwner.class, null);
assertNull(conflictUnhandledError);
}
/**
* When an empty list is provided, the {@link AppSyncConflictUnhandledError#findFirst(Class, List)}
* factory should return null, indicating that there was no conflict error found.
*/
@Test
public void noConflictErrorFoundInEmptyErrorList() {
AppSyncConflictUnhandledError<BlogOwner> conflictUnhandledError =
AppSyncConflictUnhandledError.findFirst(BlogOwner.class, Collections.emptyList());
assertNull(conflictUnhandledError);
}
/**
* When a non-empty list is provided to the {@link AppSyncConflictUnhandledError#findFirst(Class, List)}
* factory, but there are no relevant errors in the list, then null should be returned,
* indicating that no conflict error was found in the list.
*/
@Test
public void noConflictErrorFoundInUnrelatedErrorsList() {
List<GraphQLResponse.Error> unrelatedErrors = Arrays.asList(
new GraphQLResponse.Error(
"unrelated1",
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyMap()
),
new GraphQLResponse.Error(
"unrelated2",
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyMap()
)
);
AppSyncConflictUnhandledError<BlogOwner> conflictUnhandledError =
AppSyncConflictUnhandledError.findFirst(BlogOwner.class, unrelatedErrors);
assertNull(conflictUnhandledError);
}
/**
* When a conflict error *is* present in the list provided to
* {@link AppSyncConflictUnhandledError#findFirst(Class, List)},
* then a model for it should be returned from the factory method.
* That model should contain representation of the serer version of
* the model that is currently in conflict.
*/
@Test
public void conflictErrorExtractedIfPresent() {
String responseJson = Resources.readAsString("conflict-unhandled-response.json");
Type type = TypeMaker.getParameterizedType(GraphQLResponse.class, ModelWithMetadata.class, Note.class);
GraphQLResponse<ModelWithMetadata<Note>> response = gson.fromJson(responseJson, type);
AppSyncConflictUnhandledError<Note> conflictUnhandledError =
AppSyncConflictUnhandledError.findFirst(Note.class, response.getErrors());
assertNotNull(conflictUnhandledError);
// TODO: the TimeUnit appears to be wrong. Should be MILLISECONDS?
Temporal.Timestamp lastChangedAt = new Temporal.Timestamp(1601499066604L, TimeUnit.SECONDS);
assertEquals(
new ModelWithMetadata<>(
new Note("KoolId22", "Resurecting the dataz"),
new ModelMetadata("KoolId22", true, 7, lastChangedAt)
),
conflictUnhandledError.getServerVersion()
);
}
/**
* This is a model for the data that is contained in
* src/test/resources/conflict-unhandled-response.json.
*/
@SuppressWarnings("checkstyle:ParameterName")
static final class Note implements Model {
private final String id;
private final String content;
Note(String id, String content) {
this.id = id;
this.content = content;
}
public String getId() {
return id;
}
@NonNull
@Override
public String resolveIdentifier() {
return id;
}
@NonNull
String getContent() {
return content;
}
@Override
public boolean equals(@Nullable Object thatObject) {
if (this == thatObject) {
return true;
}
if (thatObject == null || getClass() != thatObject.getClass()) {
return false;
}
Note note = (Note) thatObject;
if (!resolveIdentifier().equals(note.resolveIdentifier())) {
return false;
}
return getContent().equals(note.getContent());
}
@Override
public int hashCode() {
int result = resolveIdentifier().hashCode();
result = 31 * result + getContent().hashCode();
return result;
}
@Override
public String toString() {
return "Note{" +
"id='" + id + '\'' +
", content='" + content + '\'' +
'}';
}
}
}