diff --git a/build.gradle b/build.gradle --- a/build.gradle +++ b/build.gradle @@ -130,7 +130,7 @@ violationRules { rule { limit { - minimum = 0.5 + minimum = 0.7 } } } diff --git a/src/main/java/net/wildfyre/http/Request.kt b/src/main/java/net/wildfyre/http/Request.kt --- a/src/main/java/net/wildfyre/http/Request.kt +++ b/src/main/java/net/wildfyre/http/Request.kt @@ -231,6 +231,15 @@ } /** + * Requests a raw response from the server, and returns it. + * @return The raw response from the server, wrapped in an InputStream. + */ + @Throws(IssueInTransferException::class) + fun getRaw(): InputStream { + return getInputStream(send()) + } + + /** * Calls [getJson] and performs a cast to [JsonObject], for convenience. */ fun getJsonObject(): JsonObject = getJson() as JsonObject diff --git a/src/main/java/net/wildfyre/posts/Draft.java b/src/main/java/net/wildfyre/posts/Draft.java --- a/src/main/java/net/wildfyre/posts/Draft.java +++ b/src/main/java/net/wildfyre/posts/Draft.java @@ -152,9 +152,8 @@ * @return This object itself, to allow method-chaining. */ public Draft save(){ - if(isOnlyLocal) saveFirstTime(); - else { saveAsEdit(); - isOnlyLocal = false;} + if (isOnlyLocal) saveFirstTime(); + else saveAsEdit(); return this; } @@ -169,7 +168,7 @@ this.update(json); area().cachedDraft(this); - + isOnlyLocal = false; } catch (IssueInTransferException e) { e.printStackTrace(); //TODO @@ -221,7 +220,7 @@ try { new Request(DELETE, "/areas/" + areaID + "/drafts/" + postID) .addToken(Internal.token()) - .getJson(); + .getRaw(); //Nothing to do } catch (IssueInTransferException e) { diff --git a/src/main/java/net/wildfyre/posts/PostData.java b/src/main/java/net/wildfyre/posts/PostData.java --- a/src/main/java/net/wildfyre/posts/PostData.java +++ b/src/main/java/net/wildfyre/posts/PostData.java @@ -106,7 +106,10 @@ isAnonymous = requireField(json, "anonym").asBoolean(); hasSubscribed = requireField(json, "subscribed").asBoolean(); created = ZonedDateTime.parse(requireField(json, "created").asString()); - isActive = requireField(json, "active").asBoolean(); + if(!json.get("active").isNull()) + isActive = requireField(json, "active").asBoolean(); + else + isActive = true; text = requireField(json, "text").asString(); //TODO: hotfix before T262 imageURL = optionalField(json, "image").orElse(Json.value("")).asString(); @@ -142,7 +145,8 @@ authorID = other.authorID; areaID = other.areaID; postID = other.postID; - comments = new ArrayList<>(comments); + comments = new ArrayList(); + comments.addAll(other.comments); } //endregion @@ -405,15 +409,18 @@ for(String s : additionalImages) images.add(s); - return new JsonObject() + JsonObject ret = new JsonObject() .add("anonym", isAnonymous) .add("subscribed", hasSubscribed) .add("created", created.format(DateTimeFormatter.ISO_DATE_TIME)) .add("text", text) - .add("image", imageURL) .add("additional_images", images); + if (imageURL != null && !imageURL.isEmpty()) + ret.add("image", imageURL); + return ret; } //endregion } + diff --git a/src/test/java/net/wildfyre/InternalTest.kt b/src/test/java/net/wildfyre/InternalTest.kt new file mode 100644 --- /dev/null +++ b/src/test/java/net/wildfyre/InternalTest.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2019 Wildfyre.net + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 net.wildfyre.api + +import net.wildfyre.api.WildFyre +import net.wildfyre.users.LoggedUser +import org.junit.Assert.* +import org.junit.Test +import java.util.* + +class InternalTest { + @Test(expected=NullPointerException::class) + fun setTokenNullTest() { + Internal.setToken(null) + } + + @Test(expected=IllegalArgumentException::class) + fun setTokenEmptyTest() { + Internal.setToken("") + } + + @Test() + fun cleanTest() { + Internal.clean() + } +} diff --git a/src/test/java/net/wildfyre/http/RequestTest.kt b/src/test/java/net/wildfyre/http/RequestTest.kt --- a/src/test/java/net/wildfyre/http/RequestTest.kt +++ b/src/test/java/net/wildfyre/http/RequestTest.kt @@ -90,7 +90,15 @@ .getJson().asObject() WildFyre.disconnect() - return WildFyre.connect(username, password) + assertFalse(WildFyre.isConnected()) + val u = WildFyre.connect(username, password) + assertTrue(WildFyre.isConnected()) + return u + } + + @Test( expected = NullPointerException::class ) + fun testConnect() { + WildFyre.connect(null) } @Test( timeout = 5000L ) diff --git a/src/test/java/net/wildfyre/posts/PostTest.java b/src/test/java/net/wildfyre/posts/PostTest.java --- a/src/test/java/net/wildfyre/posts/PostTest.java +++ b/src/test/java/net/wildfyre/posts/PostTest.java @@ -20,11 +20,14 @@ import net.wildfyre.areas.Areas; import net.wildfyre.http.RequestTest; import net.wildfyre.users.Users; +import net.wildfyre.posts.Post; import org.junit.Before; import org.junit.Test; import java.util.List; +import com.eclipsesource.json.JsonObject; + import static org.junit.Assert.*; public class PostTest { @@ -54,6 +57,32 @@ assertTrue(p2.toString(), p2.hasSubscribed()); assertTrue(p2.toString(), p2.isActive()); assertFalse(p2.toString(), p2.author().isPresent()); + + assertTrue(p1.equals(p1)); + assertFalse(p1.equals(p2)); + + assertEquals(new Post((PostData)p1), p1); + + assertEquals(p2.commentsList().size(), 2); + assertEquals(p2.commentsList().get(0), p2.comments().findFirst().get()); + + Comment c1 = p2.commentsList().get(0); + + JsonObject data = new JsonObject(); + data.set("id", c1.ID()); + JsonObject author = new JsonObject(); + author.set("user", c1.author().getID()); + data.set("author", author); + data.set("created", c1.created().toString()); + data.set("text", c1.text()); + data.set("imageURL", c1.imageURL().orElse("")); + Comment c2 = new Comment(p2, data); + assertEquals(c1, c2); + + assertEquals(c1.hashCode(), c2.ID()); + + assertEquals(c1.area(), p2.area()); + assertEquals(c1.post(), p2); } @Test @@ -61,13 +90,36 @@ Area a = Areas.INSTANCE.get("sample").orElseThrow(RuntimeException::new); Draft d = a.draft() - .setText("This is a test") + .setText("This is a test (1)") .setIsAnonymous(false) .subscribe() .save(); assertNotEquals(-1, d.postID); +// Post p = d.publish(); +// assertEquals(p.text(), "This is a test (1)"); +// p.delete(); + + d = a.draft() + .setText("This is a test (2)") + .setIsAnonymous(false) + .subscribe(); + +// p = d.publish(); +// assertEquals(p.text(), "This is a test (2)"); +// p.delete(); + + d = a.draft() + .setText("This is a test (3)") + .setIsAnonymous(true) + .subscribe() + .save() + .setText("This is a test (4)") + .save(); + + assertEquals(d.text(), "This is a test (4)"); + d.delete(); } diff --git a/src/test/java/net/wildfyre/utils/LazyMapTest.kt b/src/test/java/net/wildfyre/utils/LazyMapTest.kt new file mode 100644 --- /dev/null +++ b/src/test/java/net/wildfyre/utils/LazyMapTest.kt @@ -0,0 +1,71 @@ +/* + * Copyright 2019 Wildfyre.net + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 net.wildfyre.utils + +import org.junit.Assert.* +import org.junit.Test +import java.util.* +import net.wildfyre.utils.LazyMap + +class LazyMapTest { + @Test() + fun copyTest() { + val map = mapOf(0 to "1", 1 to "2", 2 to "3") + val lazyMap = LazyMap(map) + assertEquals(lazyMap.size, 3) + assertEquals(lazyMap[0], "1") + assertEquals(lazyMap[1], "2") + assertEquals(lazyMap[2], "3") + } + @Test( expected = NullPointerException::class ) + fun copyNullTest() { + val map: Map? = null + LazyMap(map) + } + @Test + fun equalsTest() { + val map1 = LazyMap() + val map2 = LazyMap() + assertEquals(map1, map2) + } + @Test + fun isEmptyTest() { + val map = LazyMap() + assertTrue(map.isEmpty()) + } + @Test + fun putAllTest() { + val map = mapOf(0 to "1", 1 to "2", 2 to "3") + val lazyMap1 = LazyMap() + lazyMap1.putAll(map) + assertEquals(lazyMap1.size, 3) + assertEquals(lazyMap1[0], "1") + assertEquals(lazyMap1[1], "2") + assertEquals(lazyMap1[2], "3") + + val lazyMap2 = LazyMap(HashMap()) + lazyMap2.putAll(map) + assertEquals(lazyMap2.size, 3) + assertEquals(lazyMap2[0], "1") + assertEquals(lazyMap2[1], "2") + assertEquals(lazyMap2[2], "3") + + val lazyMap3 = LazyMap(HashMap()) + lazyMap3.putAll(lazyMap3) + assertEquals(lazyMap3.size, 0) + } +}