From 3c0823e37b6cf933b23687a2d7339b131fe5a464 Mon Sep 17 00:00:00 2001 From: Lianhao Lu Date: Tue, 13 Nov 2018 11:25:29 +0800 Subject: FileSystemReceptionHandler: Fix sonar issues Fixed 6 code smell issues reported by sonar. Change-Id: I537df24f0f0624470efd8675f01ecd433aed1cb0 Issue-ID: POLICY-1256 Signed-off-by: Lianhao Lu --- .../sdc/TestFileSystemReceptionHandler.java | 53 ++++++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) (limited to 'plugins/reception-plugins/src/test/java') diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java index 8edca74e..29eb3f7e 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestFileSystemReceptionHandler.java @@ -29,6 +29,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; import org.junit.Before; @@ -38,7 +39,9 @@ import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; @@ -89,9 +92,9 @@ public class TestFileSystemReceptionHandler { public void teardown() { ParameterService.deregister(pssdConfigParameters); } - + @Test - public final void testInit() { + public final void testInit() throws IOException { final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class)); sypHandler.initializeReception(pssdConfigParameters.getName()); @@ -99,7 +102,7 @@ public class TestFileSystemReceptionHandler { } @Test - public final void testDestroy() { + public final void testDestroy() throws IOException { try { final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); Mockito.doNothing().when(sypHandler).main(Mockito.isA(String.class)); @@ -114,23 +117,53 @@ public class TestFileSystemReceptionHandler { @Test public void testMain() throws IOException, PolicyDecodingException { + final Object lock = new Object(); + final String watchPath = tempFolder.getRoot().getAbsolutePath().toString(); + + class Processed { + public boolean processed = false; + } + + Processed cond = new Processed(); final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); - Mockito.doNothing().when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class)); + Mockito.doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) { + synchronized (lock) { + cond.processed = true; + lock.notifyAll(); + } + return null; + } + }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class)); - final String watchPath = tempFolder.getRoot().getAbsolutePath().toString(); Thread th = new Thread(() -> { - sypHandler.main(watchPath); + try { + sypHandler.main(watchPath); + } catch (IOException ex) { + LOGGER.error(ex); + } }); th.start(); try { - // yield to main thread - Thread.sleep(1000); + //wait until internal watch service started or counter reached + AtomicInteger counter = new AtomicInteger(); + counter.set(0); + synchronized (lock) { + while (!sypHandler.isRunning() && counter.getAndIncrement() < 10) { + lock.wait(1000); + } + } Files.copy(Paths.get("src/test/resources/hpaPolicyHugePage.csar"), Paths.get(watchPath + File.separator + "hpaPolicyHugePage.csar")); - // wait enough time - Thread.sleep(1000); + //wait until mock method triggered or counter reached + counter.set(0); + synchronized (lock) { + while (!cond.processed && counter.getAndIncrement() < 10) { + lock.wait(1000); + } + } sypHandler.destroy(); th.interrupt(); th.join(); -- cgit 1.2.3-korg