diff --git a/src/main/java/com/amihaiemil/versioneye/MkJsonServer.java b/src/main/java/com/amihaiemil/versioneye/MkJsonServer.java index 90c524a..f9c67a4 100644 --- a/src/main/java/com/amihaiemil/versioneye/MkJsonServer.java +++ b/src/main/java/com/amihaiemil/versioneye/MkJsonServer.java @@ -71,6 +71,7 @@ private static void initServer(final Storage storage) { .add("message", "pong") .build() ) - .add("authenticated", Json.createArrayBuilder().build()); + .add("authenticated", Json.createArrayBuilder().build()) + .add("users", Json.createArrayBuilder().build()); } } diff --git a/src/main/java/com/amihaiemil/versioneye/MkNewUser.java b/src/main/java/com/amihaiemil/versioneye/MkNewUser.java new file mode 100644 index 0000000..b0ba31d --- /dev/null +++ b/src/main/java/com/amihaiemil/versioneye/MkNewUser.java @@ -0,0 +1,135 @@ +/** + * Copyright (c) 2017, Mihai Emil Andronache + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +package com.amihaiemil.versioneye; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObject; +import javax.json.JsonValue; + +/** + * Mock user sign-up form. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.0 + */ +final class MkNewUser implements NewUser { + + /** + * VersionEye server. + */ + private MkServer server; + + /** + * This user as JsonObject. + */ + private JsonObject json; + + /** + * Ctor. + * @param server VersionEye server. + */ + MkNewUser(final MkServer server) { + this(server, Json.createObjectBuilder().build()); + } + + /** + * Ctor. + * @param server VersionEye server. + * @param newUser The new user as json. + */ + MkNewUser(final MkServer server, final JsonObject newUser) { + this.server = server; + this.json = newUser; + } + + @Override + public String fullName() { + return this.json.getString("fullname"); + } + + @Override + public String username() { + return this.json.getString("username"); + } + + @Override + public JsonObject json() { + return this.json; + } + + @Override + public NewUser fullName(final String fullName) { + final String username = this.json.getString("username", ""); + final JsonObject edited; + if(username.isEmpty()) { + edited = Json + .createObjectBuilder() + .add("fullname", fullName) + .build(); + } else { + edited = Json + .createObjectBuilder() + .add("username", username) + .add("fullname", fullName) + .build(); + } + return new MkNewUser(this.server, edited); + } + + @Override + public NewUser username(final String username) { + final JsonObject edited = Json + .createObjectBuilder() + .add("username", username) + .add("fullname", this.json.getString("fullname", "")) + .build(); + return new MkNewUser(this.server, edited); + } + + @Override + public User signUp() { + final JsonArray registered = this.server.storage().build() + .getJsonArray("users"); + final JsonArrayBuilder users = Json.createArrayBuilder(); + for(final JsonValue user: registered) { + users.add(user); + } + users.add( + Json.createObjectBuilder() + .add( + this.json.getString("username"), + this.json + ) + ); + this.server.storage().add("users", users.build()); + return new MkUser(this.server, this.json.getString("username")); + } + +} diff --git a/src/main/java/com/amihaiemil/versioneye/MkUser.java b/src/main/java/com/amihaiemil/versioneye/MkUser.java new file mode 100644 index 0000000..e47ce5e --- /dev/null +++ b/src/main/java/com/amihaiemil/versioneye/MkUser.java @@ -0,0 +1,120 @@ +package com.amihaiemil.versioneye; + +/** + * Copyright (c) 2017, Mihai Emil Andronache + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +import java.io.IOException; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonObject; + +/** + * A mock VersionEye user. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.0 + * + */ +final class MkUser implements User { + + /** + * VersionEye server. + */ + private MkServer server; + + /** + * This user's username. + */ + private String username; + + /** + * Ctor. + * @param server VersionEye server storage. + * @param username The user's username. + */ + MkUser(final MkServer server, final String username) { + this.server = server; + this.username = username; + } + + @Override + public UserData about() throws IOException { + UserData about = null; + final JsonArray online = this.server.storage().build() + .getJsonArray("authenticated"); + for(int idx = 0; idx < online.size(); idx++) { + final JsonObject authenticated = online.getJsonObject(idx); + if(authenticated.getJsonObject(this.username) != null) { + about = new JsonUserData( + Json.createObjectBuilder() + .add( + "fullname", + authenticated.getJsonObject(this.username) + .getString("fullname") + ).add( + "username", + authenticated.getJsonObject(this.username) + .getString("username") + ) + .build() + ); + } + } + final JsonArray users = this.server.storage().build() + .getJsonArray("users"); + for(int idx = 0; idx < users.size(); idx++) { + final JsonObject user = users.getJsonObject(idx); + if(user.getJsonObject(this.username) != null) { + about = new JsonUserData( + user.getJsonObject(this.username) + ); + } + } + if(about == null) { + throw new IllegalStateException( + "User " + this.username + " not found." + + " You have to register it first, " + + "or it has to be the authenticated user." + ); + } + return about; + } + + @Override + public Comments comments() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Favorites favorites() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/com/amihaiemil/versioneye/MkUsers.java b/src/main/java/com/amihaiemil/versioneye/MkUsers.java new file mode 100644 index 0000000..cea9466 --- /dev/null +++ b/src/main/java/com/amihaiemil/versioneye/MkUsers.java @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2017, Mihai Emil Andronache + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +package com.amihaiemil.versioneye; + +/** + * Mock of VersionEye's Users API. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.0 + * + */ +final class MkUsers implements Users { + + /** + * VersionEye server. + */ + private MkServer server; + + /** + * Ctor. + * @param server VersionEye server storage. + */ + MkUsers(final MkServer server) { + this.server = server; + } + + @Override + public User user(final String username) { + return new MkUser(this.server, username); + } + + @Override + public NewUser register() { + return new MkNewUser(this.server); + } + +} diff --git a/src/main/java/com/amihaiemil/versioneye/MkVersionEye.java b/src/main/java/com/amihaiemil/versioneye/MkVersionEye.java index 68d8364..7fd8a7c 100644 --- a/src/main/java/com/amihaiemil/versioneye/MkVersionEye.java +++ b/src/main/java/com/amihaiemil/versioneye/MkVersionEye.java @@ -39,9 +39,8 @@ * @author Mihai Andronache (amihaiemil@gmail.com) * @version $Id$ * @since 1.0.0 - * @todo #13:30min/DEV Continue implementing the mock API. - * Mocks for Users, Organizations Teams etc are needed. This puzzle - * was readded after being removed by mistake. + * @todo #72:30min/DEV Continue implementing the mock API. + * Mocks for Organizations, Teams etc are needed. */ public final class MkVersionEye implements VersionEye { @@ -61,6 +60,7 @@ public final class MkVersionEye implements VersionEye { public MkVersionEye() { this.server = new MkJsonServer(); } + /** * Ctor. * @param authenticated Mock Authenticated User. @@ -89,7 +89,7 @@ public Services services() { @Override public Users users() { - return null; + return new MkUsers(this.server); } @Override @@ -107,7 +107,7 @@ public Me me() { * @param authenticated The user to authenticate. */ private void authenticate(final Authenticated authenticated) { - JsonArray online = this.server.storage().build() + final JsonArray online = this.server.storage().build() .getJsonArray("authenticated"); final JsonArrayBuilder users = Json.createArrayBuilder(); for(final JsonValue user: online) { diff --git a/src/main/java/com/amihaiemil/versioneye/NewUser.java b/src/main/java/com/amihaiemil/versioneye/NewUser.java new file mode 100644 index 0000000..b5e0d18 --- /dev/null +++ b/src/main/java/com/amihaiemil/versioneye/NewUser.java @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2017, Mihai Emil Andronache + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +package com.amihaiemil.versioneye; + +/** + * User sign-up form. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.0 + */ +public interface NewUser extends UserData { + + /** + * Specify the user's full name. + * @param fullName Given full name. + * @return SignUpForm. + */ + NewUser fullName(final String fullName); + + /** + * Specify the user's username. + * @param username Given username. + * @return SignUpForm. + */ + NewUser username(final String username); + + /** + * Sign up. + * @return User. + */ + User signUp(); +} diff --git a/src/main/java/com/amihaiemil/versioneye/RtUsers.java b/src/main/java/com/amihaiemil/versioneye/RtUsers.java index e0ae046..b20b573 100644 --- a/src/main/java/com/amihaiemil/versioneye/RtUsers.java +++ b/src/main/java/com/amihaiemil/versioneye/RtUsers.java @@ -56,4 +56,12 @@ public User user(final String username) { return new RtUser(this.req, username); } + @Override + public NewUser register() { + throw new UnsupportedOperationException( + "Users#register() is only available in the mock version" + + "of the API yet" + ); + } + } diff --git a/src/main/java/com/amihaiemil/versioneye/Users.java b/src/main/java/com/amihaiemil/versioneye/Users.java index d1628aa..3128025 100644 --- a/src/main/java/com/amihaiemil/versioneye/Users.java +++ b/src/main/java/com/amihaiemil/versioneye/Users.java @@ -42,4 +42,10 @@ public interface Users { * @return A VersionEye user. */ User user(String username); + + /** + * Create a new VersionEye account. + * @return NewUser for signup. + */ + NewUser register(); } diff --git a/src/test/java/com/amihaiemil/versioneye/MkUsersTestCase.java b/src/test/java/com/amihaiemil/versioneye/MkUsersTestCase.java new file mode 100644 index 0000000..c493eb4 --- /dev/null +++ b/src/test/java/com/amihaiemil/versioneye/MkUsersTestCase.java @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2017, Mihai Emil Andronache + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +package com.amihaiemil.versioneye; + +import java.io.IOException; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Unit tests for {@link MkUsers}. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.0 + * + */ +public final class MkUsersTestCase { + + /** + * MkUsers can fetch the authenticated user. + * @throws IOException If something goes wrong. + */ + @Test + public void returnsAuthenticatedUser() throws IOException { + MkAuthenticated authenticated = new MkJsonAuthenticated(); + authenticated = authenticated.fullName("Mihai Andronache") + .username("amihaiemil") + .email("amihaiemil@gmail.com") + .admin(false) + .deleted(false) + .enterpriseProjects(1) + .rateLimit(50) + .compLimit(50) + .active(true); + User fetched = + new MkVersionEye(authenticated).users().user("amihaiemil"); + MatcherAssert.assertThat( + fetched, Matchers.notNullValue() + ); + MatcherAssert.assertThat( + fetched.about().username(), Matchers.is("amihaiemil") + ); + MatcherAssert.assertThat( + fetched.about().fullName(), Matchers.is("Mihai Andronache") + ); + } + + /** + * MkUsers can return a registered user which is not authenticated. + * @throws IOException If something goes wrong. + */ + @Test + public void returnsRegisteredUser() throws IOException { + final VersionEye versioneye = new MkVersionEye(); + final User registered = versioneye.users().register() + .fullName("Jeff Doe") + .username("jeff") + .signUp(); + final User fetched = versioneye.users().user("jeff"); + MatcherAssert.assertThat( + fetched.about().json(), + Matchers.equalTo(registered.about().json()) + ); + MatcherAssert.assertThat( + fetched.about().username(), Matchers.is("jeff") + ); + MatcherAssert.assertThat( + fetched.about().fullName(), Matchers.is("Jeff Doe") + ); + } + + /** + * An exception is thrown if the user is not found. + * @throws IOException If something goes wrong. + */ + @Test (expected = IllegalStateException.class) + public void throwsExceptionOnMissingUser() throws IOException { + new MkVersionEye().users().user("jeff").about(); + } + +}