0b31fef57df4d005875140b8b288b11953f6f3bc
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / util / TestSystemFunctions.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util;
18
19 import java.util.Base64;
20 import java.util.HashSet;
21 import java.util.Set;
22 import org.junit.Test;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.Useless;
24
25 import static org.junit.Assert.*;
26
27 public class TestSystemFunctions {
28
29     /**
30      * test sleep
31      */
32     @Test
33     public void testSleep() throws Exception {
34         long start = System.currentTimeMillis();
35         SystemFunctions.systemFunctions().sleep(123);
36         long end = System.currentTimeMillis();
37         assertTrue(end - start >= 123);
38     }
39
40     /**
41      * test interrupted sleep
42      */
43     @Test
44     public void testInterruptedSleep() throws Exception {
45         long start = System.currentTimeMillis();
46         Set<RuntimeException> exceptions = new HashSet<>();
47         class Inter extends Thread {
48             @Override
49             public void run() {
50                 try {
51                     SystemFunctions.systemFunctions().sleep(10000);
52                 } catch (RuntimeException e) {
53                     exceptions.add(e);
54                 }
55             }
56         }
57         Inter inter = new Inter();
58         inter.start();
59         //when
60         inter.interrupt();
61
62         //verify
63         while (exceptions.size() != 1) {
64
65         }
66     }
67
68     /**
69      * test current time
70      */
71     @Test
72     public void testCurrentTime() {
73         long now = System.currentTimeMillis();
74         long now2 = SystemFunctions.systemFunctions().currentTimeMillis();
75         assertTrue(Math.abs(now2 - now) < 1000);
76     }
77
78     /**
79      * test file load
80      */
81     @Test
82     public void testFileLoad() {
83         byte[] bytes = SystemFunctions.systemFunctions().loadFile("unittests/empty.zip");
84         assertEquals("UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==", Base64.getEncoder().encodeToString(bytes));
85     }
86
87     /**
88      * missing file results in error
89      */
90     @Test
91     public void testMissingFileLoad() {
92         try {
93             SystemFunctions.systemFunctions().loadFile("unittests/missing");
94             fail();
95         } catch (Exception e) {
96             assertEquals("Unable to load unittests/missing", e.getMessage());
97         }
98     }
99
100     /**
101      * Test standard stream wrapping
102      */
103     @Test
104     public void testStandardStreams() {
105         assertEquals(System.err, SystemFunctions.systemFunctions().err());
106         assertEquals(System.out, SystemFunctions.systemFunctions().out());
107         assertEquals(System.in, SystemFunctions.systemFunctions().in());
108     }
109
110     /**
111      * Test HTTP client wrapping
112      */
113     @Test
114     @Useless //more less already ensured by Java type safety
115     public void testHttp() {
116         assertNotNull(SystemFunctions.systemFunctions().getHttpClient());
117     }
118 }