001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.wsclient.qualitygate.internal;
021
022 import org.json.simple.JSONValue;
023 import org.sonar.wsclient.internal.HttpRequestFactory;
024 import org.sonar.wsclient.qualitygate.*;
025
026 import java.util.*;
027
028 public class DefaultQualityGateClient implements QualityGateClient {
029
030 private static final String ROOT_URL = "/api/qualitygates";
031 private static final String LIST_URL = ROOT_URL + "/list";
032 private static final String SHOW_URL = ROOT_URL + "/show";
033 private static final String CREATE_URL = ROOT_URL + "/create";
034 private static final String CREATE_CONDITION_URL = ROOT_URL + "/create_condition";
035 private static final String UPDATE_CONDITION_URL = ROOT_URL + "/update_condition";
036 private static final String DELETE_CONDITION_URL = ROOT_URL + "/delete_condition";
037 private static final String RENAME_URL = ROOT_URL + "/rename";
038 private static final String DESTROY_URL = ROOT_URL + "/destroy";
039 private static final String SET_DEFAULT_URL = ROOT_URL + "/set_as_default";
040 private static final String UNSET_DEFAULT_URL = ROOT_URL + "/unset_default";
041 private static final String SELECT_URL = ROOT_URL + "/select";
042 private static final String DESELECT_URL = ROOT_URL + "/deselect";
043
044 private final HttpRequestFactory requestFactory;
045
046 public DefaultQualityGateClient(HttpRequestFactory requestFactory) {
047 this.requestFactory = requestFactory;
048 }
049
050 @Override
051 public QualityGates list() {
052 String json = requestFactory.get(LIST_URL, Collections.<String, Object> emptyMap());
053 return jsonToQualityGates(json);
054 }
055
056 @Override
057 public QualityGate create(String qGateName) {
058 String json = requestFactory.post(CREATE_URL, Collections.singletonMap("name", (Object) qGateName));
059 return jsonToQualityGate(json);
060 }
061
062 @Override
063 public QualityGate rename(long qGateId, String qGateName) {
064 Map<String, Object> params = new HashMap<String, Object>();
065 params.put("id", qGateId);
066 params.put("name", qGateName);
067 String json = requestFactory.post(RENAME_URL, params);
068 return jsonToQualityGate(json);
069 }
070
071 @Override
072 public QualityGateDetails show(long qGateId) {
073 String json = requestFactory.get(SHOW_URL, Collections.singletonMap("id", (Object) qGateId));
074 return jsonToDetails(json);
075 }
076
077 @Override
078 public QualityGateDetails show(String qGateName) {
079 String json = requestFactory.get(SHOW_URL, Collections.singletonMap("name", (Object) qGateName));
080 return jsonToDetails(json);
081 }
082
083 @Override
084 public QualityGateCondition createCondition(NewCondition condition) {
085 String json = requestFactory.post(CREATE_CONDITION_URL, condition.urlParams());
086 return jsonToCondition(json);
087 }
088
089 @Override
090 public QualityGateCondition updateCondition(UpdateCondition condition) {
091 String json = requestFactory.post(UPDATE_CONDITION_URL, condition.urlParams());
092 return jsonToCondition(json);
093 }
094
095 @Override
096 public void deleteCondition(long conditionId) {
097 requestFactory.post(DELETE_CONDITION_URL, Collections.singletonMap("id", (Object) conditionId));
098 }
099
100 @Override
101 public void destroy(long qGateId) {
102 requestFactory.post(DESTROY_URL, Collections.singletonMap("id", (Object) qGateId));
103 }
104
105 @Override
106 public void setDefault(long qGateId) {
107 requestFactory.post(SET_DEFAULT_URL, Collections.singletonMap("id", (Object) qGateId));
108 }
109
110 @Override
111 public void unsetDefault() {
112 requestFactory.post(UNSET_DEFAULT_URL, Collections.<String, Object> emptyMap());
113 }
114
115 @Override
116 public void selectProject(long qGateId, long projectId) {
117 requestFactory.post(SELECT_URL, selectionParams(qGateId, projectId));
118 }
119
120 @Override
121 public void deselectProject(long qGateId, long projectId) {
122 requestFactory.post(DESELECT_URL, selectionParams(qGateId, projectId));
123 }
124
125 private Map<String, Object> selectionParams(long qGateId, long projectId) {
126 Map<String, Object> params = new HashMap<String, Object>();
127 params.put("gateId", Long.toString(qGateId));
128 params.put("projectId", Long.toString(projectId));
129 return params;
130 }
131
132
133 @SuppressWarnings({"rawtypes", "unchecked"})
134 private QualityGate jsonToQualityGate(String json) {
135 Map jsonRoot = (Map) JSONValue.parse(json);
136 return new DefaultQualityGate((Map) jsonRoot);
137 }
138
139 @SuppressWarnings({"rawtypes", "unchecked"})
140 private QualityGates jsonToQualityGates(String json) {
141 Map jsonRoot = (Map) JSONValue.parse(json);
142 return new DefaultQualityGates((Map) jsonRoot);
143 }
144
145 @SuppressWarnings({"rawtypes", "unchecked"})
146 private QualityGateDetails jsonToDetails(String json) {
147 Map jsonRoot = (Map) JSONValue.parse(json);
148 return new DefaultQualityGateDetails((Map) jsonRoot, jsonToConditions(json));
149 }
150
151 @SuppressWarnings({"rawtypes", "unchecked"})
152 private Collection<QualityGateCondition> jsonToConditions(String json) {
153 Map jsonRoot = (Map) JSONValue.parse(json);
154 Collection<Map> conditionArray = (Collection<Map>) jsonRoot.get("conditions");
155 Collection<QualityGateCondition> conditions = new ArrayList<QualityGateCondition>();
156 if (conditionArray != null) {
157 for (Map conditionJson: conditionArray) {
158 conditions.add(new DefaultQualityGateCondition(conditionJson));
159 }
160 }
161 return conditions;
162 }
163
164 @SuppressWarnings({"rawtypes", "unchecked"})
165 private DefaultQualityGateCondition jsonToCondition(String json) {
166 return new DefaultQualityGateCondition((Map) JSONValue.parse(json));
167 }
168 }