2014-03-10 22:34:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
2018-10-05 09:17:12 +02:00
|
|
|
*
|
2014-03-10 22:34:36 +01:00
|
|
|
* 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
|
2018-10-05 09:17:12 +02:00
|
|
|
*
|
2014-03-10 22:34:36 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-10-05 09:17:12 +02:00
|
|
|
*
|
2014-03-10 22:34:36 +01:00
|
|
|
* 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 li.strolch.command;
|
|
|
|
|
2018-02-09 22:16:34 +01:00
|
|
|
import static li.strolch.service.test.AbstractRealmServiceTest.*;
|
2020-08-19 10:33:47 +02:00
|
|
|
import static org.hamcrest.MatcherAssert.assertThat;
|
2016-08-05 20:24:23 +02:00
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2014-03-10 22:34:36 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import li.strolch.agent.api.ComponentContainer;
|
|
|
|
import li.strolch.agent.api.StrolchRealm;
|
|
|
|
import li.strolch.persistence.api.StrolchTransaction;
|
2016-06-26 11:38:41 +02:00
|
|
|
import li.strolch.privilege.model.Certificate;
|
2014-03-10 22:34:36 +01:00
|
|
|
import li.strolch.service.api.Command;
|
|
|
|
import li.strolch.service.api.ServiceHandler;
|
|
|
|
import li.strolch.testbase.runtime.RuntimeMock;
|
2018-02-09 22:16:34 +01:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2014-03-10 22:34:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
|
|
|
*/
|
|
|
|
public abstract class AbstractRealmCommandTest {
|
|
|
|
|
|
|
|
protected static RuntimeMock runtimeMock;
|
|
|
|
|
2014-08-23 20:50:21 +02:00
|
|
|
protected static Certificate certificate;
|
|
|
|
|
2017-01-31 14:20:47 +01:00
|
|
|
protected String getUsername() {
|
|
|
|
return "test";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void beforeClass() throws Exception {
|
2014-03-10 22:34:36 +01:00
|
|
|
|
2022-03-08 09:51:39 +01:00
|
|
|
dropSchema(AbstractRealmCommandTest.class.getSimpleName(), "jdbc:postgresql://localhost/cacheduserdb",
|
|
|
|
"cacheduser", "test");
|
2014-03-10 22:34:36 +01:00
|
|
|
|
|
|
|
File rootPath = new File(RUNTIME_PATH);
|
|
|
|
File configSrc = new File(CONFIG_SRC);
|
|
|
|
runtimeMock = new RuntimeMock();
|
|
|
|
runtimeMock.mockRuntime(rootPath, configSrc);
|
|
|
|
runtimeMock.startContainer();
|
|
|
|
|
2017-05-22 14:37:48 +02:00
|
|
|
certificate = runtimeMock.getPrivilegeHandler().authenticate(getUsername(), getUsername().toCharArray());
|
2014-08-23 20:50:21 +02:00
|
|
|
importFromXml(REALM_CACHED, certificate, getServiceHandler());
|
2014-03-10 22:34:36 +01:00
|
|
|
}
|
|
|
|
|
2017-01-31 14:20:47 +01:00
|
|
|
@After
|
|
|
|
public void afterClass() {
|
2014-11-14 00:07:53 +01:00
|
|
|
if (runtimeMock != null)
|
|
|
|
runtimeMock.destroyRuntime();
|
2014-03-10 22:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ServiceHandler getServiceHandler() {
|
|
|
|
return runtimeMock.getContainer().getComponent(ServiceHandler.class);
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:00:02 +01:00
|
|
|
protected abstract Command getCommandInstance(StrolchTransaction tx);
|
2014-03-10 22:34:36 +01:00
|
|
|
|
2021-02-22 16:00:02 +01:00
|
|
|
protected abstract void validateAfterCommand(StrolchTransaction tx);
|
2016-08-05 20:24:23 +02:00
|
|
|
|
2021-02-22 16:00:02 +01:00
|
|
|
protected abstract void validateAfterCommandFailed(StrolchTransaction tx);
|
2014-03-10 22:34:36 +01:00
|
|
|
|
2016-08-05 20:24:23 +02:00
|
|
|
protected void doCommandAsFail(String realmName) {
|
2014-03-10 22:34:36 +01:00
|
|
|
StrolchRealm realm = runtimeMock.getContainer().getRealm(realmName);
|
2016-08-05 20:24:23 +02:00
|
|
|
boolean caught = false;
|
2019-04-01 16:31:53 +02:00
|
|
|
try (StrolchTransaction tx = realm.openTx(certificate, "test", false)) {
|
2021-02-22 16:00:02 +01:00
|
|
|
Command command = getCommandInstance(tx);
|
2014-03-10 22:34:36 +01:00
|
|
|
FailCommandFacade commandFacade = new FailCommandFacade(runtimeMock.getContainer(), tx, command);
|
2015-02-03 23:18:31 +01:00
|
|
|
|
2014-03-10 22:34:36 +01:00
|
|
|
tx.addCommand(commandFacade);
|
2015-02-03 23:18:31 +01:00
|
|
|
tx.commitOnClose();
|
2016-08-05 20:24:23 +02:00
|
|
|
} catch (RuntimeException e) {
|
|
|
|
assertThat(e.getMessage(), containsString("Fail on purpose after do command!"));
|
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
assertTrue(caught);
|
|
|
|
|
2019-04-01 16:31:53 +02:00
|
|
|
try (StrolchTransaction tx = realm.openTx(certificate, "test", true)) {
|
2021-02-22 16:00:02 +01:00
|
|
|
validateAfterCommandFailed(tx);
|
2014-03-10 22:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void doCommand(String realmName) {
|
|
|
|
StrolchRealm realm = runtimeMock.getContainer().getRealm(realmName);
|
2019-04-01 16:31:53 +02:00
|
|
|
try (StrolchTransaction tx = realm.openTx(certificate, "test", false)) {
|
2021-02-22 16:00:02 +01:00
|
|
|
Command command = getCommandInstance(tx);
|
2014-03-10 22:34:36 +01:00
|
|
|
tx.addCommand(command);
|
2015-02-03 23:18:31 +01:00
|
|
|
tx.commitOnClose();
|
2014-03-10 22:34:36 +01:00
|
|
|
}
|
2016-08-05 20:24:23 +02:00
|
|
|
|
2019-04-01 16:31:53 +02:00
|
|
|
try (StrolchTransaction tx = realm.openTx(certificate, "test", true)) {
|
2021-02-22 16:00:02 +01:00
|
|
|
validateAfterCommand(tx);
|
2016-08-05 20:24:23 +02:00
|
|
|
}
|
2014-03-10 22:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-08-05 20:24:23 +02:00
|
|
|
public void shouldDoCommandTransient() {
|
2014-03-10 22:34:36 +01:00
|
|
|
doCommandAsFail(REALM_TRANSIENT);
|
2016-08-05 20:24:23 +02:00
|
|
|
doCommand(REALM_TRANSIENT);
|
2014-03-10 22:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-08-05 20:24:23 +02:00
|
|
|
public void shouldDoCommandCached() {
|
2014-03-10 22:34:36 +01:00
|
|
|
doCommandAsFail(REALM_CACHED);
|
2016-08-05 20:24:23 +02:00
|
|
|
doCommand(REALM_CACHED);
|
2014-03-10 22:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class FailCommandFacade extends Command {
|
|
|
|
|
|
|
|
private Command command;
|
|
|
|
|
|
|
|
/**
|
2022-03-08 09:42:54 +01:00
|
|
|
*
|
2014-03-10 22:34:36 +01:00
|
|
|
*/
|
|
|
|
public FailCommandFacade(ComponentContainer container, StrolchTransaction tx, Command command) {
|
2021-02-22 16:00:02 +01:00
|
|
|
super(tx);
|
2014-03-10 22:34:36 +01:00
|
|
|
this.command = command;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void validate() {
|
|
|
|
this.command.validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doCommand() {
|
|
|
|
this.command.doCommand();
|
|
|
|
throw new RuntimeException("Fail on purpose after do command!");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void undo() {
|
|
|
|
this.command.undo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|