Author: tchemit Date: 2012-07-13 19:16:17 +0200 (Fri, 13 Jul 2012) New Revision: 291 Url: http://nuiton.org/repositories/revision/jredmine/291 Log: rename packages Added: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/ trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceImpl.java trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRequestFactoryImpl.java trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRestClient.java trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImpl.java trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImplementorImpl.java trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceProviderImpl.java trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/ trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceTest.java trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineFixtures.java trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServer.java trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceAsAnonymousTest.java trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceTest.java Removed: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_3_x/ trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_3_x/ Added: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceImpl.java =================================================================== --- trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceImpl.java (rev 0) +++ trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceImpl.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,289 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.io.rest.RestClient; +import org.nuiton.jredmine.RedmineAnonymousService; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.RedmineServiceImplementor; +import org.nuiton.jredmine.RedmineServiceLoginException; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.Issue; +import org.nuiton.jredmine.model.IssueCategory; +import org.nuiton.jredmine.model.IssuePriority; +import org.nuiton.jredmine.model.IssueStatus; +import org.nuiton.jredmine.model.ModelHelper; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.Project; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Tracker; +import org.nuiton.jredmine.model.User; +import org.nuiton.jredmine.model.Version; + +/** + * Default implementation of {@link RedmineAnonymousService}. + * <p/> + * This implementation just make sure that the rest client is anonnymous. + * <p/> + * Created: 2 janv. 2010 + * + * @author tchemit <chemit@codelutin.com> + * @see RedmineServiceConfiguration#isAnonymous() + * @since 1.0.3 + */ +public class RedmineAnonymousServiceImpl implements RedmineServiceImplementor, RedmineAnonymousService { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(RedmineAnonymousServiceImpl.class); + + protected RedmineServiceImplementor delegateImplementor; + + public RedmineAnonymousServiceImpl() { + delegateImplementor = new RedmineServiceImplementorImpl(); + } + + /////////////////////////////////////////////////////////////////////////// + /// RedmineAnonymousService implementation + /////////////////////////////////////////////////////////////////////////// + + @Override + public IssueStatus[] getIssueStatuses() throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME, + IssueStatus.class + ); + } + + @Override + public IssuePriority[] getIssuePriorities() throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME, + IssuePriority.class + ); + } + + @Override + public Project[] getProjects() throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME, Project.class); + } + + @Override + public IssueCategory[] getIssueCategories(String projectName) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, + IssueCategory.class, + projectName + ); + } + + @Override + public Project getProject(String projectName) throws RedmineServiceException { + return getData(ModelHelper.GET_PROJECT_REQUEST_NAME, + Project.class, + projectName + ); + } + + @Override + public Tracker[] getTrackers(String projectName) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, + Tracker.class, + projectName + ); + } + + @Override + public News[] getNews(String projectName) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, + News.class, + projectName + ); + } + + @Override + public User[] getProjectMembers(String projectName) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_USER_REQUEST_NAME, + User.class, + projectName + ); + } + + @Override + public Version[] getVersions(String projectName) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, + Version.class, + projectName + ); + } + + @Override + public Version getVersion(String projectName, + String versionName) throws RedmineServiceException { + return getData(ModelHelper.GET_VERSION_REQUEST_NAME, + Version.class, + projectName, + versionName + ); + } + + @Override + public Attachment[] getAttachments(String projectName, + String versionName) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, + Attachment.class, + projectName, + versionName + ); + } + + @Override + public Issue[] getIssues(String projectName, + String versionName) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, + Issue.class, + projectName, + versionName + ); + } + + @Override + public TimeEntry[] getIssueTimeEntries(String projectName, + String issueId) throws RedmineServiceException { + return getDatas(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, + TimeEntry.class, + projectName, + issueId + ); + } + + @Override + public Issue[] getIssues(String projectName) throws RedmineServiceException { + Issue[] result = getDatas(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, + Issue.class, + projectName + ); + return result; + } + + @Override + public Issue[] getOpenedIssues(String projectName) throws RedmineServiceException { + Issue[] result = getDatas(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, + Issue.class, + projectName + ); + return result; + } + + @Override + public Issue[] getClosedIssues(String projectName) throws RedmineServiceException { + Issue[] result = getDatas(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, + Issue.class, + projectName + ); + return result; + } + + /////////////////////////////////////////////////////////////////////////// + /// RedmineServiceImplementor implementation + /////////////////////////////////////////////////////////////////////////// + @Override + public RedmineServiceImplementor init(RedmineServiceConfiguration configuration) throws RedmineServiceException { + // Force to not be loggued + configuration.setAnonymous(true); + if (log.isDebugEnabled()) { + log.debug("init configuration for " + this); + } + return delegateImplementor.init(configuration); + } + + @Override + public RestClient getSession() { + return delegateImplementor.getSession(); + } + + @Override + public int ping() { + return delegateImplementor.ping(); + } + + @Override + public void login() throws RedmineServiceLoginException { + delegateImplementor.login(); + } + + @Override + public void logout() throws RedmineServiceLoginException { + delegateImplementor.logout(); + } + + @Override + public <T> T getData(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + return delegateImplementor.getData(requestName, type, args); + } + + @Override + public <T> T[] getDatas(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + return delegateImplementor.getDatas(requestName, type, args); + } + + @Override + public <T> T sendData(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + return delegateImplementor.sendData(requestName, type, args); + } + + @Override + public <T> T[] sendDatas(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + return delegateImplementor.sendDatas(requestName, type, args); + } + + @Override + public boolean isInit() { + return delegateImplementor.isInit(); + } + + @Override + public boolean isLoggued() { + return delegateImplementor.isLoggued(); + } + + @Override + public void destroy() throws RedmineServiceException { + delegateImplementor.destroy(); + } + + @Override + public void checkLoggued() throws IllegalStateException, RedmineServiceLoginException, NullPointerException { + delegateImplementor.checkLoggued(); + } + +} Added: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRequestFactoryImpl.java =================================================================== --- trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRequestFactoryImpl.java (rev 0) +++ trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRequestFactoryImpl.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,235 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Strings; +import org.nuiton.io.rest.AbstractRequestFactory; +import org.nuiton.io.rest.RestMethod; +import org.nuiton.io.rest.RestRequest; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.ModelHelper; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Version; +import org.nuiton.jredmine.model.VersionStatusEnum; +import org.nuiton.jredmine.request.DefaultRedmineRequestBuilder; +import org.nuiton.jredmine.request.IssueScopeRedmineRequestBuilder; +import org.nuiton.jredmine.request.ProjectScopeRedmineRequestBuilder; +import org.nuiton.jredmine.request.VersionScopeRedmineRequestBuilder; + +import java.io.File; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Factory of {@link RestRequest}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ +public class RedmineRequestFactoryImpl extends AbstractRequestFactory { + + @Override + public void addDefaultRequests() { + + // misc requests + + addRequestBuilder(new DefaultRedmineRequestBuilder(ModelHelper.PING_REQUEST_NAME, RestMethod.GET, "jredmine", "ping")); +// addRequestBuilder(new DefaultRedmineRequestBuilder(PING_REQUEST_NAME, RestMethod.GET, "projects")); + addRequestBuilder(new DefaultRedmineRequestBuilder(ModelHelper.LOGOUT_REQUEST_NAME, RestMethod.GET, "jredmine", "logout")); +// addRequestBuilder(new DefaultRedmineRequestBuilder(LOGIN_REQUEST_NAME, RestMethod.POST, "login") { + addRequestBuilder(new DefaultRedmineRequestBuilder(ModelHelper.LOGIN_REQUEST_NAME, RestMethod.POST, "jredmine", "login") { + + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + String login = (String) args[0]; + String password = (String) args[1]; + return new String[]{"username", login, "password", password}; + } + }); + + // data with no scope requests + + addRequestBuilder(new DefaultRedmineRequestBuilder(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_projects.xml")); +// addRequestBuilder(new DefaultRedmineRequestBuilder(GET_ALL_PROJECT_REQUEST_NAME, RestMethod.GET, "projects.xml")); + addRequestBuilder(new DefaultRedmineRequestBuilder(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_user_projects.xml")); + addRequestBuilder(new DefaultRedmineRequestBuilder(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_statuses.xml")); + addRequestBuilder(new DefaultRedmineRequestBuilder(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_priorities.xml")); + + // data with project scope requests + + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_issues.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_opened_issues.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_closed_issues.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_versions.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_categories.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_trackers.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_USER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_users.xml")); + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_news.xml")); + + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.ADD_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + return new String[]{ + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.UPDATE_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "update_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + return new String[]{ + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.NEXT_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "next_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + + + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + String oldVersionName = (String) args[2]; + return new String[]{ + "oldVersionName", oldVersionName, + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRedmineRequestBuilder(ModelHelper.ADD_NEWS_REQUEST_NAME, RestMethod.POST, "jredmine", "add_news.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + News news = (News) args[1]; + return new String[]{ + "news[title]", news.getTitle(), + "news[summary]", news.getSummary(), + "news[description]", news.getDescription() + }; + } + }); + + // version scope requests + + addRequestBuilder(new VersionScopeRedmineRequestBuilder(ModelHelper.GET_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version.xml")); + addRequestBuilder(new VersionScopeRedmineRequestBuilder(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_issues.xml")); + addRequestBuilder(new VersionScopeRedmineRequestBuilder(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_attachments.xml")); + addRequestBuilder(new VersionScopeRedmineRequestBuilder(ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version_attachment.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + String versionId = (String) args[1]; + Attachment attachment = (Attachment) args[2]; + return new String[]{ + "version_name", versionId, + "attachment[description]", attachment.getDescription() + }; + } + + @Override + public Map<String, File> getAttachments(Object... args) { + Map<String, File> upload = new HashMap<String, File>(); + Attachment attachment = (Attachment) args[2]; + upload.put("attachment[file]", attachment.getToUpload()); + return upload; + } + }); + + // issue scope requests + + addRequestBuilder(new IssueScopeRedmineRequestBuilder(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_times.xml")); + + addRequestBuilder(new IssueScopeRedmineRequestBuilder(ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.POST, "jredmine", "add_issue_time.xml") { + private static final long serialVersionUID = 1L; + + @Override + public String[] getParameters(Object... args) { + String issueId = (String) args[1]; + TimeEntry timeEntry = (TimeEntry) args[2]; + Date d = timeEntry.getSpentOn(); + if (d == null) { + d = new Date(); + } + String date = DATE_FORMAT.format(d); + return new String[]{ + "issue_id", issueId, + //"timeEntry[issue_id]", issueId, + "time_entry[activity_id]", timeEntry.getActivityId() + "", + "time_entry[spent_on]", date, + "time_entry[hours]", timeEntry.getHours() + "", + "time_entry[comments]", timeEntry.getComments() == null ? "" : timeEntry.getComments() + }; + } + }); + } + + protected static String getVersionStatus(Version version) { + String status = version.getStatus(); + if (Strings.isNullOrEmpty(status)) { + + // use default open status + status = VersionStatusEnum.open.name(); + } + return status; + } + + protected static String getVersionEffectiveDate(Version version) { + return version.getEffectiveDate() == null ? "" : + DATE_FORMAT.format(version.getEffectiveDate()); + } + + +} Added: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRestClient.java =================================================================== --- trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRestClient.java (rev 0) +++ trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineRestClient.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,175 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.StatusLine; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codehaus.plexus.util.StringUtils; +import org.nuiton.io.rest.RequestFactory; +import org.nuiton.io.rest.RestClient; +import org.nuiton.io.rest.RestRequest; +import org.nuiton.io.rest.RestSession; +import org.nuiton.jredmine.model.ModelHelper; +import org.nuiton.jredmine.model.Version; +import org.nuiton.jredmine.model.VersionStatusEnum; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +/** + * Implementation of a {@link RestClient} to access a Redmine server via the + * {@code redmine_rest} rails plugin. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ +public class RedmineRestClient extends RestClient { + + private static final Log log = LogFactory.getLog(RedmineRestClient.class); + + /** + * To obtain redmine requests. + * + * @since 1.4 + */ + protected RequestFactory requestFactory; + + public final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + + public RedmineRestClient() { + requestFactory = new RedmineRequestFactoryImpl(); + } + + @Override + public RequestFactory getRequestFactory() { + return requestFactory; + } + + @Override + protected void open(RestSession session) throws IOException { + + ping(session); + + if (!configuration.isAnonymous()) { + login(session); + } + } + + @Override + protected void close(RestSession session) throws IOException { + + if (session != null) { + + try { + RestRequest request = getRequest(ModelHelper.LOGOUT_REQUEST_NAME); + session.setOpen(false); + session.doRequest(request); + } finally { + session.close(); + } + } + } + + public int ping(RestSession session) throws IOException { + + try { + + RestRequest request = getRequest(ModelHelper.PING_REQUEST_NAME); + HttpMethod gm = session.doRequest(request); + + StatusLine sl = gm.getStatusLine(); + int statusCode = sl.getStatusCode(); + if (log.isDebugEnabled()) { + log.debug("status code " + statusCode + " for " + gm.getPath()); + } + + if (statusCode != HttpStatus.SC_OK) { + gm.releaseConnection(); + throw new IOException( + "Got error code <" + statusCode + ":" + + sl.getReasonPhrase() + "> on " + gm.getPath()); + } + +// String content = gm.getResponseBodyAsString(); +// +// boolean ok = "ping".equals(content); +// if (!ok) { +// throw new IOException( +// "can not connect to " + configuration.getRestUrl()); +// } + return statusCode; + } catch (IOException ex) { + throw ex; + } catch (Exception ex) { + throw new IOException( + "could not ping " + configuration.getRestUrl() + + " for reason " + ex.getMessage(), ex); + } + } + + public void login(RestSession session) throws IOException { + + RestRequest request = getRequest(ModelHelper.LOGIN_REQUEST_NAME, + configuration.getRestUsername(), + configuration.getRestPassword()); + + HttpMethod gm = session.doRequest(request); + + StatusLine sl = gm.getStatusLine(); + int statusCode = sl.getStatusCode(); + if (log.isDebugEnabled()) { + log.debug("status code " + statusCode + " for " + gm.getPath()); + } + + if (statusCode != HttpStatus.SC_OK && + statusCode != HttpStatus.SC_MOVED_TEMPORARILY) { + gm.releaseConnection(); + throw new IOException( + "Got error code <" + statusCode + ":" + + sl.getReasonPhrase() + "> on " + gm.getPath()); + } + + // ok session is logged in + + } + + protected String getVersionStatus(Version version) { + String status = version.getStatus(); + if (StringUtils.isEmpty(status)) { + + // use default open status + status = VersionStatusEnum.open.name(); + } + return status; + } + + protected String getVersionEffectiveDate(Version version) { + return version.getEffectiveDate() == null ? "" : + DATE_FORMAT.format(version.getEffectiveDate()); + } +} Added: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImpl.java =================================================================== --- trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImpl.java (rev 0) +++ trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImpl.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,151 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.io.rest.RestClient; +import org.nuiton.jredmine.RedmineService; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.RedmineServiceImplementor; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.ModelHelper; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.Project; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Version; + +/** + * Default {@link RedmineService} implementation based on a {@link RestClient} + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ +public class RedmineServiceImpl extends RedmineAnonymousServiceImpl implements RedmineService { + + protected static final Log log = LogFactory.getLog(RedmineServiceImpl.class); + + /////////////////////////////////////////////////////////////////////////// + /// RedmineServiceImplementor implementation + /////////////////////////////////////////////////////////////////////////// + + @Override + public RedmineServiceImplementor init(RedmineServiceConfiguration configuration) throws RedmineServiceException { + return delegateImplementor.init(configuration); + } + + /////////////////////////////////////////////////////////////////////////// + /// RedmineLogguedService implementation + /////////////////////////////////////////////////////////////////////////// + + @Override + public Project[] getUserProjects() throws RedmineServiceException { + checkLoggued(); + Project[] result = getDatas(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME, Project.class); + return result; + } + + @Override + public Version addVersion(String projectName, + Version version) throws RedmineServiceException { + + // send data and obtain created version + Version result = sendData(ModelHelper.ADD_VERSION_REQUEST_NAME, + Version.class, + projectName, + version + ); + return result; + } + + @Override + public Version updateVersion(String projectName, + Version version) throws RedmineServiceException { + + // send data and obtain updated version + Version result = sendData(ModelHelper.UPDATE_VERSION_REQUEST_NAME, + Version.class, + projectName, + version + ); + return result; + } + + @Override + public Version nextVersion(String projectName, + String oldVersionName, + Version newVersion) throws RedmineServiceException { + + // send data and obtain updated or created new version + Version result = sendData(ModelHelper.NEXT_VERSION_REQUEST_NAME, + Version.class, + projectName, + newVersion, + oldVersionName + ); + return result; + } + + @Override + public Attachment addAttachment(String projectName, + String versionName, + Attachment attachement) throws RedmineServiceException { + + // send data and obtain created attachment + Attachment result = sendData(ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, + Attachment.class, + projectName, + versionName, + attachement + ); + return result; + } + + @Override + public News addNews(String projectName, + News news) throws RedmineServiceException { + // send data and obtain created news + News result = sendData(ModelHelper.ADD_NEWS_REQUEST_NAME, + News.class, + projectName, + news + ); + return result; + } + + @Override + public TimeEntry addIssueTimeEntry(String projectName, + String issueId, + TimeEntry entry) throws RedmineServiceException { + // send data and obtain updated version + TimeEntry result = sendData(ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, + TimeEntry.class, + projectName, + issueId, + entry + ); + return result; + } +} Added: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImplementorImpl.java =================================================================== --- trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImplementorImpl.java (rev 0) +++ trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceImplementorImpl.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,317 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.io.rest.RestClient; +import org.nuiton.io.rest.RestException; +import org.nuiton.io.rest.RestRequest; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.RedmineServiceImplementor; +import org.nuiton.jredmine.RedmineServiceLoginException; +import org.nuiton.jredmine.model.io.xpp3.DefaultRedmineXpp3Helper; +import org.nuiton.jredmine.model.io.xpp3.RedmineXpp3Helper; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Default Redmine technical service implementation. + * <p/> + * Created: 2 janv. 2010 + * + * @author tchemit <chemit@codelutin.com> + * @see RedmineServiceImplementor + * @since 1.0.3 + */ +public class RedmineServiceImplementorImpl implements RedmineServiceImplementor { + + /** Logger */ + private static final Log log = + LogFactory.getLog(RedmineServiceImplementorImpl.class); + + /** Client Rest. */ + protected final RedmineRestClient session; + + /** xpp3 xpp3Helper to transform xml stream to pojo. */ + protected final RedmineXpp3Helper xpp3Helper; + + /** internal state to known if service was init */ + protected boolean init; + + /** internal state to known if service was opened */ + protected boolean open; + + public RedmineServiceImplementorImpl() { + if (log.isDebugEnabled()) { + log.debug("new " + this); + } + session = new RedmineRestClient(); + xpp3Helper = new DefaultRedmineXpp3Helper(); + } + + /////////////////////////////////////////////////////////////////////////// + /// RedmineServiceImplementor implementation + /////////////////////////////////////////////////////////////////////////// + + + public RedmineRestClient getSession() { + return session; + } + + @Override + public boolean isInit() { + return init; + } + + @Override + public boolean isLoggued() { + return session.isOpen(); + } + + @Override + public RedmineServiceImplementor init(RedmineServiceConfiguration configuration) throws RedmineServiceException { + session.setConfiguration(configuration); + session.requestFactory.addDefaultRequests(); + RedmineServiceImplementor result = init(session); + return result; + } + + public RedmineServiceImplementor init(RedmineRestClient session) throws RedmineServiceException { + if (open) { + throw new IllegalStateException( + "the client " + this + " was already opened!"); + } + try { + + if (!session.isOpen()) { + session.open(); + } + + init = true; + } catch (Exception e) { + throw new RedmineServiceException( + "could not init service for reason " + e.getMessage(), e); + } + return this; + } + + @Override + public int ping() { + + try { + return session.ping(session.getSession()); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Could not ping ", e); + } + return -1; + } + } + + @Override + public void login() { + } + + @Override + public void logout() { + } + + @Override + public void destroy() throws RedmineServiceException { + checkInit(); + try { + if (session.isOpen()) { + try { + session.close(); + } catch (RestException ex) { + throw new RedmineServiceException( + "has problem while closing Rest client " + + ex.getMessage(), ex); + } + } + } finally { + // can't remove the reference since plexus injects it +// if (session != null) { +// session = null; +// } + init = false; + } + } + + @Override + public <T> T getData(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + checkInit(); + InputStream stream = askDataStream(requestName, args); + T result = getDataFromStream(type, stream); + return result; + } + + @Override + public <T> T[] getDatas(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + checkInit(); + InputStream stream = askDataStream(requestName, args); + T[] result = getDatasFromStream(type, stream); + return result; + } + + @Override + public <T> T sendData(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + checkLoggued(); + InputStream stream = sendDataStream(requestName, args); + T result = getDataFromStream(type, stream); + return result; + } + + @Override + public <T> T[] sendDatas(String requestName, + Class<T> type, + Object... args) throws RedmineServiceException { + checkLoggued(); + InputStream stream = sendDataStream(requestName, args); + T[] result = getDatasFromStream(type, stream); + return result; + } + + @Override + public void checkLoggued() throws IllegalStateException, RedmineServiceLoginException, NullPointerException { + checkInit(); + checkSessionNotNull(session); + checkSessionConfigurationNotNull(session); + if (session.getConfiguration().isAnonymous()) { + throw new RedmineServiceLoginException( + "can not access this service in anonymous mode"); + } + } + + protected InputStream askDataStream(String requestName, + Object... args) throws RedmineServiceException { + + RestRequest r = getRequest(requestName, args); + + // obtain data from rest client + try { + + InputStream stream = session.askData(r); + return stream; + } catch (Exception e) { + throw new RedmineServiceException( + "could not obtain data stream for request " + requestName + + " for reason " + e.getMessage(), e); + } + } + + protected InputStream sendDataStream(String requestName, + Object... args) throws RedmineServiceException { + + RestRequest r = getRequest(requestName, args); + + // obtain data from rest client + try { + + InputStream stream = session.sendData(r); + return stream; + } catch (Exception e) { + throw new RedmineServiceException( + "could not send data stream for request " + requestName + + " for reason " + e.getMessage(), e); + } + } + + protected RestRequest getRequest(String requestName, + Object... args) throws RedmineServiceException { + RestRequest r; + try { + r = session.getRequest(requestName, args); + } catch (Exception e) { + throw new RedmineServiceException( + "could not find the request named " + requestName + + " for reason " + e.getMessage(), e); + } + if (r == null) { + throw new RedmineServiceException( + "could not find the request named " + requestName); + } + return r; + } + + protected <T> T getDataFromStream(Class<T> type, + InputStream stream) throws RedmineServiceException { + if (stream == null) { + return null; + } + + try { + T result = xpp3Helper.readObject(type, stream, true); + return result; + } catch (Exception ex) { + throw new RedmineServiceException( + "could not obtain datas of type " + type + " for reason " + + ex.getMessage(), ex); + } + } + + protected <T> T[] getDatasFromStream(Class<T> type, + InputStream stream) throws RedmineServiceException { + if (stream == null) { + return null; + } + try { + T[] result = xpp3Helper.readObjects(type, stream, true); + return result; + } catch (Exception ex) { + throw new RedmineServiceException( + "could not obtain datas of type " + type + " for reason " + + ex.getMessage(), ex); + } + } + + protected void checkInit() throws IllegalStateException { + if (!init) { + throw new IllegalStateException( + "the client " + this + " is not init!"); + } + } + + protected void checkSessionNotNull(RestClient session) { + if (session == null) { + throw new NullPointerException("session can not be null"); + } + } + + protected void checkSessionConfigurationNotNull(RestClient session) { + if (session.getConfiguration() == null) { + throw new NullPointerException( + "session configuration can not be null"); + } + } +} Added: trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceProviderImpl.java =================================================================== --- trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceProviderImpl.java (rev 0) +++ trunk/jredmine-client-1.x/src/main/java/org/nuiton/jredmine/v1_x/RedmineServiceProviderImpl.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,59 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.nuiton.jredmine.RedmineAnonymousService; +import org.nuiton.jredmine.RedmineService; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceProvider; + +/** + * Implmentation of {@link RedmineServiceProvider} for this module. + * + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.jredmine.RedmineServiceProvider" role-hint="1.3.x" + * @since 1.4 + */ +public class RedmineServiceProviderImpl implements RedmineServiceProvider { + + @Override + public String getRedmineVersion() { + return "1.3.x"; + } + + @Override + public boolean isRequireJRedminePlugin() { + return true; + } + + @Override + public RedmineService newRedmineService(RedmineServiceConfiguration configuration) { + return new RedmineServiceImpl(); + } + + @Override + public RedmineAnonymousService newRedmineAnonymousService(RedmineServiceConfiguration configuration) { + return new RedmineAnonymousServiceImpl(); + } +} Added: trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceTest.java =================================================================== --- trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceTest.java (rev 0) +++ trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineAnonymousServiceTest.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,186 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.nuiton.jredmine.RedmineAnonymousService; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.Issue; +import org.nuiton.jredmine.model.IssueCategory; +import org.nuiton.jredmine.model.IssuePriority; +import org.nuiton.jredmine.model.IssueStatus; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.Project; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Tracker; +import org.nuiton.jredmine.model.User; +import org.nuiton.jredmine.model.Version; + +import java.io.IOException; + +/** + * Tests the {@link RedmineAnonymousServiceImpl}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class RedmineAnonymousServiceTest { + + protected static final RedmineFixtures fixtures = new RedmineFixtures(); + + @Rule + public final RedmineServer<RedmineAnonymousService> server = createNewServer(fixtures); + + protected RedmineAnonymousService getService() { + return server.getService(); + } + + protected RedmineServer<RedmineAnonymousService> createNewServer(RedmineFixtures fixtures) { + return new RedmineServer<RedmineAnonymousService>(fixtures) { + + @Override + protected RedmineAnonymousService createService( + RedmineFixtures fixture, + RedmineServiceConfiguration configuration) throws IOException, RedmineServiceException { + + return fixture.newRedmineAnonymousService(configuration); + } + + @Override + protected RedmineServiceConfiguration createConfiguration(RedmineFixtures fixture) throws IOException { + RedmineServiceConfiguration conf = fixture.newAnonymousConfiguration(); + return conf; + } + }; + } + + @BeforeClass + public static void beforeClass() { + RedmineServer<RedmineAnonymousService> newServer = + new RedmineAnonymousServiceTest().createNewServer(fixtures); + newServer.checkService(); + } + + @Test + public void getProjects() throws Exception { + Project[] projects = getService().getProjects(); + Assert.assertNotNull(projects); + } + + @Test + public void getIssuePriorities() throws Exception { + IssuePriority[] issuePriorities = getService().getIssuePriorities(); + Assert.assertNotNull(issuePriorities); + } + + @Test + public void getIssueStatuses() throws Exception { + IssueStatus[] issueStatuses = getService().getIssueStatuses(); + Assert.assertNotNull(issueStatuses); + } + + @Test + public void getProject() throws Exception { + Project project = getService().getProject(fixtures.projectName()); + Assert.assertNotNull(project); + } + + @Test + public void getIssueCategories() throws Exception { + IssueCategory[] issueCategories = getService().getIssueCategories(fixtures.projectName()); + Assert.assertNotNull(issueCategories); + } + + @Test + public void getTrackers() throws Exception { + Tracker[] trackers = getService().getTrackers(fixtures.projectName()); + Assert.assertNotNull(trackers); + } + + @Test + public void getNews() throws Exception { + News[] news = getService().getNews(fixtures.projectName()); + Assert.assertNotNull(news); + } + + @Test + public void getProjectMembers() throws Exception { + User[] users = getService().getProjectMembers(fixtures.projectName()); + Assert.assertNotNull(users); + } + + @Test + public void getProjectIssues() throws Exception { + Issue[] issues = getService().getIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getVersions() throws Exception { + Version[] versions = getService().getVersions(fixtures.projectName()); + Assert.assertNotNull(versions); + } + + @Test + public void getVersion() throws Exception { + Version version = getService().getVersion(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(version); + } + + @Test + public void getVersionIssues() throws Exception { + Issue[] issues = getService().getIssues(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(issues); + } + + @Test + public void getOpenedIssues() throws Exception { + Issue[] issues = getService().getOpenedIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getClosedIssues() throws Exception { + Issue[] issues = getService().getClosedIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getIssueTimeEntries() throws Exception { + TimeEntry[] timeEntries = getService().getIssueTimeEntries(fixtures.projectName(), fixtures.issueId()); + Assert.assertNotNull(timeEntries); + } + + @Test + public void getAttachments() throws Exception { + Attachment[] attachments = getService().getAttachments(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(attachments); + } + +} Added: trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineFixtures.java =================================================================== --- trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineFixtures.java (rev 0) +++ trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineFixtures.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,821 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Charsets; +import com.google.common.collect.ArrayListMultimap; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.jredmine.RedmineAnonymousService; +import org.nuiton.jredmine.RedmineService; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.Issue; +import org.nuiton.jredmine.model.IssueCategory; +import org.nuiton.jredmine.model.IssuePriority; +import org.nuiton.jredmine.model.IssueStatus; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.Project; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Tracker; +import org.nuiton.jredmine.model.User; +import org.nuiton.jredmine.model.Version; +import org.nuiton.jredmine.model.io.xpp3.RedmineDataConverter; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +/** + * fixtures of redmine model. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ +public class RedmineFixtures { + + /** Logger. */ + private static final Log log = LogFactory.getLog(RedmineFixtures.class); + + public static final String FILE_TO_UPLOAD_CONTENT = "Fichier à uploader"; + + public static final String PROJECT_NAME = "jredmine"; + + public static final String VERSION_NAME = "1.3"; + + public static final String ISSUE_ID = "2030"; + + private Project JRedmineProject; + + private ArrayListMultimap<Class<?>, Object> model; + + private RedmineServiceConfiguration anonymousConfiguration; + + private RedmineServiceConfiguration logguedConfiguration; + + public String projectName() { + return PROJECT_NAME; + } + + public String versionName() { + return VERSION_NAME; + } + + public String issueId() { + return ISSUE_ID; + } + + public RedmineServiceConfiguration newAnonymousConfiguration() + throws IOException { + RedmineServiceConfiguration conf = new FakeRedmineServiceConfiguration(); + copyConfiguration(getAnonymousConfiguration(), conf); + return conf; + } + + public RedmineServiceConfiguration newLogguedConfiguration() + throws IOException { + RedmineServiceConfiguration conf = new FakeRedmineServiceConfiguration(); + copyConfiguration(getLogguedConfiguration(), conf); + return conf; + } + + public RedmineAnonymousService newRedmineAnonymousService(RedmineServiceConfiguration configuration) + throws IOException, RedmineServiceException { + RedmineAnonymousServiceImpl service = new RedmineAnonymousServiceImpl(); + service.init(configuration); + return service; + } + + public RedmineService newRedmineService(RedmineServiceConfiguration configuration) + throws IOException, RedmineServiceException { + RedmineServiceImpl service = new RedmineServiceImpl(); + service.init(configuration); + return service; + } + + protected RedmineServiceConfiguration getAnonymousConfiguration() + throws IOException { + if (anonymousConfiguration == null) { + + Properties props = new Properties(); + + InputStream inputStream = null; + try { + String jredmineConfiguration = System.getenv("jredmine-test.properties"); + if (jredmineConfiguration == null) { + if (log.isWarnEnabled()) { + log.warn("Could not find environement variable " + + "'jredmine-test.properties' will use " + + "default test configuration"); + } + + inputStream = getClass().getResourceAsStream("/test-config.properties"); + } else { + + File file = new File(jredmineConfiguration); + + if (!file.exists()) { + throw new IllegalStateException("Could not find " + jredmineConfiguration + + " file"); + } + inputStream = FileUtils.openInputStream(file); + } + props.load(inputStream); + } finally { + if (inputStream != null) { + inputStream.close(); + } + } + anonymousConfiguration = new FakeRedmineServiceConfiguration(); + + String url = props.getProperty("test.redmineUrl"); + anonymousConfiguration.setRestUrl(new URL(url)); + + String e = props.getProperty("test.encoding"); + anonymousConfiguration.setEncoding(e); + + String verbose = props.getProperty("test.verbose"); + if (StringUtils.isNotEmpty(verbose)) { + anonymousConfiguration.setVerbose(Boolean.valueOf(verbose)); + } + anonymousConfiguration.setEncoding(Charsets.UTF_8.name()); + anonymousConfiguration.setAnonymous(true); + + verbose = System.getenv("jredmine-test.verbose"); + if (StringUtils.isNotEmpty(verbose)) { + anonymousConfiguration.setVerbose(Boolean.valueOf(verbose)); + } + + } + return anonymousConfiguration; + } + + protected RedmineServiceConfiguration getLogguedConfiguration() + throws IOException { + if (logguedConfiguration == null) { + + // use anonymous configuration + + RedmineServiceConfiguration anoConf = getAnonymousConfiguration(); + if (anoConf != null) { + logguedConfiguration = new FakeRedmineServiceConfiguration(); + copyConfiguration(anoConf, logguedConfiguration); + + // get system login password from env + String login = System.getenv("jredmine-test.login"); + String password = System.getenv("jredmine-test.password"); + if (!"null".equals(login)) { + logguedConfiguration.setRestUsername(login); + } + if (!"null".equals(password)) { + logguedConfiguration.setRestPassword(password); + } + logguedConfiguration.setAnonymous(false); + } + } + return logguedConfiguration; + } + + protected void copyConfiguration(RedmineServiceConfiguration src, + RedmineServiceConfiguration dst) { + dst.setRestUrl(src.getRestUrl()); + dst.setRestUsername(src.getRestUsername()); + dst.setRestPassword(src.getRestPassword()); + dst.setEncoding(src.getEncoding()); + dst.setVerbose(src.isVerbose()); + dst.setAnonymous(src.isAnonymous()); + } + + public List<Attachment> getAttachments() { + return get(Attachment.class); + } + + public List<Issue> getIssues() { + return get(Issue.class); + } + + public List<Project> getProjects() { + return get(Project.class); + } + + public List<Tracker> getTrackers() { + return get(Tracker.class); + } + + public List<User> getUsers() { + return get(User.class); + } + + public List<Version> getVersions() { + return get(Version.class); + } + + public <T> List<T> get(Class<T> modelType) { + if (model == null) { + try { + loadModel(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + return (List<T>) model.get(modelType); + } + + public <T> T get(Class<T> type, int pos) { + List<T> ts = get(type); + return ts.get(pos); + } + + public Project getJRedmineProject() { + if (JRedmineProject == null) { + JRedmineProject = new Project(); + JRedmineProject.setName("jredmine"); + JRedmineProject.setIdentifier("jredmine"); + JRedmineProject.setIsPublic(true); + JRedmineProject.setHomepage("http://maven-site.nuiton.org/jredmine"); + JRedmineProject.setDescription( + "Permet de communiquer en java avec un serveur " + "redmine qui a installé le plugin rails jredmine"); + JRedmineProject.setId(36); + JRedmineProject.setStatus(1); + } + return JRedmineProject; + } + + + public static final String VERSION_TO_CREATE_NAME = "do_not_use_me"; + + public Version getVersion() { + Version version = new Version(); + version.setName(VERSION_TO_CREATE_NAME); + version.setDescription("Une version créée par les tests de jredmine," + + " ne pas utiliser,et modifiée"); + return version; + } + + private void loadModel() + throws Exception { + model = ArrayListMultimap.create(); + + Attachment tempA; + tempA = new Attachment(); + tempA.setAuthorId((Integer) RedmineDataConverter.Integer.convert("4")); + tempA.setContainerId((Integer) RedmineDataConverter.Integer.convert("1")); + tempA.setId((Integer) RedmineDataConverter.Integer.convert("1")); + tempA.setFilesize((Integer) RedmineDataConverter.Integer.convert("411")); + tempA.setDownloads((Integer) RedmineDataConverter.Integer.convert("0")); + tempA.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T12:56:41+02:00")); + tempA.setContainerType((String) RedmineDataConverter.Text.convert("Version")); + tempA.setContentType((String) RedmineDataConverter.Text.convert("application/json")); + tempA.setDigest((String) RedmineDataConverter.Text.convert("6ea84342c7475c05fb077b4aca832f9a")); + tempA.setDiskFilename((String) RedmineDataConverter.Text.convert("090905125641_get_issue.json")); + tempA.setFilename((String) RedmineDataConverter.Text.convert("get_issue.json")); + model.put(Attachment.class, tempA); + tempA = new Attachment(); + tempA.setAuthorId((Integer) RedmineDataConverter.Integer.convert("4")); + tempA.setContainerId((Integer) RedmineDataConverter.Integer.convert("1")); + tempA.setId((Integer) RedmineDataConverter.Integer.convert("1")); + tempA.setFilesize((Integer) RedmineDataConverter.Integer.convert("411")); + tempA.setDownloads((Integer) RedmineDataConverter.Integer.convert("0")); + tempA.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T12:56:41+02:00")); + tempA.setContainerType((String) RedmineDataConverter.Text.convert("Version")); + tempA.setContentType((String) RedmineDataConverter.Text.convert("application/json")); + tempA.setDigest((String) RedmineDataConverter.Text.convert("6ea84342c7475c05fb077b4aca832f9a")); + tempA.setDiskFilename((String) RedmineDataConverter.Text.convert("090905125641_get_issue.json2")); + tempA.setFilename((String) RedmineDataConverter.Text.convert("get_issue.json2")); + model.put(Attachment.class, tempA); + + Issue tempI; + tempI = new Issue(); + tempI.setAuthorId((Integer) RedmineDataConverter.Integer.convert("5")); + tempI.setCategoryId((Integer) RedmineDataConverter.Integer.convert("2")); + tempI.setDoneRatio((Integer) RedmineDataConverter.Integer.convert("0")); + tempI.setLockVersion((Integer) RedmineDataConverter.Integer.convert("7")); + tempI.setPriorityId((Integer) RedmineDataConverter.Integer.convert("4")); + tempI.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempI.setStatusId((Integer) RedmineDataConverter.Integer.convert("3")); + tempI.setTrackerId((Integer) RedmineDataConverter.Integer.convert("1")); + tempI.setFixedVersionId((Integer) RedmineDataConverter.Integer.convert("1")); + tempI.setId((Integer) RedmineDataConverter.Integer.convert("3")); + tempI.setParentId((Integer) RedmineDataConverter.Integer.convert("3")); + tempI.setRootId((Integer) RedmineDataConverter.Integer.convert("3")); + tempI.setLft((Integer) RedmineDataConverter.Integer.convert("1")); + tempI.setRgt((Integer) RedmineDataConverter.Integer.convert("2")); + tempI.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T20:11:52+02:00")); + tempI.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T00:37:40+02:00")); + tempI.setStartDate((Date) RedmineDataConverter.Date.convert("2009-09-04")); + tempI.setDescription((String) RedmineDataConverter.Text.convert("avec une description !")); + tempI.setSubject((String) RedmineDataConverter.Text.convert("yes!")); + model.put(Issue.class, tempI); + tempI = new Issue(); + tempI.setAuthorId((Integer) RedmineDataConverter.Integer.convert("5")); + tempI.setCategoryId((Integer) RedmineDataConverter.Integer.convert("2")); + tempI.setDoneRatio((Integer) RedmineDataConverter.Integer.convert("0")); + tempI.setLockVersion((Integer) RedmineDataConverter.Integer.convert("7")); + tempI.setPriorityId((Integer) RedmineDataConverter.Integer.convert("4")); + tempI.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempI.setStatusId((Integer) RedmineDataConverter.Integer.convert("3")); + tempI.setTrackerId((Integer) RedmineDataConverter.Integer.convert("1")); + tempI.setFixedVersionId((Integer) RedmineDataConverter.Integer.convert("1")); + tempI.setId((Integer) RedmineDataConverter.Integer.convert("4")); + tempI.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T20:11:52+02:00")); + tempI.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T00:37:40+02:00")); + tempI.setStartDate((Date) RedmineDataConverter.Date.convert("2009-09-04")); + tempI.setDescription((String) RedmineDataConverter.Text.convert("avec une description !2")); + tempI.setSubject((String) RedmineDataConverter.Text.convert("yes!2")); + model.put(Issue.class, tempI); + + Project tempP; + tempP = new Project(); + tempP.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:11:54+02:00")); + tempP.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:11:54+02:00")); + tempP.setIdentifier((String) RedmineDataConverter.Text.convert("one")); + tempP.setName((String) RedmineDataConverter.Text.convert("one")); + tempP.setId((Integer) RedmineDataConverter.Integer.convert("1")); + tempP.setLft((Integer) RedmineDataConverter.Integer.convert("1")); + tempP.setRgt((Integer) RedmineDataConverter.Integer.convert("2")); + tempP.setProjectsCount((Integer) RedmineDataConverter.Integer.convert("0")); + tempP.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); + tempP.setIsPublic((Boolean) RedmineDataConverter.Boolean.convert("true")); + model.put(Project.class, tempP); + tempP = new Project(); + tempP.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T16:22:14+02:00")); + tempP.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T16:22:29+02:00")); + tempP.setIdentifier((String) RedmineDataConverter.Text.convert("two")); + tempP.setName((String) RedmineDataConverter.Text.convert("two")); + tempP.setId((Integer) RedmineDataConverter.Integer.convert("2")); + tempP.setProjectsCount((Integer) RedmineDataConverter.Integer.convert("0")); + tempP.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); + tempP.setIsPublic((Boolean) RedmineDataConverter.Boolean.convert("false")); + model.put(Project.class, tempP); + + Tracker tempT; + tempT = new Tracker(); + tempT.setId((Integer) RedmineDataConverter.Integer.convert("1")); + tempT.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempT.setTrackerId((Integer) RedmineDataConverter.Integer.convert("1")); + tempT.setPosition((Integer) RedmineDataConverter.Integer.convert("1")); + tempT.setIsInChlog((Boolean) RedmineDataConverter.Boolean.convert("true")); + tempT.setIsInRoadmap((Boolean) RedmineDataConverter.Boolean.convert("false")); + tempT.setName((String) RedmineDataConverter.Text.convert("Anomalie")); + model.put(Tracker.class, tempT); + tempT = new Tracker(); + tempT.setId((Integer) RedmineDataConverter.Integer.convert("2")); + tempT.setIsInChlog((Boolean) RedmineDataConverter.Boolean.convert("true")); + tempT.setIsInRoadmap((Boolean) RedmineDataConverter.Boolean.convert("true")); + tempT.setName((String) RedmineDataConverter.Text.convert("Evolution")); + tempT.setPosition((Integer) RedmineDataConverter.Integer.convert("2")); + tempT.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempT.setTrackerId((Integer) RedmineDataConverter.Integer.convert("2")); + model.put(Tracker.class, tempT); + tempT = new Tracker(); + tempT.setId((Integer) RedmineDataConverter.Integer.convert("3")); + tempT.setIsInChlog((Boolean) RedmineDataConverter.Boolean.convert("false")); + tempT.setIsInRoadmap((Boolean) RedmineDataConverter.Boolean.convert("false")); + tempT.setName((String) RedmineDataConverter.Text.convert("Assistance")); + tempT.setPosition((Integer) RedmineDataConverter.Integer.convert("3")); + tempT.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempT.setTrackerId((Integer) RedmineDataConverter.Integer.convert("3")); + model.put(Tracker.class, tempT); + + User tempU; + tempU = new User(); + tempU.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T17:24:46+02:00")); + tempU.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T01:23:59+02:00")); + tempU.setLastLoginOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T01:23:59+02:00")); + tempU.setId((Integer) RedmineDataConverter.Integer.convert("1")); + tempU.setMemberId((Integer) RedmineDataConverter.Integer.convert("5")); + tempU.setRoleId((Integer) RedmineDataConverter.Integer.convert("3")); + tempU.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); + tempU.setAdmin((Boolean) RedmineDataConverter.Boolean.convert("true")); + tempU.setMailNotification((Boolean) RedmineDataConverter.Boolean.convert("true")); + tempU.setFirstname((String) RedmineDataConverter.Text.convert("Redmine")); + tempU.setHashedPassword( + (String) RedmineDataConverter.Text.convert("70c881d4a26984ddce795f6f71817c9cf4480e79")); + tempU.setLanguage((String) RedmineDataConverter.Text.convert("fr")); + tempU.setLastname((String) RedmineDataConverter.Text.convert("Admin")); + tempU.setLogin((String) RedmineDataConverter.Text.convert("admin")); + tempU.setMail((String) RedmineDataConverter.Text.convert("dummy@codelutin.com")); + tempU.setIdentityUrl((String) RedmineDataConverter.Text.convert("yo")); + model.put(User.class, tempU); + tempU = new User(); + tempU.setAdmin((Boolean) RedmineDataConverter.Boolean.convert("true")); + tempU.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T19:49:02+02:00")); + tempU.setFirstname((String) RedmineDataConverter.Text.convert("tony")); + tempU.setHashedPassword( + (String) RedmineDataConverter.Text.convert("8aed1322e5450badb078e1fb60a817a1df25a2ca")); + tempU.setId((Integer) RedmineDataConverter.Integer.convert("5")); + tempU.setLanguage((String) RedmineDataConverter.Text.convert("fr")); + tempU.setLastLoginOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T19:49:38+02:00")); + tempU.setLastname((String) RedmineDataConverter.Text.convert("chemit2")); + tempU.setLogin((String) RedmineDataConverter.Text.convert("tchemit2")); + tempU.setMail((String) RedmineDataConverter.Text.convert("chemit@codelutin.com")); + tempU.setMailNotification((Boolean) RedmineDataConverter.Boolean.convert("false")); + tempU.setMemberId((Integer) RedmineDataConverter.Integer.convert("4")); + tempU.setRoleId((Integer) RedmineDataConverter.Integer.convert("3")); + tempU.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); + tempU.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T19:49:38+02:00")); + model.put(User.class, tempU); + tempU = new User(); + tempU.setAdmin((Boolean) RedmineDataConverter.Boolean.convert("false")); + tempU.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T16:24:11+02:00")); + tempU.setFirstname((String) RedmineDataConverter.Text.convert("dev")); + tempU.setHashedPassword( + (String) RedmineDataConverter.Text.convert("70c881d4a26984ddce795f6f71817c9cf4480e79")); + tempU.setId((Integer) RedmineDataConverter.Integer.convert("7")); + tempU.setLanguage((String) RedmineDataConverter.Text.convert("fr")); + tempU.setLastLoginOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T16:34:39+02:00")); + tempU.setLastname((String) RedmineDataConverter.Text.convert("dev")); + tempU.setLogin((String) RedmineDataConverter.Text.convert("dev")); + tempU.setMail((String) RedmineDataConverter.Text.convert("dev3@ynot-home.info")); + tempU.setMailNotification((Boolean) RedmineDataConverter.Boolean.convert("false")); + tempU.setMemberId((Integer) RedmineDataConverter.Integer.convert("9")); + tempU.setRoleId((Integer) RedmineDataConverter.Integer.convert("4")); + tempU.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); + tempU.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T16:34:39+02:00")); + model.put(User.class, tempU); + + Version tempV; + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:47:39+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("yo")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("9")); + tempV.setName((String) RedmineDataConverter.Text.convert("yor")); + tempV.setSharing((String) RedmineDataConverter.Text.convert("none")); + tempV.setStatus((String) RedmineDataConverter.Text.convert("open")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:50:49+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:09+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("13")); + tempV.setName((String) RedmineDataConverter.Text.convert("rrrrrrrrrouuuuuua")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:09+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:07:58+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("15")); + tempV.setName((String) RedmineDataConverter.Text.convert("aaaauuuuuua")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:07:58+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T04:12:25+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoyeppppppppppppppppp")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("16")); + tempV.setName((String) RedmineDataConverter.Text.convert("aaaau")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T04:13:20+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:40+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("14")); + tempV.setName((String) RedmineDataConverter.Text.convert("aaaaaaaaaarrrrrrrrrouuuuuua")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:40+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T00:39:15+02:00")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("5")); + tempV.setName((String) RedmineDataConverter.Text.convert("2")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T00:39:15+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:13:05+02:00")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setName((String) RedmineDataConverter.Text.convert("1.0.0")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:13:05+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:12+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("yoye")); + tempV.setEffectiveDate((Date) RedmineDataConverter.Date.convert("2009-09-06")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("11")); + tempV.setName((String) RedmineDataConverter.Text.convert("yaouuuuuua")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:12+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:50:59+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("yoye")); + tempV.setEffectiveDate((Date) RedmineDataConverter.Date.convert("2009-09-06")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("10")); + tempV.setName((String) RedmineDataConverter.Text.convert("ya")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:54:16+02:00")); + model.put(Version.class, tempV); + tempV = new Version(); + tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:37+02:00")); + tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); + tempV.setEffectiveDate((Date) RedmineDataConverter.Date.convert("2009-09-06")); + tempV.setId((Integer) RedmineDataConverter.Integer.convert("12")); + tempV.setName((String) RedmineDataConverter.Text.convert("ouuuuuua")); + tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); + tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:42+02:00")); + model.put(Version.class, tempV); + + IssueStatus tempIS; + tempIS = new IssueStatus(); + tempIS.setId(1); + tempIS.setName("Nouveau"); + tempIS.setPosition(1); + tempIS.setDefaultDoneRatio(10); + tempIS.setIsClosed(false); + tempIS.setIsDefault(true); + model.put(IssueStatus.class, tempIS); + tempIS = new IssueStatus(); + tempIS.setId(2); + tempIS.setName("Assigné"); + tempIS.setPosition(2); + tempIS.setIsClosed(false); + tempIS.setIsDefault(false); + model.put(IssueStatus.class, tempIS); + tempIS = new IssueStatus(); + tempIS.setId(3); + tempIS.setName("Résolu"); + tempIS.setPosition(3); + tempIS.setIsClosed(false); + tempIS.setIsDefault(false); + model.put(IssueStatus.class, tempIS); + tempIS = new IssueStatus(); + tempIS.setId(4); + tempIS.setName("Commentaire"); + tempIS.setPosition(4); + tempIS.setIsClosed(false); + tempIS.setIsDefault(false); + model.put(IssueStatus.class, tempIS); + tempIS = new IssueStatus(); + tempIS.setId(5); + tempIS.setName("Fermé"); + tempIS.setPosition(5); + tempIS.setIsClosed(true); + tempIS.setIsDefault(false); + model.put(IssueStatus.class, tempIS); + tempIS = new IssueStatus(); + tempIS.setId(6); + tempIS.setPosition(6); + tempIS.setName("Rejeté"); + tempIS.setIsClosed(true); + tempIS.setIsDefault(false); + model.put(IssueStatus.class, tempIS); + + IssuePriority tempIP; + tempIP = new IssuePriority(); + tempIP.setId(3); + tempIP.setParentId(1); + tempIP.setProjectId(2); + tempIP.setName("Bas"); + tempIP.setPosition(1); + tempIP.setOpt("IPRI"); + tempIP.setIsDefault(false); + tempIP.setActive(true); + model.put(IssuePriority.class, tempIP); + tempIP = new IssuePriority(); + tempIP.setId(4); + tempIP.setName("Normal"); + tempIP.setPosition(2); + tempIP.setOpt("IPRI"); + tempIP.setIsDefault(true); + model.put(IssuePriority.class, tempIP); + tempIP = new IssuePriority(); + tempIP.setId(5); + tempIP.setName("Haut"); + tempIP.setPosition(3); + tempIP.setOpt("IPRI"); + tempIP.setIsDefault(false); + model.put(IssuePriority.class, tempIP); + tempIP = new IssuePriority(); + tempIP.setId(6); + tempIP.setName("Urgent"); + tempIP.setPosition(4); + tempIP.setOpt("IPRI"); + tempIP.setIsDefault(false); + model.put(IssuePriority.class, tempIP); + tempIP = new IssuePriority(); + tempIP.setId(7); + tempIP.setName("Immédiat"); + tempIP.setPosition(5); + tempIP.setOpt("IPRI"); + tempIP.setIsDefault(false); + model.put(IssuePriority.class, tempIP); + + IssueCategory tempIC; + tempIC = new IssueCategory(); + tempIC.setId(1); + tempIC.setName("categorie one"); + tempIC.setProjectId(1); + model.put(IssueCategory.class, tempIC); + tempIC = new IssueCategory(); + tempIC.setId(2); + tempIC.setName("categorie two"); + tempIC.setProjectId(1); + model.put(IssueCategory.class, tempIC); + + News tempN; + tempN = new News(); + tempN.setId(85); + tempN.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-17T21:50:26+02:00")); + tempN.setProjectId(1); + tempN.setAuthorId(4); + tempN.setCommentsCount(0); + tempN.setDescription("description"); + tempN.setSummary("summary"); + tempN.setTitle("title"); + model.put(News.class, tempN); + tempN = new News(); + tempN.setId(86); + tempN.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-17T21:55:26+02:00")); + tempN.setProjectId(1); + tempN.setAuthorId(4); + tempN.setCommentsCount(0); + tempN.setDescription("description2"); + tempN.setSummary("summary2"); + tempN.setTitle("title2"); + model.put(News.class, tempN); + + TimeEntry tempE; + + tempE = new TimeEntry(); + tempE.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:02:02+01:00")); + tempE.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:02:02+01:00")); + tempE.setSpentOn((Date) RedmineDataConverter.Date.convert("2009-12-31")); + tempE.setId(1); + tempE.setProjectId(1); + tempE.setUserId(4); + tempE.setIssueId(6); + tempE.setActivityId(8); + + tempE.setHours(1); + tempE.setTmonth(12); + tempE.setTyear(2009); + tempE.setTweek(53); + tempE.setComments("Test"); + model.put(TimeEntry.class, tempE); + + tempE = new TimeEntry(); + tempE.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:10:01+01:00")); + tempE.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:10:01+01:00")); + tempE.setSpentOn((Date) RedmineDataConverter.Date.convert("2009-12-31")); + tempE.setId(2); + tempE.setProjectId(1); + tempE.setUserId(4); + tempE.setIssueId(6); + tempE.setActivityId(9); + + tempE.setHours(2); + tempE.setTmonth(12); + tempE.setTyear(2009); + tempE.setTweek(53); + tempE.setComments("deuxième temps"); + model.put(TimeEntry.class, tempE); + } + + + public Attachment getAttachment() { + Attachment result = new Attachment(); + File fileToUpload = null; + try { + fileToUpload = File.createTempFile("toupload", ".txt"); + FileUtils.write(fileToUpload, FILE_TO_UPLOAD_CONTENT); + result.setToUpload(fileToUpload); + result.setDescription("attachment description..."); + return result; + } catch (IOException e) { + throw new RuntimeException("Could not create temp file", e); + } + + } + + /** + * Configuration of a redmine service for test purposes. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ + public static class FakeRedmineServiceConfiguration implements RedmineServiceConfiguration { + + URL restUrl; + + String restUsername; + + String restPassword; + + boolean verbose; + + boolean anonymous; + + String encoding; + + @Override + public String getEncoding() { + return encoding; + } + + @Override + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + @Override + public String getRestPassword() { + return restPassword; + } + + @Override + public void setRestPassword(String restPassword) { + this.restPassword = restPassword; + } + + @Override + public URL getRestUrl() { + return restUrl; + } + + @Override + public void setRestUrl(URL restUrl) { + this.restUrl = restUrl; + } + + @Override + public String getRestUsername() { + return restUsername; + } + + @Override + public void setRestUsername(String restUsername) { + this.restUsername = restUsername; + } + + @Override + public boolean isVerbose() { + return verbose; + } + + @Override + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + @Override + public boolean isAnonymous() { + return anonymous; + } + + @Override + public void setAnonymous(boolean anonymous) { + this.anonymous = anonymous; + } + + @Override + public String toString() { + ToStringBuilder b = new ToStringBuilder(this, + ToStringStyle.MULTI_LINE_STYLE + ); + b.append("redmineUrl", restUrl); + if (anonymous) { + b.append("anonymous", true); + } else { + b.append("redmineUsername", restUsername); + b.append("redminePassword", "***"); + } + b.append("encoding", encoding); + b.append("verbose", verbose); + return b.toString(); + } + } +} Added: trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServer.java =================================================================== --- trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServer.java (rev 0) +++ trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServer.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,206 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codehaus.plexus.util.StringUtils; +import org.junit.Assume; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.RedmineServiceImplementor; + +import java.io.IOException; + +/** + * A redmine server usable by tests. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ +public abstract class RedmineServer<S> extends TestWatcher { + + /** Logger. */ + private static final Log log = LogFactory.getLog(RedmineServer.class); + + /** + * Fixtures. + * + * @since 1.4 + */ + protected final RedmineFixtures fixtures; + + /** + * Service configuration to use. + * + * @since 1.4 + */ + protected RedmineServiceConfiguration configuration; + + /** + * Current service. + * + * @since 1.4 + */ + protected S service; + + public RedmineServer(RedmineFixtures fixtures) { + this.fixtures = fixtures; + } + + protected abstract S createService(RedmineFixtures fixtures, + RedmineServiceConfiguration configuration) throws IOException, RedmineServiceException; + + protected abstract RedmineServiceConfiguration createConfiguration(RedmineFixtures fixtures) throws IOException; + + public RedmineFixtures getFixtures() { + return fixtures; + } + + public RedmineServiceConfiguration getConfiguration() { + return configuration; + } + + public S getService() { + return service; + } + + @Override + protected void starting(Description description) { + + try { + configuration = createConfiguration(fixtures); + } catch (IOException e) { + + throw new IllegalStateException("Could not create configuration", e); + } + + try { + service = createService(fixtures, configuration); + } catch (Exception e) { + throw new IllegalStateException("Could not create service", e); + } + } + + @Override + protected void finished(Description description) { + + if (service != null) { + try { + ((RedmineServiceImplementor) service).destroy(); + } catch (RedmineServiceException e) { + throw new IllegalStateException("Could not close service ", e); + } + } + } + + protected void checkService() { + + if (log.isInfoEnabled()) { + log.info("Will check service..."); + } + + try { + + try { + configuration = createConfiguration(fixtures); + } catch (IOException e) { + + throw new IllegalStateException("Could not create configuration", e); + } + + boolean anonymous = configuration.isAnonymous(); + try { + configuration.setAnonymous(true); + service = createService(fixtures, configuration); + } catch (Exception e) { + + // could not init service + if (log.isInfoEnabled()) { + log.info("Will skip test " + + RedmineServiceTest.class.getName() + + ", since could not init service", e); + } + Assume.assumeTrue(false); + return; + + } finally { + + //push back initial flag value + configuration.setAnonymous(anonymous); + } + + if (!anonymous) { + + if (StringUtils.isBlank(configuration.getRestUsername())) { + if (log.isInfoEnabled()) { + log.info("Will skip test " + + RedmineServiceTest.class.getName() + + ", since not username was given and loggued service is required"); + } + Assume.assumeTrue(false); + return; + } + + + if (StringUtils.isBlank(configuration.getRestPassword())) { + if (log.isInfoEnabled()) { + log.info("Will skip test " + + RedmineServiceTest.class.getName() + + ", since not password was given and loggued service is required"); + } + Assume.assumeTrue(false); + return; + } + + // recheck service (to test now login) + try { + service = createService(fixtures, configuration); + } catch (Exception e) { + // ping was not ok + if (log.isInfoEnabled()) { + log.info("Will skip test " + + RedmineServiceTest.class.getName() + + ", since login [" + configuration.getRestUsername() + "] was bad : " + e.getMessage(), e); + } + + Assume.assumeTrue(false); + } + } + } finally { + if (log.isInfoEnabled()) { + log.info("Service checked..."); + } + if (service != null) { + try { + ((RedmineServiceImplementor) service).destroy(); + } catch (RedmineServiceException e) { + throw new IllegalStateException("Could not close service", e); + } + } + } + } +} Added: trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceAsAnonymousTest.java =================================================================== --- trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceAsAnonymousTest.java (rev 0) +++ trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceAsAnonymousTest.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,223 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.nuiton.jredmine.RedmineService; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.RedmineServiceLoginException; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.Issue; +import org.nuiton.jredmine.model.IssueCategory; +import org.nuiton.jredmine.model.IssuePriority; +import org.nuiton.jredmine.model.IssueStatus; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.Project; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Tracker; +import org.nuiton.jredmine.model.User; +import org.nuiton.jredmine.model.Version; + +import java.io.IOException; + +/** + * Tests the {@link RedmineAnonymousServiceImpl}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class RedmineServiceAsAnonymousTest { + + protected static final RedmineFixtures fixtures = new RedmineFixtures(); + + @Rule + public final RedmineServer<RedmineService> server = createNewServer(fixtures); + + protected RedmineService getService() { + return server.getService(); + } + + protected RedmineServer<RedmineService> createNewServer(RedmineFixtures fixtures) { + return new RedmineServer<RedmineService>(fixtures) { + + @Override + protected RedmineService createService( + RedmineFixtures fixture, + RedmineServiceConfiguration configuration) throws IOException, RedmineServiceException { + + return fixture.newRedmineService(configuration); + } + + @Override + protected RedmineServiceConfiguration createConfiguration(RedmineFixtures fixture) throws IOException { + RedmineServiceConfiguration conf = fixture.newAnonymousConfiguration(); + return conf; + } + }; + } + + + @BeforeClass + public static void beforeClass() { + RedmineServer<RedmineService> newServer = + new RedmineServiceAsAnonymousTest().createNewServer(fixtures); + newServer.checkService(); + } + + @Test + public void getProjects() throws Exception { + Project[] projects = getService().getProjects(); + Assert.assertNotNull(projects); + } + + @Test + public void getIssuePriorities() throws Exception { + IssuePriority[] issuePriorities = getService().getIssuePriorities(); + Assert.assertNotNull(issuePriorities); + } + + @Test + public void getIssueStatuses() throws Exception { + IssueStatus[] issueStatuses = getService().getIssueStatuses(); + Assert.assertNotNull(issueStatuses); + } + + @Test + public void getProject() throws Exception { + Project project = getService().getProject(fixtures.projectName()); + Assert.assertNotNull(project); + } + + @Test + public void getIssueCategories() throws Exception { + IssueCategory[] issueCategories = getService().getIssueCategories(fixtures.projectName()); + Assert.assertNotNull(issueCategories); + } + + @Test + public void getTrackers() throws Exception { + Tracker[] trackers = getService().getTrackers(fixtures.projectName()); + Assert.assertNotNull(trackers); + } + + @Test + public void getNews() throws Exception { + News[] news = getService().getNews(fixtures.projectName()); + Assert.assertNotNull(news); + } + + @Test + public void getProjectMembers() throws Exception { + User[] users = getService().getProjectMembers(fixtures.projectName()); + Assert.assertNotNull(users); + } + + @Test + public void getProjectIssues() throws Exception { + Issue[] issues = getService().getIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getVersions() throws Exception { + Version[] versions = getService().getVersions(fixtures.projectName()); + Assert.assertNotNull(versions); + } + + @Test + public void getVersion() throws Exception { + Version version = getService().getVersion(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(version); + } + + @Test + public void getVersionIssues() throws Exception { + Issue[] issues = getService().getIssues(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(issues); + } + + @Test + public void getOpenedIssues() throws Exception { + Issue[] issues = getService().getOpenedIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getClosedIssues() throws Exception { + Issue[] issues = getService().getClosedIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getIssueTimeEntries() throws Exception { + TimeEntry[] timeEntries = getService().getIssueTimeEntries(fixtures.projectName(), fixtures.issueId()); + Assert.assertNotNull(timeEntries); + } + + @Test + public void getAttachments() throws Exception { + Attachment[] attachments = getService().getAttachments(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(attachments); + } + + @Test(expected = RedmineServiceLoginException.class) + public void getUserProjects() throws Exception { + getService().getUserProjects(); + } + + @Test(expected = RedmineServiceLoginException.class) + public void addVersion() throws Exception { + getService().addVersion(fixtures.projectName(), new Version()); + } + + @Test(expected = RedmineServiceLoginException.class) + public void addAttachment() throws Exception { + getService().addAttachment(fixtures.projectName(), fixtures.versionName(), new Attachment()); + } + + @Test(expected = RedmineServiceLoginException.class) + public void addNews() throws Exception { + getService().addNews(fixtures.projectName(), new News()); + } + + @Test(expected = RedmineServiceLoginException.class) + public void updateVersion() throws Exception { + getService().updateVersion(fixtures.projectName(), new Version()); + } + + @Test(expected = RedmineServiceLoginException.class) + public void nextVersion() throws Exception { + getService().nextVersion(fixtures.projectName(), fixtures.versionName(), new Version()); + } + + @Test(expected = RedmineServiceLoginException.class) + public void addIssueTime() throws Exception { + getService().addIssueTimeEntry(fixtures.projectName(), fixtures.issueId(), new TimeEntry()); + } + +} Added: trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceTest.java =================================================================== --- trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceTest.java (rev 0) +++ trunk/jredmine-client-1.x/src/test/java/org/nuiton/jredmine/v1_x/RedmineServiceTest.java 2012-07-13 17:16:17 UTC (rev 291) @@ -0,0 +1,236 @@ +package org.nuiton.jredmine.v1_x; +/* + * #%L + * JRedmine :: Client 1.x + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program 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 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.nuiton.jredmine.RedmineService; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.RedmineServiceException; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.Issue; +import org.nuiton.jredmine.model.IssueCategory; +import org.nuiton.jredmine.model.IssuePriority; +import org.nuiton.jredmine.model.IssueStatus; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.Project; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Tracker; +import org.nuiton.jredmine.model.User; +import org.nuiton.jredmine.model.Version; + +import java.io.IOException; + +/** + * Tests the {@link RedmineAnonymousServiceImpl}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class RedmineServiceTest { + + protected static final RedmineFixtures fixtures = new RedmineFixtures(); + + @Rule + public final RedmineServer<RedmineService> server = createNewServer(fixtures); + + protected RedmineService getService() { + return server.getService(); + } + + + protected RedmineServer<RedmineService> createNewServer(RedmineFixtures fixtures) { + return new RedmineServer<RedmineService>(fixtures) { + + @Override + protected RedmineService createService( + RedmineFixtures fixture, + RedmineServiceConfiguration configuration) throws IOException, RedmineServiceException { + return fixture.newRedmineService(configuration); + } + + @Override + protected RedmineServiceConfiguration createConfiguration(RedmineFixtures fixture) throws IOException { + RedmineServiceConfiguration conf = fixture.newLogguedConfiguration(); + return conf; + } + }; + } + + @BeforeClass + public static void beforeClass() { + RedmineServer<RedmineService> newServer = + new RedmineServiceTest().createNewServer(fixtures); + newServer.checkService(); + } + + @Test + public void getProjects() throws Exception { + Project[] projects = getService().getProjects(); + Assert.assertNotNull(projects); + } + + @Test + public void getIssuePriorities() throws Exception { + IssuePriority[] issuePriorities = getService().getIssuePriorities(); + Assert.assertNotNull(issuePriorities); + } + + @Test + public void getIssueStatuses() throws Exception { + IssueStatus[] issueStatuses = getService().getIssueStatuses(); + Assert.assertNotNull(issueStatuses); + } + + @Test + public void getProject() throws Exception { + Project project = getService().getProject(fixtures.projectName()); + Assert.assertNotNull(project); + } + + @Test + public void getIssueCategories() throws Exception { + IssueCategory[] issueCategories = getService().getIssueCategories(fixtures.projectName()); + Assert.assertNotNull(issueCategories); + } + + @Test + public void getTrackers() throws Exception { + Tracker[] trackers = getService().getTrackers(fixtures.projectName()); + Assert.assertNotNull(trackers); + } + + @Test + public void getNews() throws Exception { + News[] news = getService().getNews(fixtures.projectName()); + Assert.assertNotNull(news); + } + + @Test + public void getProjectMembers() throws Exception { + User[] users = getService().getProjectMembers(fixtures.projectName()); + Assert.assertNotNull(users); + } + + @Test + public void getProjectIssues() throws Exception { + Issue[] issues = getService().getIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getVersions() throws Exception { + Version[] versions = getService().getVersions(fixtures.projectName()); + Assert.assertNotNull(versions); + } + + @Test + public void getVersion() throws Exception { + Version version = getService().getVersion(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(version); + } + + @Test + public void getVersionIssues() throws Exception { + Issue[] issues = getService().getIssues(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(issues); + } + + @Test + public void getOpenedIssues() throws Exception { + Issue[] issues = getService().getOpenedIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getClosedIssues() throws Exception { + Issue[] issues = getService().getClosedIssues(fixtures.projectName()); + Assert.assertNotNull(issues); + } + + @Test + public void getIssueTimeEntries() throws Exception { + TimeEntry[] timeEntries = getService().getIssueTimeEntries(fixtures.projectName(), fixtures.issueId()); + Assert.assertNotNull(timeEntries); + } + + @Test + public void getAttachments() throws Exception { + Attachment[] attachments = getService().getAttachments(fixtures.projectName(), fixtures.versionName()); + Assert.assertNotNull(attachments); + } + + @Test + public void getUserProjects() throws Exception { + Project[] userProjects = getService().getUserProjects(); + Assert.assertNotNull(userProjects); + } + + @Ignore + @Test + public void addVersion() throws Exception { + getService().addVersion(fixtures.projectName(), new Version()); + } + + @Ignore + @Test + public void addAttachment() throws Exception { + Version version = fixtures.getVersion(); + Attachment attachment = fixtures.getAttachment(); + Attachment updatedAttachment = getService().addAttachment( + fixtures.projectName(), + version.getName(), + attachment + ); + Assert.assertNotNull(updatedAttachment); + } + + @Ignore + @Test + public void addNews() throws Exception { + getService().addNews(fixtures.projectName(), new News()); + } + + @Ignore + @Test + public void updateVersion() throws Exception { + getService().updateVersion(fixtures.projectName(), new Version()); + } + + @Ignore + @Test + public void nextVersion() throws Exception { + getService().nextVersion(fixtures.projectName(), fixtures.versionName(), new Version()); + } + + @Ignore + @Test + public void addIssueTime() throws Exception { + getService().addIssueTimeEntry(fixtures.projectName(), fixtures.issueId(), new TimeEntry()); + } + +}