Commit c51b8241 authored by Aurelien Pupier's avatar Aurelien Pupier
Browse files

BS-14288: Use Web rest API to delete a process definition in order to

clean client side
parent 174a7ddb
......@@ -14,14 +14,22 @@
*/
package org.bonitasoft.studio.engine.operation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.process.builders.PoolBuilder.aPool;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.net.HttpURLConnection;
import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.studio.engine.BOSEngineManager;
......@@ -30,6 +38,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
......@@ -39,6 +48,8 @@ public class UndeployProcessOperationTest {
@Mock
private ProcessAPI processAPI;
@Mock
private HttpURLConnection connection;
@Test
public void should_disable_process_definition_before_deleting_instances_and_definition() throws Exception {
......@@ -51,7 +62,12 @@ public class UndeployProcessOperationTest {
inOrder.verify(processAPI).disableProcess(1L);
inOrder.verify(processAPI).deleteProcessInstances(1L, 0, 1000);
inOrder.verify(processAPI).deleteArchivedProcessInstances(1L, 0, 1000);
inOrder.verify(processAPI).deleteProcessDefinition(1L);
inOrder.verify(processAPI, never()).deleteProcessDefinition(1L);
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(operation, times(2)).openConnection(captor.capture());
assertThat(captor.getAllValues())
.contains("http://localhost:8080/bonita/bonita/API/bpm/process/1", "http://localhost:8080/bonita/bonita/logoutservice");
}
private UndeployProcessOperation createFixture() throws Exception {
......@@ -59,6 +75,11 @@ public class UndeployProcessOperationTest {
when(engineManager.createSession(notNull(AbstractProcess.class), anyString(), any(IProgressMonitor.class))).thenReturn(mock(APISession.class));
when(engineManager.getProcessAPI(notNull(APISession.class))).thenReturn(processAPI);
when(processAPI.getNumberOfProcessDeploymentInfos()).thenReturn(1L);
return new UndeployProcessOperation(engineManager);
final UndeployProcessOperation operation = spy(new UndeployProcessOperation(engineManager));
doReturn(connection).when(operation).openLoginConnection(any(IProgressMonitor.class));
doReturn(connection).when(operation).openConnection(anyString());
doReturn(HttpURLConnection.HTTP_OK).when(connection).getResponseCode();
doReturn("http://localhost:8080/bonita").when(operation).getUrlBase();
return operation;
}
}
......@@ -473,11 +473,15 @@ public class BOSWebServerManager {
}
}
public String generateLoginURL(final String username, final String password) {
public String generateUrlBase() {
final IPreferenceStore store = BonitaStudioPreferencesPlugin.getDefault().getPreferenceStore();
final String port = store.getString(BonitaPreferenceConstants.CONSOLE_PORT);
final String host = store.getString(BonitaPreferenceConstants.CONSOLE_HOST);
return "http://" + host + ":" + port + LOGINSERVICE_PATH + "username=" + username + "&password=" + password;
return "http://" + host + ":" + port;
}
public String generateLoginURL(final String username, final String password) {
return generateUrlBase() + LOGINSERVICE_PATH + "username=" + username + "&password=" + password;
}
public void cleanBeforeShutdown() {
......
......@@ -64,6 +64,12 @@ public abstract class AbstractBonitaURLBuilder {
public URL toURL(final IProgressMonitor monitor) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException {
final String locale = getWebLocale();
final String loginURL = buildLoginUrl();
return new URL(loginURL + "&redirectUrl="
+ URLEncoder.encode(getRedirectURL(locale, monitor), ENCODING_UTF8));
}
protected String buildLoginUrl() {
String userName = getDefaultUsername();
String password = getDefaultPassword();
......@@ -73,9 +79,7 @@ public abstract class AbstractBonitaURLBuilder {
password = conf.getPassword();
}
final String loginURL = buildLoginUrl(userName, password);
return new URL(loginURL + "&redirectUrl="
+ URLEncoder.encode(getRedirectURL(locale, monitor), ENCODING_UTF8));
return buildLoginUrl(userName, password);
}
protected abstract Configuration getConfiguration();
......
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 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 Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.engine.operation;
import java.io.UnsupportedEncodingException;
import org.bonitasoft.studio.model.configuration.Configuration;
import org.eclipse.core.runtime.IProgressMonitor;
public class LoginUrlBuilder extends AbstractBonitaURLBuilder {
@Override
protected String getRedirectURL(final String locale, final IProgressMonitor monitor) throws UnsupportedEncodingException {
return "";
}
@Override
protected String getLocaleParameter(final String locale) {
return "";
}
@Override
protected Configuration getConfiguration() {
return null;
}
}
......@@ -15,6 +15,16 @@
package org.bonitasoft.studio.engine.operation;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
......@@ -26,6 +36,7 @@ import org.bonitasoft.engine.exception.ProcessInstanceHierarchicalDeletionExcept
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.engine.BOSEngineManager;
import org.bonitasoft.studio.engine.BOSWebServerManager;
import org.bonitasoft.studio.engine.EnginePlugin;
import org.bonitasoft.studio.engine.i18n.Messages;
import org.bonitasoft.studio.model.process.AbstractProcess;
......@@ -41,12 +52,13 @@ import org.eclipse.core.runtime.Status;
*/
public class UndeployProcessOperation {
private static final String API_LOGOUT = "/bonita/logoutservice";
private static final String API_PROCESS_RESOURCE = "/bonita/API/bpm/process/";
private static final String HTTP_METHOD_DELETE = "DELETE";
private static final String HTTP_METHOD_POST = "POST";
private static final int MAX_RESULTS = 1000;
private String configurationId;
private final List<AbstractProcess> processes = new ArrayList<AbstractProcess>();
private final BOSEngineManager engineManager;
public UndeployProcessOperation(final BOSEngineManager engineManager) {
......@@ -104,9 +116,56 @@ public class UndeployProcessOperation {
private void deleteProcessDefinition(final AbstractProcess process, final ProcessAPI processApi, final long processDefinitionId,
final IProgressMonitor monitor)
throws DeletionException {
throws DeletionException, URISyntaxException, IOException {
monitor.subTask(Messages.bind(Messages.deletingProcessDefinition, getProcessLabel(process)));
processApi.deleteProcessDefinition(processDefinitionId);
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
logToSession(monitor);
try {
deleteProcessDefinition(processApi, processDefinitionId);
} finally {
logoutFromSession();
}
}
protected void deleteProcessDefinition(final ProcessAPI processApi, final long processDefinitionId) throws IOException, MalformedURLException,
ProtocolException, DeletionException {
final HttpURLConnection deleteConnection = openConnection(getUrlBase() + API_PROCESS_RESOURCE + processDefinitionId);
deleteConnection.setRequestMethod(HTTP_METHOD_DELETE);
if (HttpURLConnection.HTTP_OK != deleteConnection.getResponseCode()) {
processApi.deleteProcessDefinition(processDefinitionId);
}
deleteConnection.disconnect();
}
protected HttpURLConnection openConnection(final String url) throws IOException, MalformedURLException {
return (HttpURLConnection) new URL(url).openConnection();
}
protected String getUrlBase() {
return BOSWebServerManager.getInstance().generateUrlBase();
}
protected void logoutFromSession() throws IOException, MalformedURLException, ProtocolException {
final HttpURLConnection logoutConnection = openConnection(getUrlBase() + API_LOGOUT);
logoutConnection.setRequestMethod(HTTP_METHOD_POST);
if (HttpURLConnection.HTTP_OK != logoutConnection.getResponseCode()) {
BonitaStudioLog.error("Cannot unlog to session " + logoutConnection.getResponseCode(), EnginePlugin.PLUGIN_ID);
}
}
protected void logToSession(final IProgressMonitor monitor) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException, IOException {
final HttpURLConnection connection = openLoginConnection(monitor);
if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
BonitaStudioLog.error("Cannot log to session " + connection.getResponseCode(), EnginePlugin.PLUGIN_ID);
}
connection.disconnect();
}
protected HttpURLConnection openLoginConnection(final IProgressMonitor monitor) throws MalformedURLException, UnsupportedEncodingException,
URISyntaxException, IOException {
final URL loginUrl = new LoginUrlBuilder().toURL(monitor);
final HttpURLConnection connection = (HttpURLConnection) loginUrl.openConnection();
return connection;
}
private void deleteArchivedProcessInstances(final ProcessAPI processApi, final long processDefinitionId) throws DeletionException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment