From 691c76c7c3af9b17185723c2781c8e9959e2c4ad Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Thu, 4 May 2017 16:20:13 +0300 Subject: [PATCH] RtTeams and JsonTeam + unit test --- .../com/amihaiemil/versioneye/JsonTeam.java | 142 ++++++++++++++++++ .../amihaiemil/versioneye/RtOrganization.java | 6 +- .../com/amihaiemil/versioneye/RtTeams.java | 78 ++++++++++ .../java/com/amihaiemil/versioneye/Team.java | 139 +++++++++++++++++ .../java/com/amihaiemil/versioneye/Teams.java | 11 +- .../versioneye/RtOrganizationsTestCase.java | 48 ++++++ src/test/resources/teams.json | 46 ++++++ 7 files changed, 466 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/amihaiemil/versioneye/JsonTeam.java create mode 100644 src/main/java/com/amihaiemil/versioneye/RtTeams.java create mode 100644 src/main/java/com/amihaiemil/versioneye/Team.java create mode 100644 src/test/resources/teams.json diff --git a/src/main/java/com/amihaiemil/versioneye/JsonTeam.java b/src/main/java/com/amihaiemil/versioneye/JsonTeam.java new file mode 100644 index 0000000..2a05c5f --- /dev/null +++ b/src/main/java/com/amihaiemil/versioneye/JsonTeam.java @@ -0,0 +1,142 @@ +/** + * 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.util.ArrayList; +import java.util.List; + +import javax.json.JsonArray; +import javax.json.JsonObject; + +/** + * VersionEye Team backed by a JsonObject. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.0 + */ +final class JsonTeam implements Team { + + /** + * Team. + */ + private JsonObject team; + + /** + * Ctor. + * @param team Given team. + */ + JsonTeam(final JsonObject team) { + this.team = team; + } + + @Override + public String teamId() { + return this.team.getString("ids"); + } + + @Override + public String name() { + return this.team.getString("name"); + } + + @Override + public boolean versionNotifications() { + return this.team.getBoolean("version_notifications"); + } + + @Override + public boolean licenseNotifications() { + return this.team.getBoolean("license_notifications"); + } + + @Override + public boolean securityNotifications() { + return this.team.getBoolean("security_notifications"); + } + + @Override + public boolean monday() { + return this.team.getBoolean("monday"); + } + + @Override + public boolean tuesday() { + return this.team.getBoolean("tuesday"); + } + + @Override + public boolean wednesday() { + return this.team.getBoolean("wednesday"); + } + + @Override + public boolean thursday() { + return this.team.getBoolean("thursday"); + } + + @Override + public boolean friday() { + return this.team.getBoolean("friday"); + } + + @Override + public boolean saturday() { + return this.team.getBoolean("saturday"); + } + + @Override + public boolean sunday() { + return this.team.getBoolean("sunday"); + } + + @Override + public List users() { + final JsonArray users = this.team.getJsonArray("users"); + final List members = new ArrayList<>(); + for(int idx = 0; idx < users.size(); idx++) { + members.add(new JsonUserData(users.getJsonObject(idx))); + } + return members; + } + + @Override + public String createdAt() { + return this.team.getString("created_at"); + } + + @Override + public String updatedAt() { + return this.team.getString("updated_at"); + } + + @Override + public JsonObject json() { + return this.team; + } + +} diff --git a/src/main/java/com/amihaiemil/versioneye/RtOrganization.java b/src/main/java/com/amihaiemil/versioneye/RtOrganization.java index 5e36d66..f046363 100644 --- a/src/main/java/com/amihaiemil/versioneye/RtOrganization.java +++ b/src/main/java/com/amihaiemil/versioneye/RtOrganization.java @@ -36,7 +36,7 @@ * @author Sherif Waly (sheifwaly95@gmail.com) * @version $Id$ * @since 1.0.0 - * @todo #45:30min/DEV implement and test `teams()` and `projects()` methods. + * @todo #37:30min/DEV implement and test `projects()` method. */ final class RtOrganization implements Organization { @@ -57,12 +57,12 @@ final class RtOrganization implements Organization { */ RtOrganization(final JsonObject organization, final Request entry) { this.organization = organization; - this.req = entry; + this.req = entry.uri().path(organization.getString("name")).back(); } @Override public Teams teams() { - return null; + return new RtTeams(this.req, this.organization.getString("api_key")); } @Override diff --git a/src/main/java/com/amihaiemil/versioneye/RtTeams.java b/src/main/java/com/amihaiemil/versioneye/RtTeams.java new file mode 100644 index 0000000..9bc9130 --- /dev/null +++ b/src/main/java/com/amihaiemil/versioneye/RtTeams.java @@ -0,0 +1,78 @@ +/** + * 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 java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.List; +import javax.json.JsonArray; +import com.jcabi.http.Request; +import com.jcabi.http.response.JsonResponse; +import com.jcabi.http.response.RestResponse; + +/** + * Real implementation for {@link Teams}. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.0 + */ +final class RtTeams implements Teams { + + /** + * HTTP request. + */ + private Request req; + + /** + * Ctor. + * @param entry HTTP request. + * @param orgKey The organization's api_key + */ + RtTeams(final Request entry, final String orgKey) { + this.req = entry.uri().queryParam("api_key", orgKey).back(); + } + + @Override + public List fetch() throws IOException { + final JsonArray array = this.req.fetch() + .as(RestResponse.class) + .assertStatus(HttpURLConnection.HTTP_OK) + .as(JsonResponse.class) + .json() + .readArray(); + final List teams = new ArrayList<>(); + for(int idx=0; idx users(); + + /** + * Created at. + * @return String. + */ + String createdAt(); + + /** + * Updated at. + * @return String. + */ + String updatedAt(); + + /** + * This Team as JsonObject. + * @return JsonObject. + */ + JsonObject json(); + +} diff --git a/src/main/java/com/amihaiemil/versioneye/Teams.java b/src/main/java/com/amihaiemil/versioneye/Teams.java index e088117..e033039 100644 --- a/src/main/java/com/amihaiemil/versioneye/Teams.java +++ b/src/main/java/com/amihaiemil/versioneye/Teams.java @@ -27,6 +27,9 @@ */ package com.amihaiemil.versioneye; +import java.io.IOException; +import java.util.List; + /** * An organization's teams. * @author Sherif Waly (sherifwaly95@gmail.com) @@ -36,5 +39,11 @@ * The class should work with a given request and organization name. */ public interface Teams { - + + /** + * Fetch the teams. + * @return List of {@link Team} + * @throws IOException If something goes wrong with the HTTP request. + */ + List fetch() throws IOException; } diff --git a/src/test/java/com/amihaiemil/versioneye/RtOrganizationsTestCase.java b/src/test/java/com/amihaiemil/versioneye/RtOrganizationsTestCase.java index 7f8eebe..503038f 100644 --- a/src/test/java/com/amihaiemil/versioneye/RtOrganizationsTestCase.java +++ b/src/test/java/com/amihaiemil/versioneye/RtOrganizationsTestCase.java @@ -118,6 +118,54 @@ public void fetchesOrganization() throws IOException { ); } + /** + * An Organization's teams can be fetched. + * @throws IOException If something goes wrong. + */ + @Test + public void teamsAreFetched() throws IOException { + final MkContainer container = new MkGrizzlyContainer().next( + new MkAnswer.Simple( + HttpURLConnection.HTTP_OK, + this.readResource("organizations.json") + ) + ).next( + new MkAnswer.Simple( + HttpURLConnection.HTTP_OK, + this.readResource("teams.json") + ) + ).start(); + final Organizations organizations = new RtOrganizations( + new JdkRequest(container.home()) + ); + final List teams = organizations.fetch().get(0).teams().fetch(); + MatcherAssert.assertThat( + container.take().uri().toString(), + Matchers.equalTo("/organisations") + ); + MatcherAssert.assertThat( + container.take().uri().toString(), + Matchers.equalTo( + "/organisations/sherifwaly_orga?api_key=88870f2710dba853a326" + ) + ); + MatcherAssert.assertThat(teams.size(), Matchers.is(2)); + + Team team = teams.get(0); + MatcherAssert.assertThat( + team.name(), + Matchers.equalTo("Owners") + ); + MatcherAssert.assertThat( + team.createdAt(), + Matchers.equalTo("2017-01-11T08:04:13.077Z") + ); + MatcherAssert.assertThat( + team.updatedAt(), + Matchers.equalTo("2017-01-11T08:15:18.077Z") + ); + } + /** * RtOrganizations can return an organization with given name. * @throws IOException If something goes wrong. diff --git a/src/test/resources/teams.json b/src/test/resources/teams.json new file mode 100644 index 0000000..8216f74 --- /dev/null +++ b/src/test/resources/teams.json @@ -0,0 +1,46 @@ +[ + { + "ids": "5875e6fd224dfe001251bef2", + "name": "Owners", + "version_notifications": true, + "license_notifications": true, + "security_notifications": true, + "monday": true, + "tuesday": true, + "wednesday": true, + "thursday": true, + "friday": true, + "saturday": false, + "sunday": false, + "users": [{ + "id": "5875e6fc224dfe001251bef0", + "username": "amihaiemil", + "fullname": "amihaiemil" + }], + "projects": [], + "created_at": "2017-01-11T08:04:13.077Z", + "updated_at": "2017-01-11T08:15:18.077Z" + }, + { + "ids": "590b190ee01eee0016ad2ba5", + "name": "amihaiemil_team", + "version_notifications": true, + "license_notifications": true, + "security_notifications": true, + "monday": true, + "tuesday": true, + "wednesday": true, + "thursday": true, + "friday": true, + "saturday": false, + "sunday": false, + "users": [{ + "id": "58fcd9c174e93c0017237d99", + "username": "SherifWaly", + "fullname": "SherifWaly" + }], + "projects": [], + "created_at": "2017-05-04T12:05:34.515Z", + "updated_at": "2017-05-04T12:05:34.515Z" + } +] \ No newline at end of file