Skip to content

Commit

Permalink
feat: api for locale member list
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 24, 2017
1 parent b13f498 commit fe63594
Show file tree
Hide file tree
Showing 20 changed files with 370 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.rest.search.dto;
package org.zanata.rest.dto;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.zanata.common.LocaleId;
import org.zanata.model.HLocale;
import org.zanata.rest.dto.LocaleDetails;

/**
* @author Carlos Munoz
Expand All @@ -40,16 +38,7 @@ public LanguageTeamSearchResult() {
this.setType(SearchResultType.LanguageTeam);
}

public LanguageTeamSearchResult(HLocale locale) {
this.setType(SearchResultType.LanguageTeam);
this.setId(locale.getLocaleId().getId());
this.localeDetails = new LocaleDetails(locale.getLocaleId(),
locale.retrieveDisplayName(), null, locale.retrieveNativeName(),
locale.isActive(), locale.isEnabledByDefault(),
locale.getPluralForms());
this.memberCount = locale.getMembers().size();
}

@JsonProperty("localeDetails")
public LocaleDetails getLocaleDetails() {
return this.localeDetails;
}
Expand All @@ -58,6 +47,7 @@ public void setLocaleDetails(final LocaleDetails localeDetails) {
this.localeDetails = localeDetails;
}

@JsonProperty("memberCount")
public long getMemberCount() {
return this.memberCount;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2014, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.zanata.rest.dto;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import java.io.Serializable;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class LocaleMember implements Serializable {

private final String username;
private final Boolean isCoordinator;
private final Boolean isReviewer;
private final Boolean isTranslator;


public LocaleMember(String username, Boolean isCoordinator,
Boolean isReviewer, Boolean isTranslator) {
this.username = username;
this.isCoordinator = isCoordinator;
this.isReviewer = isReviewer;
this.isTranslator = isTranslator;
}

@JsonProperty("username")
public String getUsername() {
return username;
}

@JsonProperty("isCoordinator")
public Boolean getCoordinator() {
return isCoordinator;
}

@JsonProperty("isReviewer")
public Boolean getReviewer() {
return isReviewer;
}

@JsonProperty("isTranslator")
public Boolean getTranslator() {
return isTranslator;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

LocaleMember that = (LocaleMember) o;

if (username != null ? !username.equals(that.username) :
that.username != null) return false;
if (isCoordinator != null ? !isCoordinator.equals(that.isCoordinator) :
that.isCoordinator != null) return false;
if (isReviewer != null ? !isReviewer.equals(that.isReviewer) :
that.isReviewer != null) return false;
return isTranslator != null ? isTranslator.equals(that.isTranslator) :
that.isTranslator == null;
}

@Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
result =
31 * result +
(isCoordinator != null ? isCoordinator.hashCode() : 0);
result = 31 * result + (isReviewer != null ? isReviewer.hashCode() : 0);
result = 31 * result +
(isTranslator != null ? isTranslator.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.zanata.rest.editor.dto;
package org.zanata.rest.dto;

import java.io.Serializable;
import java.util.List;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.zanata.rest.dto.LocaleDetails;
import org.zanata.rest.search.dto.LanguageTeamSearchResult;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
Expand All @@ -23,11 +22,32 @@ public LocalesResults(final int totalCount,
this.results = results;
}

@JsonProperty("totalCount")
public int getTotalCount() {
return this.totalCount;
}

@JsonProperty("results")
public List<LanguageTeamSearchResult> getResults() {
return this.results;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

LocalesResults results1 = (LocalesResults) o;

if (totalCount != results1.totalCount) return false;
return results != null ? results.equals(results1.results) :
results1.results == null;
}

@Override
public int hashCode() {
int result = totalCount;
result = 31 * result + (results != null ? results.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.rest.search.dto;
package org.zanata.rest.dto;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
* @author Carlos Munoz
* <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public abstract class SearchResult {

public enum SearchResultType {
Expand All @@ -38,6 +42,7 @@ public enum SearchResultType {
private String description;
private SearchResultType type;

@JsonProperty("id")
public String getId() {
return this.id;
}
Expand All @@ -46,6 +51,7 @@ public void setId(final String id) {
this.id = id;
}

@JsonProperty("description")
public String getDescription() {
return this.description;
}
Expand All @@ -54,6 +60,7 @@ public void setDescription(final String description) {
this.description = description;
}

@JsonProperty("type")
public SearchResultType getType() {
return this.type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.webcohesion.enunciate.metadata.rs.TypeHint;
import org.zanata.rest.dto.LocaleDetails;
import org.zanata.rest.dto.LocaleMember;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
Expand All @@ -34,7 +36,7 @@ public interface LocalesResource extends RestResource {
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response get(@QueryParam("filter") String filter,
Response get(@QueryParam("filter") String filter,
@QueryParam("sort") String fields,
@DefaultValue("1") @QueryParam("page") int page,
@DefaultValue("10") @QueryParam("sizePerPage") int sizePerPage);
Expand All @@ -45,7 +47,17 @@ public Response get(@QueryParam("filter") String filter,
@GET
@Path("/locale/{localeId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getDetails(@PathParam("localeId") String localeId);
@TypeHint(LocaleDetails.class)
Response getDetails(@PathParam("localeId") String localeId);

/**
* Retrieve locale member list
*/
@GET
@Path("/locale/{localeId}/members")
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(LocaleMember[].class)
Response getMembers(@PathParam("localeId") String localeId);

/**
* Retrieves a full list of localized locales for server.
Expand All @@ -59,6 +71,7 @@ public Response get(@QueryParam("filter") String filter,
@GET
@Path("/ui")
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(LocaleDetails[].class)
Response getUITranslations();


Expand All @@ -74,6 +87,7 @@ public Response get(@QueryParam("filter") String filter,
@GET
@Path("/new")
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(LocaleDetails[].class)
Response getNewLocales(@QueryParam("filter") String filter,
@QueryParam("size") @DefaultValue("10") int size);

Expand All @@ -89,7 +103,7 @@ Response getNewLocales(@QueryParam("filter") String filter,
@DELETE
@Path("/locale/{localeId}")
@Produces(MediaType.APPLICATION_JSON)
public Response delete(@PathParam("localeId") String localeId);
Response delete(@PathParam("localeId") String localeId);

/**
* Create a new language in Zanata
Expand All @@ -103,5 +117,6 @@ Response getNewLocales(@QueryParam("filter") String filter,
@PUT
@Path("/locale")
@Produces(MediaType.APPLICATION_JSON)
public Response createLanguage(LocaleDetails localeDetails);
@TypeHint(LocaleDetails.class)
Response createLanguage(LocaleDetails localeDetails);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.zanata.rest.dto;

import org.junit.Test;
import org.zanata.common.LocaleId;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

/**
* @author Alex Eng<a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
public class LanguageTeamSearchResultTest {
@Test
public void testConstructor() {
LanguageTeamSearchResult searchResults = new LanguageTeamSearchResult();
assertThat(searchResults.getType(), equalTo(
SearchResult.SearchResultType.LanguageTeam));
}

@Test
public void testLocaleDetails() {
LocaleDetails localeDetails =
new LocaleDetails(LocaleId.DE, "German", null, null, true, true,
null);
LanguageTeamSearchResult searchResults = new LanguageTeamSearchResult();
searchResults.setLocaleDetails(localeDetails);
assertThat(searchResults.getLocaleDetails(), equalTo(localeDetails));
}

@Test
public void testMemberCount() {
long count = 100L;
LanguageTeamSearchResult searchResults = new LanguageTeamSearchResult();
searchResults.setMemberCount(count);
assertThat(searchResults.getMemberCount(), equalTo(count));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.zanata.rest.dto;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

/**
* @author Alex Eng<a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
public class LocaleMemberTest {

@Test
public void testConstructor() {
String username = "user1";
LocaleMember member = new LocaleMember(username, true, true, true);
assertThat(member.getUsername(), equalTo(username));
assertThat(member.getCoordinator(), equalTo(true));
assertThat(member.getReviewer(), equalTo(true));
assertThat(member.getTranslator(), equalTo(true));
}

@Test
public void testEqualsAndHashCode() {
LocaleMember member = new LocaleMember("user1", true, true, true);
LocaleMember member2 = new LocaleMember("user1", true, true, true);
assertThat(member.hashCode(), equalTo(member2.hashCode()));
assertThat(member.equals(member2), equalTo(true));

// different username
member2 = new LocaleMember("user2", true, true, true);
assertThat(member.hashCode(), not(equalTo(member2.hashCode())));
assertThat(member.equals(member2), equalTo(false));

// different coordinator
member2 = new LocaleMember("user1", false, true, true);
assertThat(member.hashCode(), not(equalTo(member2.hashCode())));
assertThat(member.equals(member2), equalTo(false));

// different reviewer
member2 = new LocaleMember("user1", true, false, true);
assertThat(member.hashCode(), not(equalTo(member2.hashCode())));
assertThat(member.equals(member2), equalTo(false));

// different translator
member2 = new LocaleMember("user1", true, true, false);
assertThat(member.hashCode(), not(equalTo(member2.hashCode())));
assertThat(member.equals(member2), equalTo(false));
}
}
Loading

0 comments on commit fe63594

Please sign in to comment.