Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
MkUsers, MkUser and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jun 1, 2017
1 parent 4254e1a commit 8294c2d
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/amihaiemil/versioneye/MkJsonServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
135 changes: 135 additions & 0 deletions src/main/java/com/amihaiemil/versioneye/MkNewUser.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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"));
}

}
120 changes: 120 additions & 0 deletions src/main/java/com/amihaiemil/versioneye/MkUser.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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;
}

}
62 changes: 62 additions & 0 deletions src/main/java/com/amihaiemil/versioneye/MkUsers.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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);
}

}
10 changes: 5 additions & 5 deletions src/main/java/com/amihaiemil/versioneye/MkVersionEye.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
* @author Mihai Andronache ([email protected])
* @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 {

Expand All @@ -61,6 +60,7 @@ public final class MkVersionEye implements VersionEye {
public MkVersionEye() {
this.server = new MkJsonServer();
}

/**
* Ctor.
* @param authenticated Mock Authenticated User.
Expand Down Expand Up @@ -89,7 +89,7 @@ public Services services() {

@Override
public Users users() {
return null;
return new MkUsers(this.server);
}

@Override
Expand All @@ -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) {
Expand Down
Loading

2 comments on commit 8294c2d

@0pdd
Copy link

@0pdd 0pdd commented on 8294c2d Jun 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 13-9719886d disappeared in src/main/java/com/amihaiemil/versioneye/MkVersionEye.java, that's why I closed #72.

@0pdd
Copy link

@0pdd 0pdd commented on 8294c2d Jun 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 72-33f7b683 discovered in src/main/java/com/amihaiemil/versioneye/MkVersionEye.java and submitted as #108.

Please sign in to comment.