diff options
Diffstat (limited to 'utils/src/test/java/org/onap')
-rw-r--r-- | utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java | 53 | ||||
-rw-r--r-- | utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java | 62 |
2 files changed, 107 insertions, 8 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java new file mode 100644 index 00000000..7f246ab2 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.common.utils.resources; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.junit.Test; + +/** + * Test text file utilities. + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class TextFileUtilsTest { + + private static final String FILE_CONTENT = "This is the contents of a text file"; + + @Test + public void test() throws IOException { + final File tempTextFile = File.createTempFile("Test", "txt"); + + TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempTextFile.getAbsolutePath()); + + final String textFileString0 = TextFileUtils.getTextFileAsString(tempTextFile.getAbsolutePath()); + assertEquals(FILE_CONTENT, textFileString0); + + final FileInputStream fis = new FileInputStream(tempTextFile); + final String textFileString1 = TextFileUtils.getStreamAsString(fis); + assertEquals(textFileString0, textFileString1); + + } +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java b/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java index 49c0599b..b7774a5e 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java @@ -23,6 +23,8 @@ package org.onap.policy.common.utils.services; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -36,7 +38,8 @@ import org.onap.policy.common.capabilities.Startable; import org.onap.policy.common.utils.services.ServiceManager.RunnableWithEx; public class ServiceManagerTest { - private static final String ALREADY_RUNNING = "services are already running"; + private static final String MY_NAME = "my-name"; + private static final String ALREADY_RUNNING = MY_NAME + " is already running"; private static final String EXPECTED_EXCEPTION = "expected exception"; private ServiceManager svcmgr; @@ -46,7 +49,17 @@ public class ServiceManagerTest { */ @Before public void setUp() { - svcmgr = new ServiceManager(); + svcmgr = new ServiceManager(MY_NAME); + } + + @Test + public void testServiceName() { + assertEquals("service manager", new ServiceManager().getName()); + } + + @Test + public void testGetName() { + assertEquals(MY_NAME, svcmgr.getName()); } @Test @@ -106,16 +119,20 @@ public class ServiceManagerTest { Startable start1 = mock(Startable.class); svcmgr.addService("test start", start1); - svcmgr.start(); + assertTrue(svcmgr.start()); + + assertTrue(svcmgr.isAlive()); verify(start1).start(); verify(start1, never()).stop(); // cannot re-start - assertThatIllegalStateException().isThrownBy(() -> svcmgr.start()) - .withMessage(ALREADY_RUNNING); + assertThatIllegalStateException().isThrownBy(() -> svcmgr.start()).withMessage(ALREADY_RUNNING); // verify that it didn't try to start the service again verify(start1).start(); + + // still running + assertTrue(svcmgr.isAlive()); } @Test @@ -140,6 +157,8 @@ public class ServiceManagerTest { assertThatThrownBy(() -> svcmgr.start()).isInstanceOf(ServiceManagerException.class).hasCause(exception); + assertFalse(svcmgr.isAlive()); + verify(start1).start(); verify(start2).start(); verify(start3).start(); @@ -177,6 +196,8 @@ public class ServiceManagerTest { svcmgr.addService("fifth test start rewind", start5); assertThatThrownBy(() -> svcmgr.start()).isInstanceOf(ServiceManagerException.class).hasCause(exception); + + assertFalse(svcmgr.isAlive()); } @Test @@ -185,8 +206,7 @@ public class ServiceManagerTest { svcmgr.addService("first stop", start1); // cannot stop until started - assertThatIllegalStateException().isThrownBy(() -> svcmgr.stop()) - .withMessage("services are not running"); + assertThatIllegalStateException().isThrownBy(() -> svcmgr.stop()).withMessage(MY_NAME + " is not running"); // verify that it didn't try to stop the service verify(start1, never()).stop(); @@ -194,7 +214,9 @@ public class ServiceManagerTest { // start it svcmgr.start(); - svcmgr.stop(); + assertTrue(svcmgr.stop()); + + assertFalse(svcmgr.isAlive()); verify(start1).stop(); } @@ -218,6 +240,28 @@ public class ServiceManagerTest { verify(stop1).run(); verify(start2).start(); verify(start2).stop(); + + assertFalse(svcmgr.isAlive()); + } + + @Test + public void testShutdown() throws Exception { + Startable start1 = mock(Startable.class); + svcmgr.addService("first stop", start1); + + // cannot stop until started + assertThatIllegalStateException().isThrownBy(() -> svcmgr.shutdown()).withMessage(MY_NAME + " is not running"); + + // verify that it didn't try to stop the service + verify(start1, never()).stop(); + + // start it + svcmgr.start(); + + svcmgr.shutdown(); + + assertFalse(svcmgr.isAlive()); + verify(start1).stop(); } @Test @@ -242,6 +286,8 @@ public class ServiceManagerTest { assertThatThrownBy(() -> svcmgr.stop()).isInstanceOf(ServiceManagerException.class).hasCause(exception); + assertFalse(svcmgr.isAlive()); + // all of them should have been stopped, in reverse order assertEquals(Arrays.asList("rewind5", "rewind4", "rewind3", "rewind2", "rewind1").toString(), lst.toString()); } |