Merge "fix connection state machine"
[ccsdk/features.git] / sdnr / wt / common / src / test / java / org / onap / ccsdk / features / sdnr / wt / common / test / TestPortstatus.java
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.test;
19
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
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;
31
32 public class TestPortstatus extends Mockito{
33
34
35     @Test
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) );
40         serverSocket.close();
41     }
42
43     @Test
44     public void testWaitIsAvailable() throws IOException {
45         ServerSocket serverSocket = new ServerSocket(4567);
46         assertTrue("Port status should be true", Portstatus.waitSecondsTillAvailable(5, "localhost", 4567) );
47         serverSocket.close();
48     }
49
50     @Test
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)) );
54         serverSocket.close();
55     }
56
57     @Test
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  );
64     }
65
66     @Test
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  );
73     }
74
75
76 }