diff options
author | Jorge Hernandez <jorge.hernandez-herrero@att.com> | 2019-03-28 01:12:10 -0500 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2019-04-02 20:48:41 -0500 |
commit | 964b127fc75e202d3c661454dbea58acf2b234c6 (patch) | |
tree | c2991183110e1ba8ec0905350eb2256d5f3c1cec /feature-lifecycle/src/test/java | |
parent | 92cb38f9137d80328445d7b22cb51716783e3aea (diff) |
Initial lifecycle state machine.
Basic state machine and initial functionality to
transition to the Active state via Engine commands and
StateUpdate messages.
Change-Id: I8212bd264dbbea446d4e682918f9360e3984887f
Issue-ID: POLICY-1608
Signed-off-by: Jorge Hernandez <jorge.hernandez-herrero@att.com>
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'feature-lifecycle/src/test/java')
6 files changed, 802 insertions, 0 deletions
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java new file mode 100644 index 00000000..eb83bc61 --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java @@ -0,0 +1,191 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * =============LICENSE_END======================================================== + */ + +package org.onap.policy.drools.lifecycle; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.utils.logging.LoggerUtil; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.enums.PdpMessageType; +import org.onap.policy.models.pdp.enums.PdpState; + +/** + * Lifecycle State Active Test. + */ +public class LifecycleStateActiveTest { + + private LifecycleFsm fsm; + + @BeforeClass + public static void setUp() { + SystemPersistence.manager.setConfigurationDir("src/test/resources"); + LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN"); + } + + @AfterClass + public static void tearDown() { + SystemPersistence.manager.setConfigurationDir(null); + } + + /** + * Start tests in the Active state. + */ + @Before + public void startActive() throws CoderException { + fsm = new LifecycleFsm(); + fsm.setStatusTimerSeconds(15); + assertTrue(fsm.start()); + + goActive(); + assertActive(); + } + + private void goActive() throws CoderException { + PdpStateChange change = new PdpStateChange(); + change.setPdpGroup("A"); + change.setPdpSubgroup("a"); + change.setState(PdpState.ACTIVE); + change.setName("test"); + + fsm.source.offer(new StandardCoder().encode(change)); + } + + @Test + public void constructor() { + assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStateActive(null)); + fsm.shutdown(); + } + + @Test + public void start() { + assertActive(); + assertFalse(fsm.start()); + assertActive(); + + fsm.shutdown(); + } + + private void assertActive() { + assertEquals(PdpState.ACTIVE, fsm.state()); + assertEquals("A", fsm.getPdpGroup()); + assertEquals("a", fsm.getPdpSubgroup()); + assertTrue(fsm.isAlive()); + await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE)); + } + + @Test + public void stop() { + assertTrue(fsm.stop()); + assertBasicTerminated(); + + fsm.shutdown(); + } + + private void assertBasicTerminated() { + assertEquals(PdpState.TERMINATED, fsm.state()); + assertFalse(fsm.isAlive()); + assertFalse(fsm.state.isAlive()); + await().atMost(1, TimeUnit.SECONDS).until(isStatus(PdpState.TERMINATED)); + } + + @Test + public void shutdown() { + fsm.shutdown(); + + assertBasicTerminated(); + + assertTrue(fsm.statusTask.isCancelled()); + assertTrue(fsm.statusTask.isDone()); + } + + private Callable<Boolean> isStatus(PdpState state) { + return () -> { + if (fsm.client.getSink().getRecentEvents().length == 0) { + return false; + } + + List<String> events = Arrays.asList(fsm.client.getSink().getRecentEvents()); + PdpStatus status = + new StandardCoder().decode(events.get(events.size() - 1), PdpStatus.class); + + return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState(); + }; + } + + @Test + public void status() { + await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE)); + int preCount = fsm.client.getSink().getRecentEvents().length; + + assertTrue(fsm.status()); + assertEquals(preCount + 1, fsm.client.getSink().getRecentEvents().length); + + fsm.shutdown(); + } + + @Test + public void stateChange() throws CoderException { + assertActive(); + + /* dup */ + PdpStateChange change = new PdpStateChange(); + change.setPdpGroup("B"); + change.setPdpSubgroup("b"); + change.setState(PdpState.ACTIVE); + change.setName("test"); + + fsm.source.offer(new StandardCoder().encode(change)); + assertEquals(PdpState.ACTIVE, fsm.state()); + assertEquals("B", fsm.getPdpGroup()); + assertEquals("b", fsm.getPdpSubgroup()); + + change.setState(PdpState.SAFE); + fsm.source.offer(new StandardCoder().encode(change)); + assertEquals(PdpState.ACTIVE, fsm.state()); + + change.setState(PdpState.TERMINATED); + fsm.source.offer(new StandardCoder().encode(change)); + assertEquals(PdpState.ACTIVE, fsm.state()); + + change.setState(PdpState.PASSIVE); + fsm.source.offer(new StandardCoder().encode(change)); + assertEquals(PdpState.PASSIVE, fsm.state()); + await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.PASSIVE)); + + fsm.shutdown(); + } +} diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java new file mode 100644 index 00000000..fbc2eeba --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java @@ -0,0 +1,221 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.lifecycle; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; +import org.awaitility.core.ConditionTimeoutException; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.utils.logging.LoggerUtil; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpMessageType; +import org.onap.policy.models.pdp.enums.PdpState; + +/** + * Lifecycle State Passive Tests. + */ +public class LifecycleStatePassiveTest { + + private LifecycleFsm fsm; + + @BeforeClass + public static void setUp() { + SystemPersistence.manager.setConfigurationDir("src/test/resources"); + LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN"); + } + + @AfterClass + public static void tearDown() { + SystemPersistence.manager.setConfigurationDir(null); + } + + /** + * Start tests in the Passive state. + */ + @Before + public void startPassive() { + /* start every test in passive mode */ + fsm = new LifecycleFsm(); + fsm.setStatusTimerSeconds(15L); + simpleStart(); + + assertEquals(0, fsm.client.getSink().getRecentEvents().length); + } + + @Test + public void constructor() { + assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStatePassive(null)); + fsm.shutdown(); + } + + @Test + public void start() { + assertEquals(0, fsm.client.getSink().getRecentEvents().length); + assertFalse(fsm.start()); + assertBasicPassive(); + + fsm.shutdown(); + } + + private Callable<Boolean> isStatus(PdpState state, int count) { + return () -> { + if (!fsm.client.getSink().isAlive()) { + return false; + } + + if (fsm.client.getSink().getRecentEvents().length != count) { + return false; + } + + String[] events = fsm.client.getSink().getRecentEvents(); + PdpStatus status = + new StandardCoder().decode(events[events.length - 1], PdpStatus.class); + + return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState(); + }; + } + + @Test + public void stop() { + simpleStop(); + assertBasicTerminated(); + } + + private void simpleStart() { + assertTrue(fsm.start()); + assertBasicPassive(); + } + + private void simpleStop() { + assertTrue(fsm.stop()); + assertBasicTerminated(); + } + + @Test + public void shutdown() throws CoderException { + simpleStop(); + + fsm.shutdown(); + assertExtendedTerminated(); + } + + @Test + public void status() { + status(PdpState.PASSIVE); + fsm.shutdown(); + } + + private void status(PdpState state) { + await() + .atMost(5, TimeUnit.SECONDS) + .until(isStatus(state, 1)); + + await() + .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS) + .until(isStatus(state, 2)); + + await() + .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS) + .until(isStatus(state, 3)); + + assertTrue(fsm.status()); + await() + .atMost(200, TimeUnit.MILLISECONDS) + .until(isStatus(state, 4)); + } + + @Test + public void update() { + // TODO + fsm.shutdown(); + } + + @Test + public void stateChange() throws CoderException { + PdpStateChange change = new PdpStateChange(); + change.setPdpGroup("A"); + change.setPdpSubgroup("a"); + change.setState(PdpState.ACTIVE); + change.setName("test"); + + fsm.source.offer(new StandardCoder().encode(change)); + assertEquals(PdpState.ACTIVE, fsm.state.state()); + assertEquals("A", fsm.pdpGroup); + assertEquals("a", fsm.pdpSubgroup); + + fsm.shutdown(); + } + + private void assertBasicTerminated() { + assertEquals(PdpState.TERMINATED, fsm.state.state()); + assertFalse(fsm.isAlive()); + assertFalse(fsm.state.isAlive()); + } + + private void assertExtendedTerminated() throws CoderException { + assertBasicTerminated(); + assertTrue(fsm.statusTask.isCancelled()); + assertTrue(fsm.statusTask.isDone()); + + assertEquals(1, fsm.client.getSink().getRecentEvents().length); + PdpStatus status = new StandardCoder().decode(fsm.client.getSink().getRecentEvents()[0], PdpStatus.class); + assertEquals("drools", status.getPdpType()); + assertEquals(PdpState.TERMINATED, status.getState()); + assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy()); + assertEquals(NetworkUtil.getHostname(), status.getInstance()); + assertEquals(PdpMessageType.PDP_STATUS, status.getMessageName()); + + assertThatThrownBy( () -> await() + .atMost(fsm.statusTimerSeconds + 5, TimeUnit.SECONDS) + .until(isStatus(PdpState.TERMINATED, 2))).isInstanceOf(ConditionTimeoutException.class); + } + + private void assertBasicPassive() { + assertEquals(PdpState.PASSIVE, fsm.state.state()); + assertNotNull(fsm.source); + assertNotNull(fsm.client); + assertNotNull(fsm.statusTask); + + assertTrue(fsm.isAlive()); + assertTrue(fsm.source.isAlive()); + assertTrue(fsm.client.getSink().isAlive()); + + assertFalse(fsm.statusTask.isCancelled()); + assertFalse(fsm.statusTask.isDone()); + } +} diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateSafeTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateSafeTest.java new file mode 100644 index 00000000..81ce85f1 --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateSafeTest.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.lifecycle; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.models.pdp.enums.PdpState; + +/** + * TEST State Junits. + */ +public class LifecycleStateSafeTest extends LifecycleStateUnsupportedTest { + + public LifecycleStateSafeTest() { + super(new LifecycleStateSafe(new LifecycleFsm())); + } + + @Override + public LifecycleState create(LifecycleFsm fsm) { + return new LifecycleStateSafe(fsm); + } + + @Test + public void constructor() { + super.constructor(); + assertEquals(PdpState.SAFE, new LifecycleStateSafe(new LifecycleFsm()).state()); + } + + @Test + public void state() { + assertEquals(PdpState.SAFE, state.state()); + } +}
\ No newline at end of file diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java new file mode 100644 index 00000000..b77fdcd9 --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java @@ -0,0 +1,178 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.lifecycle; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.utils.logging.LoggerUtil; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.enums.PdpState; + +/** + * Lifecycle State Terminated Tests. + */ +public class LifecycleStateTerminatedTest { + private LifecycleFsm fsm = new LifecycleFsm(); + + @BeforeClass + public static void setUp() { + SystemPersistence.manager.setConfigurationDir("src/test/resources"); + LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN"); + } + + @AfterClass + public static void tearDown() { + SystemPersistence.manager.setConfigurationDir(null); + } + + @Test + public void constructor() { + assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStateTerminated(null)); + + LifecycleState state = new LifecycleStateTerminated(new LifecycleFsm()); + assertNull(state.fsm.source); + assertNull(state.fsm.client); + assertNull(state.fsm.statusTask); + + assertEquals(PdpState.TERMINATED, state.state()); + assertEquals(PdpState.TERMINATED, state.fsm.state.state()); + assertFalse(state.isAlive()); + } + + @Test + public void stop() { + assertEquals(PdpState.TERMINATED, fsm.state.state()); + assertFalse(fsm.isAlive()); + + simpleStop(); + } + + private void simpleStart() { + assertTrue(fsm.start()); + assertBasicPassive(); + } + + private void simpleStop() { + assertTrue(fsm.stop()); + assertBasicTerminated(); + } + + @Test + public void bounce() { + assertBasicTerminated(); + simpleStart(); + simpleStop(); + + assertFalse(fsm.source.isAlive()); + assertFalse(fsm.client.getSink().isAlive()); + assertExtendedTerminated(); + } + + @Test + public void doubleBounce() { + bounce(); + bounce(); + } + + @Test + public void doubleStartBounce() { + simpleStart(); + assertFalse(fsm.start()); + assertBasicPassive(); + simpleStop(); + } + + @Test + public void shutdown() { + assertBasicTerminated(); + fsm.shutdown(); + assertBasicTerminated(); + + fsm = new LifecycleFsm(); + } + + @Test + public void status() { + assertBasicTerminated(); + assertFalse(fsm.status()); + assertBasicTerminated(); + } + + @Test + public void changeState() { + assertFalse(fsm.state.transitionToState(new LifecycleStateTerminated(fsm))); + assertEquals(PdpState.TERMINATED, fsm.state.state()); + } + + @Test + public void update() { + // TODO + } + + @Test + public void stateChange() { + PdpStateChange change = new PdpStateChange(); + change.setPdpGroup("A"); + change.setPdpSubgroup("a"); + change.setState(PdpState.ACTIVE); + change.setName("test"); + + fsm.stateChange(change); + + assertEquals(PdpState.TERMINATED, fsm.state.state()); + } + + private void assertBasicTerminated() { + assertEquals(PdpState.TERMINATED, fsm.state.state()); + assertFalse(fsm.isAlive()); + assertFalse(fsm.state.isAlive()); + } + + private void assertExtendedTerminated() { + assertBasicTerminated(); + assertTrue(fsm.statusTask.isCancelled()); + assertTrue(fsm.statusTask.isDone()); + assertFalse(fsm.scheduler.isShutdown()); + } + + private void assertBasicPassive() { + assertEquals(PdpState.PASSIVE, fsm.state.state()); + assertNotNull(fsm.source); + assertNotNull(fsm.client); + assertNotNull(fsm.statusTask); + + assertTrue(fsm.isAlive()); + assertTrue(fsm.source.isAlive()); + assertTrue(fsm.client.getSink().isAlive()); + + assertFalse(fsm.statusTask.isCancelled()); + assertFalse(fsm.statusTask.isDone()); + } +}
\ No newline at end of file diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTestTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTestTest.java new file mode 100644 index 00000000..c086dfb4 --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTestTest.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.lifecycle; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.models.pdp.enums.PdpState; + +/** + * TEST State Junits. + */ +public class LifecycleStateTestTest extends LifecycleStateUnsupportedTest { + + public LifecycleStateTestTest() { + super(new LifecycleStateTest(new LifecycleFsm())); + } + + @Override + public LifecycleState create(LifecycleFsm fsm) { + return new LifecycleStateTest(fsm); + } + + @Test + public void constructor() { + super.constructor(); + assertEquals(PdpState.TEST, new LifecycleStateTest(new LifecycleFsm()).state()); + } + + @Test + public void state() { + assertEquals(PdpState.TEST, state.state()); + } +}
\ No newline at end of file diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateUnsupportedTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateUnsupportedTest.java new file mode 100644 index 00000000..01efca32 --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateUnsupportedTest.java @@ -0,0 +1,108 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.lifecycle; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Java6Assertions.assertThatThrownBy; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpUpdate; + +/** + * Lifecycle State Unsupported Test. + */ +public abstract class LifecycleStateUnsupportedTest { + + protected final LifecycleState state; + + @BeforeClass + public static void setUp() { + SystemPersistence.manager.setConfigurationDir("src/test/resources"); + } + + @AfterClass + public static void tearDown() { + SystemPersistence.manager.setConfigurationDir(null); + } + + public LifecycleStateUnsupportedTest(LifecycleState state) { + this.state = state; + } + + public abstract LifecycleState create(LifecycleFsm fsm); + + @Test + public void constructor() { + assertThatIllegalArgumentException().isThrownBy(() -> create(null)); + } + + @Test + public void start() { + assertThatThrownBy(() -> state.start()) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + public void stop() { + assertThatThrownBy(() -> state.stop()) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + public void shutdown() { + assertThatThrownBy(() -> state.shutdown()) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + public void isAlive() { + assertThatThrownBy(() -> state.isAlive()) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + public void status() { + assertThatThrownBy(() -> state.status()) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + public void update() { + assertThatThrownBy(() -> state.update(new PdpUpdate())) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + public void stateChange() { + assertThatThrownBy(() -> state.stateChange(new PdpStateChange())) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + public void changeState() { + assertThatThrownBy(() -> state.transitionToState(new LifecycleStateActive(new LifecycleFsm()))) + .isInstanceOf(UnsupportedOperationException.class); + } +} |