|
| 1 | +package io.appwrite; |
| 2 | + |
| 3 | +import androidx.test.core.app.ApplicationProvider; |
| 4 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 5 | + |
| 6 | +import com.google.gson.Gson; |
| 7 | + |
| 8 | +import io.appwrite.exceptions.AppwriteException; |
| 9 | +import io.appwrite.Permission; |
| 10 | +import io.appwrite.Role; |
| 11 | +import io.appwrite.ID; |
| 12 | +import io.appwrite.Query; |
| 13 | +import io.appwrite.enums.MockType; |
| 14 | +import io.appwrite.extensions.GsonExtensionsKt; |
| 15 | +import io.appwrite.models.Error; |
| 16 | +import io.appwrite.models.InputFile; |
| 17 | +import io.appwrite.models.Mock; |
| 18 | +import io.appwrite.services.Bar; |
| 19 | +import io.appwrite.services.Foo; |
| 20 | +import io.appwrite.services.General; |
| 21 | +import io.appwrite.services.Realtime; |
| 22 | + |
| 23 | +import kotlinx.coroutines.Dispatchers; |
| 24 | +import kotlinx.coroutines.ExperimentalCoroutinesApi; |
| 25 | +import kotlinx.coroutines.delay; |
| 26 | +import kotlinx.coroutines.runBlocking; |
| 27 | +import kotlinx.coroutines.test.TestCoroutineDispatcher; |
| 28 | +import kotlinx.coroutines.test.TestCoroutineScope; |
| 29 | + |
| 30 | +import okhttp3.Response; |
| 31 | + |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.robolectric.annotation.Config; |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.io.IOException; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.nio.file.Path; |
| 42 | +import java.nio.file.Paths; |
| 43 | +import java.nio.file.StandardOpenOption; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +import static kotlinx.coroutines.test.TestCoroutineScopeKt.*; |
| 48 | + |
| 49 | +class TestPayload { |
| 50 | + private String response; |
| 51 | + |
| 52 | + public String getResponse() { |
| 53 | + return response; |
| 54 | + } |
| 55 | + |
| 56 | + public void setResponse(String response) { |
| 57 | + this.response = response; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +@Config(manifest = Config.NONE) |
| 62 | +@RunWith(AndroidJUnit4.class) |
| 63 | +public class ServiceTest { |
| 64 | + |
| 65 | + private final String filename = "result.txt"; |
| 66 | + |
| 67 | + @Before |
| 68 | + @ExperimentalCoroutinesApi |
| 69 | + public void setUp() throws IOException { |
| 70 | + Dispatchers.setMain(new TestCoroutineDispatcher()); |
| 71 | + Files.deleteIfExists(Paths.get(filename)); |
| 72 | + writeToFile("Test Started"); |
| 73 | + } |
| 74 | + |
| 75 | + @After |
| 76 | + @ExperimentalCoroutinesApi |
| 77 | + public void tearDown() { |
| 78 | + Dispatchers.resetMain(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @Throws(IOException.class) |
| 83 | + public void test() throws IOException { |
| 84 | + Client client = new Client(ApplicationProvider.getApplicationContext()) |
| 85 | + .setEndpointRealtime("wss://demo.appwrite.io/v1") |
| 86 | + .setProject("console") |
| 87 | + .addHeader("Origin", "http://localhost") |
| 88 | + .setSelfSigned(true); |
| 89 | + |
| 90 | + Foo foo = new Foo(client); |
| 91 | + Bar bar = new Bar(client); |
| 92 | + General general = new General(client); |
| 93 | + Realtime realtime = new Realtime(client); |
| 94 | + String realtimeResponse = "Realtime failed!"; |
| 95 | + |
| 96 | + realtime.subscribe("tests", TestPayload.class, payload -> { |
| 97 | + realtimeResponse = payload.getResponse(); |
| 98 | + return null; |
| 99 | + }); |
| 100 | + |
| 101 | + runBlocking(new TestCoroutineScope(), () -> { |
| 102 | + Mock mock; |
| 103 | + // Foo Tests |
| 104 | + mock = foo.get("string", 123, List.of("string in array")); |
| 105 | + writeToFile(mock.getResult()); |
| 106 | + mock = foo.post("string", 123, List.of("string in array")); |
| 107 | + writeToFile(mock.getResult()); |
| 108 | + mock = foo.put("string", 123, List.of("string in array")); |
| 109 | + writeToFile(mock.getResult()); |
| 110 | + mock = foo.patch("string", 123, List.of("string in array")); |
| 111 | + writeToFile(mock.getResult()); |
| 112 | + mock = foo.delete("string", 123, List.of("string in array")); |
| 113 | + writeToFile(mock.getResult()); |
| 114 | + |
| 115 | + // Bar Tests |
| 116 | + mock = bar.get("string", 123, List.of("string in array")); |
| 117 | + writeToFile(mock.getResult()); |
| 118 | + mock = bar.post("string", 123, List.of("string in array")); |
| 119 | + writeToFile(mock.getResult()); |
| 120 | + mock = bar.put("string", 123, List.of("string in array")); |
| 121 | + writeToFile(mock.getResult()); |
| 122 | + mock = bar.patch("string", 123, List.of("string in array")); |
| 123 | + writeToFile(mock.getResult()); |
| 124 | + mock = bar.delete("string", 123, List.of("string in array")); |
| 125 | + writeToFile(mock.getResult()); |
| 126 | + |
| 127 | + // General Tests |
| 128 | + Object result = general.redirect(); |
| 129 | + writeToFile((String) ((Map<String, Object>) result).get("result")); |
| 130 | + |
| 131 | + try { |
| 132 | + mock = general.upload("string", 123, List.of("string in array"), InputFile.fromPath("../../../resources/file.png")); |
| 133 | + writeToFile(mock.getResult()); |
| 134 | + } catch (Exception ex) { |
| 135 | + writeToFile(ex.toString()); |
| 136 | + } |
| 137 | + |
| 138 | + try { |
| 139 | + mock = general.upload("string", 123, List.of("string in array"), InputFile.fromPath("../../../resources/large_file.mp4")); |
| 140 | + writeToFile(mock.getResult()); |
| 141 | + } catch (Exception ex) { |
| 142 | + writeToFile(ex.toString()); |
| 143 | + } |
| 144 | + |
| 145 | + try { |
| 146 | + byte[] bytes = Files.readAllBytes(Paths.get("../../../resources/file.png")); |
| 147 | + mock = general.upload("string", 123, List.of("string in array"), InputFile.fromBytes(bytes, "file.png", "image/png")); |
| 148 | + writeToFile(mock.getResult()); |
| 149 | + } catch (Exception ex) { |
| 150 | + writeToFile(ex.toString()); |
| 151 | + } |
| 152 | + |
| 153 | + try { |
| 154 | + byte[] bytes = Files.readAllBytes(Paths.get("../../../resources/large_file.mp4")); |
| 155 | + mock = general.upload("string", 123, List.of("string in array"), InputFile.fromBytes(bytes, "large_file.mp4", "video/mp4")); |
| 156 | + writeToFile(mock.getResult()); |
| 157 | + } catch (Exception ex) { |
| 158 | + writeToFile(ex.toString()); |
| 159 | + } |
| 160 | + |
| 161 | + mock = general.enum(MockType.FIRST); |
| 162 | + writeToFile(mock.getResult()); |
| 163 | + |
| 164 | + try { |
| 165 | + general.error400(); |
| 166 | + } catch (AppwriteException e) { |
| 167 | + writeToFile(e.getMessage()); |
| 168 | + } |
| 169 | + |
| 170 | + try { |
| 171 | + general.error500(); |
| 172 | + } catch (AppwriteException e) { |
| 173 | + writeToFile(e.getMessage()); |
| 174 | + } |
| 175 | + |
| 176 | + try { |
| 177 | + general.error502(); |
| 178 | + } catch (AppwriteException e) { |
| 179 | + writeToFile(e.getMessage()); |
| 180 | + } |
| 181 | + |
| 182 | + delay(5000); |
| 183 | + writeToFile(realtimeResponse); |
| 184 | + |
| 185 | + // mock = general.setCookie(); |
| 186 | + // writeToFile(mock.getResult()); |
| 187 | + |
| 188 | + // mock = general.getCookie(); |
| 189 | + // writeToFile(mock.getResult()); |
| 190 | + |
| 191 | + general.empty(); |
| 192 | + |
| 193 | + // Query helper tests |
| 194 | + writeToFile(Query.Companion.equal("released", List.of(true)).toString()); |
| 195 | + writeToFile(Query.Companion.equal("title", List.of("Spiderman", "Dr. Strange")).toString()); |
| 196 | + writeToFile(Query.Companion.notEqual("title", "Spiderman").toString()); |
| 197 | + writeToFile(Query.Companion.lessThan("releasedYear", 1990).toString()); |
| 198 | + writeToFile(Query.Companion.greaterThan("releasedYear", 1990).toString()); |
| 199 | + writeToFile(Query.Companion.search("name", "john").toString()); |
| 200 | + writeToFile(Query.Companion.isNull("name").toString()); |
| 201 | + writeToFile(Query.Companion.isNotNull("name").toString()); |
| 202 | + writeToFile(Query.Companion.between("age", 50, 100).toString()); |
| 203 | + writeToFile(Query.Companion.between("age", 50.5, 100.5).toString()); |
| 204 | + writeToFile(Query.Companion.between("name", "Anna", "Brad").toString()); |
| 205 | + writeToFile(Query.Companion.startsWith("name", "Ann").toString()); |
| 206 | + writeToFile(Query.Companion.endsWith("name", "nne").toString()); |
| 207 | + writeToFile(Query.Companion.select(List.of("name", "age")).toString()); |
| 208 | + writeToFile(Query.Companion.orderAsc("title").toString()); |
| 209 | + writeToFile(Query.Companion.orderDesc("title").toString()); |
| 210 | + writeToFile(Query.Companion.cursorAfter("my_movie_id").toString()); |
| 211 | + writeToFile(Query.Companion.cursorBefore("my_movie_id").toString()); |
| 212 | + writeToFile(Query.Companion.limit(50).toString()); |
| 213 | + writeToFile(Query.Companion.offset(20).toString()); |
| 214 | + writeToFile(Query.Companion.contains("title", List.of("Spider")).toString()); |
| 215 | + writeToFile(Query.Companion.contains("labels", List.of("first")).toString()); |
| 216 | + writeToFile(Query.Companion.or(List.of(Query.Companion.equal("released", List.of(true)), Query.Companion.lessThan("releasedYear", 1990))).toString()); |
| 217 | + writeToFile(Query.Companion.and(List.of(Query.Companion.equal("released", List.of(false)), Query.Companion.greaterThan("releasedYear", 2015))).toString()); |
| 218 | + |
| 219 | + // Permission & Roles helper tests |
| 220 | + writeToFile(Permission.Companion.read(Role.Companion.any()).toString()); |
| 221 | + writeToFile(Permission.Companion.write(Role.Companion.user(ID.custom("userid"))).toString()); |
| 222 | + writeToFile(Permission.Companion.create(Role.Companion.users()).toString()); |
| 223 | + writeToFile(Permission.Companion.update(Role.Companion.guests()).toString()); |
| 224 | + writeToFile(Permission.Companion.delete(Role.Companion.team("teamId", "owner")).toString()); |
| 225 | + writeToFile(Permission.Companion.delete(Role.Companion.team("teamId")).toString()); |
| 226 | + writeToFile(Permission.Companion.create(Role.Companion.member("memberId")).toString()); |
| 227 | + writeToFile(Permission.Companion.update(Role.Companion.users("verified")).toString()); |
| 228 | + writeToFile(Permission.Companion.update(Role.Companion.user(ID.custom("userid"), "unverified")).toString()); |
| 229 | + writeToFile(Permission.Companion.create(Role.Companion.label("admin")).toString()); |
| 230 | + |
| 231 | + // ID helper tests |
| 232 | + writeToFile(ID.unique().toString()); |
| 233 | + writeToFile(ID.custom("custom_id").toString()); |
| 234 | + |
| 235 | + mock = general.headers(); |
| 236 | + writeToFile(mock.getResult()); |
| 237 | + |
| 238 | + return null; |
| 239 | + }); |
| 240 | + } |
| 241 | + |
| 242 | + private void writeToFile(String string) { |
| 243 | + String text = (string != null ? string : "") + "\n"; |
| 244 | + try { |
| 245 | + Files.writeString( |
| 246 | + Paths.get(filename), |
| 247 | + text, |
| 248 | + StandardOpenOption.APPEND |
| 249 | + ); |
| 250 | + } catch (IOException e) { |
| 251 | + e.printStackTrace(); |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments