1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.test;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
23 import java.io.IOException;
24 import java.net.ServerSocket;
25 import java.time.Duration;
26 import java.time.Instant;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.Portstatus;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
32 public class TestPortstatus extends Mockito{
36 public void testIsAvailable() throws IOException {
37 assertFalse("Port status should be false", Portstatus.isAvailable("localhost", 4567) );
38 ServerSocket serverSocket = new ServerSocket(4567);
39 assertTrue("Port status should be true", Portstatus.isAvailable("localhost", 4567) );
44 public void testWaitIsAvailable() throws IOException {
45 ServerSocket serverSocket = new ServerSocket(4567);
46 assertTrue("Port status should be true", Portstatus.waitSecondsTillAvailable(5, "localhost", 4567) );
51 public void testWaitIsAvailableHostInfo() throws IOException {
52 ServerSocket serverSocket = new ServerSocket(4567);
53 assertTrue("Port status should be true", Portstatus.waitSecondsTillAvailable(5, new HostInfo("localhost", 4567)) );
58 public void testWaitTillAvailable() throws IOException {
59 Instant start = Instant.now();
60 assertFalse("Port status should be false", Portstatus.waitSecondsTillAvailable(5, "localhost", 4567) );
61 Instant end = Instant.now();
62 long seconds = Duration.between(start, end).getSeconds();
63 assertTrue("Port status timeout 5 expected and not between 3 .. 7 seconds", 3 < seconds && seconds < 7 );
67 public void testWaitTillAvailableHostinfo() throws IOException {
68 Instant start = Instant.now();
69 assertFalse("Port status should be false", Portstatus.waitSecondsTillAvailable(5, new HostInfo("localhost", 4567)));
70 Instant end = Instant.now();
71 long seconds = Duration.between(start, end).getSeconds();
72 assertTrue("Port status timeout 5 expected and not between 3 .. 7 seconds", 3 < seconds && seconds < 7 );