From 3eee7427b5795355e01d81fe552b3893f2a3121e Mon Sep 17 00:00:00 2001 From: Mikolaj Izdebski Date: Mar 18 2014 14:18:23 +0000 Subject: Add Java client --- diff --git a/java/.gitignore b/java/.gitignore new file mode 100644 index 0000000..f66baa4 --- /dev/null +++ b/java/.gitignore @@ -0,0 +1,4 @@ +.project +.classpath +.settings +target/ diff --git a/java/AUTHORS b/java/AUTHORS new file mode 100644 index 0000000..c7f97d8 --- /dev/null +++ b/java/AUTHORS @@ -0,0 +1 @@ +Mikolaj Izdebski diff --git a/java/LICENSE b/java/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/java/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/java/NOTICE b/java/NOTICE new file mode 100644 index 0000000..ec33637 --- /dev/null +++ b/java/NOTICE @@ -0,0 +1,4 @@ +Copyright (c) 2014 Red Hat, Inc. + +This product includes software developed by +Red Hat, Inc. (http://www.redhat.com/). diff --git a/java/README b/java/README new file mode 100644 index 0000000..5a8b5b6 --- /dev/null +++ b/java/README @@ -0,0 +1 @@ +Copr Client for Java diff --git a/java/copr-api/pom.xml b/java/copr-api/pom.xml new file mode 100644 index 0000000..ee9edd5 --- /dev/null +++ b/java/copr-api/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + org.fedoraproject.copr.client + copr-parent + 0.0.0-SNAPSHOT + + copr-api + Copr Client API + diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/BuildRequest.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/BuildRequest.java new file mode 100644 index 0000000..5f27953 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/BuildRequest.java @@ -0,0 +1,101 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * @author Mikolaj Izdebski + */ +public class BuildRequest +{ + private String userName; + + private String projectName; + + private final Set srpms = new LinkedHashSet<>(); + + private String memory; + + private String timeout; + + public BuildRequest() + { + } + + public BuildRequest( String userName, String projectName ) + { + this.userName = userName; + this.projectName = projectName; + } + + public String getUserName() + { + return userName; + } + + public void setUserName( String userName ) + { + this.userName = userName; + } + + public String getProjectName() + { + return projectName; + } + + public void setProjectName( String projectName ) + { + this.projectName = projectName; + } + + public Collection getSourceRpmList() + { + return srpms; + } + + public void addSourceRpm( String url ) + { + srpms.add( url ); + } + + public void removeSourceRpm( String url ) + { + srpms.remove( url ); + } + + public String getMemory() + { + return memory; + } + + public void setMemory( String memory ) + { + this.memory = memory; + } + + public String getTimeout() + { + return timeout; + } + + public void setTimeout( String timeout ) + { + this.timeout = timeout; + } +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/BuildResult.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/BuildResult.java new file mode 100644 index 0000000..5efa6e8 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/BuildResult.java @@ -0,0 +1,26 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +/** + * @author Mikolaj Izdebski + */ +public interface BuildResult +{ + String getMessage(); + + long getBuildId(); +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprConfiguration.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprConfiguration.java new file mode 100644 index 0000000..7bfe741 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprConfiguration.java @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +/** + * @author Mikolaj Izdebski + */ +public class CoprConfiguration +{ + private String url; + + private String login; + + private String token; + + public String getUrl() + { + return url; + } + + public void setUrl( String url ) + { + this.url = url; + } + + public String getLogin() + { + return login; + } + + public void setLogin( String login ) + { + this.login = login; + } + + public String getToken() + { + return token; + } + + public void setToken( String token ) + { + this.token = token; + } +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprException.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprException.java new file mode 100644 index 0000000..59b6e16 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprException.java @@ -0,0 +1,35 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +/** + * @author Mikolaj Izdebski + */ +public class CoprException + extends Exception +{ + private static final long serialVersionUID = 1L; + + public CoprException( String detail ) + { + super( detail ); + } + + public CoprException( String detail, Throwable cause ) + { + super( detail, cause ); + } +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprService.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprService.java new file mode 100644 index 0000000..a13dff3 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprService.java @@ -0,0 +1,24 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +/** + * @author Mikolaj Izdebski + */ +public interface CoprService +{ + CoprSession newSession( CoprConfiguration configuration ); +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprSession.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprSession.java new file mode 100644 index 0000000..91e41c7 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/CoprSession.java @@ -0,0 +1,33 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +/** + * @author Mikolaj Izdebski + */ +public interface CoprSession + extends AutoCloseable +{ + ListResult list( ListRequest request ) + throws CoprException; + + BuildResult build( BuildRequest request ) + throws CoprException; + + @Override + void close() + throws CoprException; +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/ListRequest.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/ListRequest.java new file mode 100644 index 0000000..e57ee21 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/ListRequest.java @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +/** + * @author Mikolaj Izdebski + */ +public class ListRequest +{ + private String userName; + + public ListRequest() + { + } + + public ListRequest( String userName ) + { + this.userName = userName; + } + + public String getUserName() + { + return userName; + } + + public void setUserName( String userName ) + { + this.userName = userName; + } +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/ListResult.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/ListResult.java new file mode 100644 index 0000000..cb1c4d2 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/ListResult.java @@ -0,0 +1,26 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +import java.util.List; + +/** + * @author Mikolaj Izdebski + */ +public interface ListResult +{ + List getProjects(); +} diff --git a/java/copr-api/src/main/java/org/fedoraproject/copr/client/ProjectId.java b/java/copr-api/src/main/java/org/fedoraproject/copr/client/ProjectId.java new file mode 100644 index 0000000..a7d8a35 --- /dev/null +++ b/java/copr-api/src/main/java/org/fedoraproject/copr/client/ProjectId.java @@ -0,0 +1,46 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client; + +/** + * @author Mikolaj Izdebski + */ +public class ProjectId +{ + private String userName; + + private String projectName; + + public String getUserName() + { + return userName; + } + + public void setUserName( String userName ) + { + this.userName = userName; + } + + public String getProjectName() + { + return projectName; + } + + public void setProjectName( String projectName ) + { + this.projectName = projectName; + } +} diff --git a/java/copr-cli/pom.xml b/java/copr-cli/pom.xml new file mode 100644 index 0000000..e39b652 --- /dev/null +++ b/java/copr-cli/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + org.fedoraproject.copr.client + copr-parent + 0.0.0-SNAPSHOT + + copr-cli + Copr Command Line Client + + + ${project.groupId} + copr-api + ${project.version} + + + ${project.groupId} + copr-impl + ${project.version} + runtime + + + com.beust + jcommander + 1.35 + + + org.ini4j + ini4j + 0.5.1 + + + diff --git a/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/ConfigurationLoader.java b/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/ConfigurationLoader.java new file mode 100644 index 0000000..8029d5b --- /dev/null +++ b/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/ConfigurationLoader.java @@ -0,0 +1,101 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.cli; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.fedoraproject.copr.client.CoprConfiguration; +import org.ini4j.Wini; + +/** + * @author Mikolaj Izdebski + */ +public class ConfigurationLoader +{ + private String getEvnDefault( String key, Object defaultValue ) + { + String value = System.getenv( key ); + if ( value == null || value.isEmpty() ) + return defaultValue.toString(); + + return value; + } + + private void addXdgBasePath( List configPaths, String location ) + { + Path base = Paths.get( location ); + if ( !base.isAbsolute() ) + return; + + Path path = base.resolve( "copr" ); + if ( Files.isRegularFile( path ) ) + configPaths.add( path ); + } + + public CoprConfiguration loadConfiguration( String customPath ) + { + List configPaths = new ArrayList<>(); + + if ( customPath != null ) + { + configPaths.add( Paths.get( customPath ) ); + } + else + { + Path xdgHome = Paths.get( getEvnDefault( "HOME", System.getProperty( "user.home" ) ) ); + addXdgBasePath( configPaths, getEvnDefault( "XDG_CONFIG_HOME", xdgHome.resolve( ".config" ) ) ); + + for ( String part : getEvnDefault( "XDG_CONFIG_DIRS", "/etc/xdg" ).split( ":" ) ) + addXdgBasePath( configPaths, part ); + + Collections.reverse( configPaths ); + } + + CoprConfiguration configuration = new CoprConfiguration(); + + for ( Path path : configPaths ) + { + try + { + Wini wini = new Wini( path.toFile() ); + + String url = wini.get( "copr-cli", "copr_url" ); + if ( url != null ) + configuration.setUrl( url ); + + String login = wini.get( "copr-cli", "login" ); + if ( login != null ) + configuration.setLogin( login ); + + String token = wini.get( "copr-cli", "token" ); + if ( token != null ) + configuration.setToken( token ); + } + catch ( IOException e ) + { + return null; + } + } + + return configuration; + } +} diff --git a/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/CoprCli.java b/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/CoprCli.java new file mode 100644 index 0000000..0a60f8f --- /dev/null +++ b/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/CoprCli.java @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.cli; + +import java.util.ArrayList; +import java.util.List; + +import org.fedoraproject.copr.client.CoprConfiguration; + +import com.beust.jcommander.Parameter; + +/** + * @author Mikolaj Izdebski + */ +public class CoprCli +{ + @Parameter + private final List parameters = new ArrayList(); + + @Parameter( names = { "-h", "--help" }, description = "Show generic help" ) + private boolean help; + + @Parameter( names = { "--help-commands" }, description = "Show help about subcommands" ) + private boolean helpCommands; + + @Parameter( names = { "-c", "--config" }, description = "Select configuration file to use" ) + private String config; + + public void run() + { + ConfigurationLoader loader = new ConfigurationLoader(); + CoprConfiguration configuration = loader.loadConfiguration( config ); + if ( configuration == null ) + { + System.err.println( "Unable to load Copr configuration" ); + System.exit( 1 ); + } + } +} diff --git a/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/Main.java b/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/Main.java new file mode 100644 index 0000000..221ecaa --- /dev/null +++ b/java/copr-cli/src/main/java/org/fedoraproject/copr/client/cli/Main.java @@ -0,0 +1,27 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.cli; + +/** + * @author Mikolaj Izdebski + */ +public class Main +{ + public static void main( String[] args ) + { + CoprCli cli = new CoprCli(); + } +} diff --git a/java/copr-impl/pom.xml b/java/copr-impl/pom.xml new file mode 100644 index 0000000..4626686 --- /dev/null +++ b/java/copr-impl/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.fedoraproject.copr.client + copr-parent + 0.0.0-SNAPSHOT + + copr-impl + + + ${project.groupId} + copr-api + ${project.version} + + + org.apache.httpcomponents + httpclient + 4.3.3 + + + com.google.code.gson + gson + 2.2.4 + + + junit + junit + 4.11 + test + + + org.easymock + easymock + 3.2 + test + + + org.apache.httpcomponents + httpclient + 4.3.3 + tests + test + + + Copr Implementation + diff --git a/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/BuildCommand.java b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/BuildCommand.java new file mode 100644 index 0000000..e509265 --- /dev/null +++ b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/BuildCommand.java @@ -0,0 +1,82 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.fedoraproject.copr.client.BuildRequest; +import org.fedoraproject.copr.client.BuildResult; + +import com.google.gson.JsonObject; + +/** + * @author Mikolaj Izdebski + */ +public class BuildCommand + extends RpcCommand +{ + private final BuildRequest request; + + public BuildCommand( BuildRequest request ) + { + this.request = request; + } + + @Override + protected String getCommandUrl() + { + return "/api/copr/" + request.getUserName() + "/" + request.getProjectName() + "/new_build/"; + } + + @Override + protected Map getExtraArguments() + { + Map map = new LinkedHashMap<>(); + + StringBuilder sb = new StringBuilder(); + + for ( String url : request.getSourceRpmList() ) + { + if ( sb.length() > 0 ) + sb.append( ' ' ); + sb.append( url ); + } + + map.put( "pkgs", sb.toString() ); + + if ( request.getMemory() != null ) + { + map.put( "memory", request.getMemory() ); + } + + if ( request.getTimeout() != null ) + { + map.put( "timeout", request.getTimeout() ); + } + + return map; + } + + @Override + protected BuildResult parseResponse( JsonObject response ) + { + long buildId = response.get( "id" ).getAsLong(); + String message = response.get( "message" ).getAsString(); + + return new DefaultBuildResult( buildId, message ); + } +} diff --git a/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultBuildResult.java b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultBuildResult.java new file mode 100644 index 0000000..4916118 --- /dev/null +++ b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultBuildResult.java @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import org.fedoraproject.copr.client.BuildResult; + +/** + * @author Mikolaj Izdebski + */ +public class DefaultBuildResult + implements BuildResult +{ + private final long buildId; + + private final String message; + + public DefaultBuildResult( long buildId, String message ) + { + this.buildId = buildId; + this.message = message; + } + + @Override + public long getBuildId() + { + return buildId; + } + + @Override + public String getMessage() + { + return message; + } +} diff --git a/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultCoprService.java b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultCoprService.java new file mode 100644 index 0000000..e6e6c70 --- /dev/null +++ b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultCoprService.java @@ -0,0 +1,33 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import org.fedoraproject.copr.client.CoprConfiguration; +import org.fedoraproject.copr.client.CoprService; +import org.fedoraproject.copr.client.CoprSession; + +/** + * @author Mikolaj Izdebski + */ +public class DefaultCoprService + implements CoprService +{ + @Override + public CoprSession newSession( CoprConfiguration configuration ) + { + return new DefaultCoprSession( configuration ); + } +} diff --git a/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultCoprSession.java b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultCoprSession.java new file mode 100644 index 0000000..6a6bb53 --- /dev/null +++ b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultCoprSession.java @@ -0,0 +1,75 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClients; +import org.fedoraproject.copr.client.BuildRequest; +import org.fedoraproject.copr.client.BuildResult; +import org.fedoraproject.copr.client.CoprConfiguration; +import org.fedoraproject.copr.client.CoprException; +import org.fedoraproject.copr.client.CoprSession; +import org.fedoraproject.copr.client.ListRequest; +import org.fedoraproject.copr.client.ListResult; + +/** + * @author Mikolaj Izdebski + */ +public class DefaultCoprSession + implements CoprSession +{ + private final CoprConfiguration configuration; + + private final HttpClient client; + + public DefaultCoprSession( CoprConfiguration configuration ) + { + this.configuration = configuration; + client = HttpClients.createDefault(); + } + + public HttpClient getClient() + { + return client; + } + + public CoprConfiguration getConfiguration() + { + return configuration; + } + + @Override + public void close() + throws CoprException + { + } + + @Override + public ListResult list( ListRequest request ) + throws CoprException + { + ListCommand command = new ListCommand( request ); + return command.execute( this ); + } + + @Override + public BuildResult build( BuildRequest request ) + throws CoprException + { + BuildCommand command = new BuildCommand( request ); + return command.execute( this ); + } +} diff --git a/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultListResult.java b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultListResult.java new file mode 100644 index 0000000..1c4f64b --- /dev/null +++ b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/DefaultListResult.java @@ -0,0 +1,41 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import java.util.Collections; +import java.util.List; + +import org.fedoraproject.copr.client.ListResult; +import org.fedoraproject.copr.client.ProjectId; + +/** + * @author Mikolaj Izdebski + */ +public class DefaultListResult + implements ListResult +{ + private final List projects; + + public DefaultListResult( List projects ) + { + this.projects = Collections.unmodifiableList( projects ); + } + + public List getProjects() + { + return projects; + } +} diff --git a/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/ListCommand.java b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/ListCommand.java new file mode 100644 index 0000000..0284869 --- /dev/null +++ b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/ListCommand.java @@ -0,0 +1,74 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.fedoraproject.copr.client.ListRequest; +import org.fedoraproject.copr.client.ListResult; +import org.fedoraproject.copr.client.ProjectId; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +/** + * @author Mikolaj Izdebski + */ +public class ListCommand + extends RpcCommand +{ + private final ListRequest request; + + public ListCommand( ListRequest request ) + { + this.request = request; + } + + @Override + protected String getCommandUrl() + { + return "/api/coprs/" + request.getUserName() + "/"; + } + + @Override + protected Map getExtraArguments() + { + return null; + } + + @Override + protected ListResult parseResponse( JsonObject response ) + { + List projects = new ArrayList<>(); + + JsonArray repos = response.get( "repos" ).getAsJsonArray(); + for ( JsonElement element : repos ) + { + JsonObject repo = element.getAsJsonObject(); + + ProjectId id = new ProjectId(); + id.setUserName( request.getUserName() ); + id.setProjectName( repo.get( "name" ).getAsString() ); + + projects.add( id ); + } + + return new DefaultListResult( projects ); + } +} diff --git a/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/RpcCommand.java b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/RpcCommand.java new file mode 100644 index 0000000..f75e208 --- /dev/null +++ b/java/copr-impl/src/main/java/org/fedoraproject/copr/client/impl/RpcCommand.java @@ -0,0 +1,111 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import static org.apache.http.entity.ContentType.APPLICATION_JSON; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.message.BasicNameValuePair; +import org.fedoraproject.copr.client.CoprException; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +/** + * @author Mikolaj Izdebski + */ +public abstract class RpcCommand +{ + protected abstract String getCommandUrl(); + + protected abstract Map getExtraArguments(); + + protected abstract T parseResponse( JsonObject response ); + + public T execute( DefaultCoprSession session ) + throws CoprException + { + try + { + HttpClient client = session.getClient(); + + String baseUrl = session.getConfiguration().getUrl(); + String commandUrl = getCommandUrl(); + String url = baseUrl + commandUrl; + + Map extraArgs = getExtraArguments(); + HttpUriRequest request; + if ( extraArgs == null ) + { + request = new HttpGet( url ); + } + else + { + HttpPost post = new HttpPost( url ); + request = post; + + List nameValuePairs = new ArrayList( 2 ); + for ( Entry entry : extraArgs.entrySet() ) + { + nameValuePairs.add( new BasicNameValuePair( entry.getKey(), entry.getValue() ) ); + } + post.setEntity( new UrlEncodedFormEntity( nameValuePairs ) ); + } + + request.addHeader( "Accept", APPLICATION_JSON.getMimeType() ); + + HttpResponse response = client.execute( request ); + int returnCode = response.getStatusLine().getStatusCode(); + + if ( returnCode != HttpURLConnection.HTTP_OK ) + { + throw new CoprException( "Copr RPC failed: HTTP " + returnCode + " " + + response.getStatusLine().getReasonPhrase() ); + } + + Reader responseReader = new InputStreamReader( response.getEntity().getContent() ); + JsonParser parser = new JsonParser(); + JsonObject rpcResponse = parser.parse( responseReader ).getAsJsonObject(); + + String rpcStatus = rpcResponse.get( "output" ).getAsString(); + if ( !rpcStatus.equals( "ok" ) ) + { + throw new CoprException( "Copr RPC returned failure reponse" ); + } + + return parseResponse( rpcResponse ); + } + catch ( IOException e ) + { + throw new CoprException( "Failed to call remote Copr procedure", e ); + } + } +} diff --git a/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/BuildTest.java b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/BuildTest.java new file mode 100644 index 0000000..a202f58 --- /dev/null +++ b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/BuildTest.java @@ -0,0 +1,73 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.easymock.EasyMockRunner; +import org.fedoraproject.copr.client.BuildRequest; +import org.fedoraproject.copr.client.BuildResult; +import org.fedoraproject.copr.client.CoprException; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( EasyMockRunner.class ) +public class BuildTest + extends RpcTest +{ + @Test + public void testBuild() + throws Exception + { + expect( mock.post( "/api/copr/john/toy/new_build/" ) ).andReturn( "build1" ); + replay( mock ); + + BuildRequest request = new BuildRequest( "john", "toy" ); + + BuildResult result = session.build( request ); + + verify( mock ); + assertEquals( 1, server.getAcceptedConnectionCount() ); + + assertEquals( "Build was added to log4j.", result.getMessage() ); + assertEquals( 5, result.getBuildId() ); + } + + @Test + public void testBuildFail() + throws Exception + { + expect( mock.post( "/api/copr/john/toy/new_build/" ) ).andReturn( "fail" ); + replay( mock ); + + BuildRequest request = new BuildRequest( "john", "toy" ); + + try + { + session.build( request ); + fail(); + } + catch ( CoprException e ) + { + verify( mock ); + assertEquals( 1, server.getAcceptedConnectionCount() ); + } + } +} diff --git a/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/HttpMock.java b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/HttpMock.java new file mode 100644 index 0000000..59480f0 --- /dev/null +++ b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/HttpMock.java @@ -0,0 +1,88 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR; +import static org.apache.http.HttpStatus.SC_NOT_FOUND; +import static org.apache.http.HttpStatus.SC_OK; +import static org.apache.http.entity.ContentType.APPLICATION_JSON; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.entity.FileEntity; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpRequestHandler; + +/** + * @author Mikolaj Izdebski + */ +public class HttpMock + implements HttpRequestHandler +{ + private final RpcTest test; + + public HttpMock( RpcTest test ) + { + this.test = test; + } + + @Override + public void handle( HttpRequest request, HttpResponse response, HttpContext context ) + throws HttpException, IOException + { + String uri = request.getRequestLine().getUri(); + String resourceName; + + try + { + String method = request.getRequestLine().getMethod(); + if ( method.equals( "GET" ) ) + { + resourceName = test.getMock().get( uri ); + } + else if ( method.equals( "POST" ) ) + { + resourceName = test.getMock().post( uri ); + } + else + { + fail( "Unexpected method: " + method ); + throw new RuntimeException(); + } + + if ( resourceName == null ) + { + response.setStatusCode( SC_NOT_FOUND ); + } + else + { + Path resourcePath = Paths.get( "src/test/resources" ).resolve( resourceName + ".json" ); + response.setStatusCode( SC_OK ); + response.setEntity( new FileEntity( resourcePath.toFile(), APPLICATION_JSON ) ); + } + } + catch ( Exception e ) + { + response.setStatusCode( SC_INTERNAL_SERVER_ERROR ); + } + } +} diff --git a/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/ListTest.java b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/ListTest.java new file mode 100644 index 0000000..6fb0cbb --- /dev/null +++ b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/ListTest.java @@ -0,0 +1,141 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Iterator; +import java.util.List; + +import org.easymock.EasyMockRunner; +import org.fedoraproject.copr.client.CoprException; +import org.fedoraproject.copr.client.ListRequest; +import org.fedoraproject.copr.client.ListResult; +import org.fedoraproject.copr.client.ProjectId; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( EasyMockRunner.class ) +public class ListTest + extends RpcTest +{ + @Test + public void testApiFailure() + throws Exception + { + expect( mock.get( "/api/coprs/jdaniels/" ) ).andReturn( "fail" ); + replay( mock ); + + ListRequest request = new ListRequest( "jdaniels" ); + + try + { + session.list( request ); + fail(); + } + catch ( CoprException e ) + { + verify( mock ); + assertEquals( 1, server.getAcceptedConnectionCount() ); + } + } + + @Test + public void test404() + throws Exception + { + expect( mock.get( "/api/coprs/jdaniels/" ) ).andReturn( null ); + replay( mock ); + + ListRequest request = new ListRequest( "jdaniels" ); + + try + { + session.list( request ); + fail(); + } + catch ( CoprException e ) + { + verify( mock ); + assertEquals( 1, server.getAcceptedConnectionCount() ); + + assertTrue( e.getMessage().contains( "404 Not Found" ) ); + } + } + + @Test + public void testSingleRepo() + throws Exception + { + expect( mock.get( "/api/coprs/jdaniels/" ) ).andReturn( "list1" ); + replay( mock ); + + ListRequest request = new ListRequest( "jdaniels" ); + ListResult result = session.list( request ); + + verify( mock ); + assertEquals( 1, server.getAcceptedConnectionCount() ); + + assertNotNull( result ); + List projects = result.getProjects(); + + assertNotNull( projects ); + assertEquals( 1, projects.size() ); + + ProjectId project = projects.iterator().next(); + assertEquals( "jdaniels", project.getUserName() ); + assertEquals( "log4j", project.getProjectName() ); + } + + @Test + public void testMultipleRepos() + throws Exception + { + expect( mock.get( "/api/coprs/johndoe/" ) ).andReturn( "list2" ); + replay( mock ); + + ListRequest request = new ListRequest( "johndoe" ); + ListResult result = session.list( request ); + + verify( mock ); + assertEquals( 1, server.getAcceptedConnectionCount() ); + + assertNotNull( result ); + List projects = result.getProjects(); + + assertNotNull( projects ); + assertEquals( 3, projects.size() ); + Iterator projectIterator = projects.iterator(); + + ProjectId project1 = projectIterator.next(); + assertEquals( "johndoe", project1.getUserName() ); + assertEquals( "log4j", project1.getProjectName() ); + + ProjectId project2 = projectIterator.next(); + assertEquals( "johndoe", project2.getUserName() ); + assertEquals( "xyzzy", project2.getProjectName() ); + + ProjectId project3 = projectIterator.next(); + assertEquals( "johndoe", project3.getUserName() ); + assertEquals( "my-fancy-repo", project3.getProjectName() ); + } +} diff --git a/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/RpcMock.java b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/RpcMock.java new file mode 100644 index 0000000..a1321e4 --- /dev/null +++ b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/RpcMock.java @@ -0,0 +1,26 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +/** + * @author Mikolaj Izdebski + */ +public interface RpcMock +{ + String get( String uri ); + + String post( String uri, String... args ); +} diff --git a/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/RpcTest.java b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/RpcTest.java new file mode 100644 index 0000000..2cf5abd --- /dev/null +++ b/java/copr-impl/src/test/java/org/fedoraproject/copr/client/impl/RpcTest.java @@ -0,0 +1,74 @@ +/*- + * Copyright (c) 2014 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.copr.client.impl; + +import java.net.InetSocketAddress; + +import org.apache.http.localserver.LocalTestServer; +import org.easymock.Mock; +import org.easymock.MockType; +import org.fedoraproject.copr.client.CoprConfiguration; +import org.fedoraproject.copr.client.CoprService; +import org.fedoraproject.copr.client.CoprSession; +import org.fedoraproject.copr.client.impl.DefaultCoprService; +import org.junit.After; +import org.junit.Before; + +/** + * @author Mikolaj Izdebski + */ +public abstract class RpcTest +{ + protected LocalTestServer server; + + protected String url; + + protected CoprSession session; + + @Mock( type = MockType.STRICT ) + protected RpcMock mock; + + public RpcMock getMock() + { + return mock; + } + + @Before + public void setUp() + throws Exception + { + server = new LocalTestServer( null, null ); + server.register( "/api/*", new HttpMock( this ) ); + server.start(); + + InetSocketAddress address = server.getServiceAddress(); + url = "http://" + address.getHostName() + ":" + address.getPort(); + + CoprConfiguration configuration = new CoprConfiguration(); + configuration.setUrl( url ); + + CoprService copr = new DefaultCoprService(); + + session = copr.newSession( configuration ); + } + + @After + public void tearDown() + throws Exception + { + server.stop(); + } +} diff --git a/java/copr-impl/src/test/resources/build1.json b/java/copr-impl/src/test/resources/build1.json new file mode 100644 index 0000000..9657de8 --- /dev/null +++ b/java/copr-impl/src/test/resources/build1.json @@ -0,0 +1,5 @@ + { + "output": "ok", + "message": "Build was added to log4j.", + "id": 5 + } diff --git a/java/copr-impl/src/test/resources/fail.json b/java/copr-impl/src/test/resources/fail.json new file mode 100644 index 0000000..5ea7f3b --- /dev/null +++ b/java/copr-impl/src/test/resources/fail.json @@ -0,0 +1 @@ +{"output":"notok"} diff --git a/java/copr-impl/src/test/resources/list1.json b/java/copr-impl/src/test/resources/list1.json new file mode 100644 index 0000000..d211bc3 --- /dev/null +++ b/java/copr-impl/src/test/resources/list1.json @@ -0,0 +1,15 @@ + { + "output": "ok", + "repos": [ + { + "yum_repos": { + "fedora-19-i686": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-i686/", + "fedora-19-x86_64": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-x86_64/" + }, + "additional_repos": "", + "instructions": "", + "name": "log4j", + "description": "Java logging package" + } + ] + } diff --git a/java/copr-impl/src/test/resources/list2.json b/java/copr-impl/src/test/resources/list2.json new file mode 100644 index 0000000..aae875d --- /dev/null +++ b/java/copr-impl/src/test/resources/list2.json @@ -0,0 +1,33 @@ + { + "output": "ok", + "repos": [ + { + "yum_repos": { + "fedora-19-i686": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-i686/", + "fedora-19-x86_64": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-x86_64/" + }, + "additional_repos": "", + "instructions": "", + "name": "log4j", + "description": "Java logging package" + }, { + "yum_repos": { + "fedora-19-i686": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-i686/", + "fedora-19-x86_64": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-x86_64/" + }, + "additional_repos": "", + "instructions": "", + "name": "xyzzy", + "description": "Java logging package" + }, { + "yum_repos": { + "fedora-19-i686": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-i686/", + "fedora-19-x86_64": "https://copr-be.cloud.fedoraproject.org/results/jdaniels/log4j/fedora-19-x86_64/" + }, + "additional_repos": "", + "instructions": "", + "name": "my-fancy-repo", + "description": "Java logging package" + } + ] + } diff --git a/java/pom.xml b/java/pom.xml new file mode 100644 index 0000000..0788149 --- /dev/null +++ b/java/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + org.fedoraproject.copr.client + copr-parent + 0.0.0-SNAPSHOT + pom + Copr Client Parent POM + + copr-api + copr-impl + copr-cli + + + + + maven-compiler-plugin + + 1.7 + 1.7 + + + + + + Red Hat, Inc. + http://www.redhat.com/ + + + Bugzilla + https://bugzilla.redhat.com/ + +