df97d4a4293b07955eb81dfd0a4ac577bf8140d5
[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
24 import static org.junit.Assert.*;
25
26 public class TestSystemFunctions {
27
28     /**
29      * test sleep
30      */
31     @Test
32     public void testSleep() throws Exception {
33         long start = System.currentTimeMillis();
34         SystemFunctions.systemFunctions().sleep(123);
35         long end = System.currentTimeMillis();
36         assertTrue(end - start >= 123);
37     }
38
39     /**
40      * test interrupted sleep
41      */
42     @Test
43     public void testInterruptedSleep() throws Exception {
44         long start = System.currentTimeMillis();
45         Set<RuntimeException> exceptions = new HashSet<>();
46         class Inter extends Thread {
47             @Override
48             public void run() {
49                 try {
50                     SystemFunctions.systemFunctions().sleep(10000);
51                 } catch (RuntimeException e) {
52                     exceptions.add(e);
53                 }
54             }
55         }
56         Inter inter = new Inter();
57         inter.start();
58         //when
59         inter.interrupt();
60
61         //verify
62         while (exceptions.size() != 1) {
63
64         }
65     }
66
67     /**
68      * test current time
69      */
70     @Test
71     public void testCurrentTime() {
72         long now = System.currentTimeMillis();
73         long now2 = SystemFunctions.systemFunctions().currentTimeMillis();
74         assertTrue(Math.abs(now2 - now) < 1000);
75     }
76
77     /**
78      * test file load
79      */
80     @Test
81     public void testFileLoad() {
82         byte[] bytes = SystemFunctions.systemFunctions().loadFile("unittests/empty.zip");
83         assertEquals("UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==", Base64.getEncoder().encodeToString(bytes));
84     }
85
86     /**
87      * missing file results in error
88      */
89     @Test
90     public void testMissingFileLoad() {
91         try {
92             SystemFunctions.systemFunctions().loadFile("unittests/missing");
93             fail();
94         } catch (Exception e) {
95             assertEquals("Unable to load unittests/missing", e.getMessage());
96         }
97     }
98
99     /**
100      * Test standard stream wrapping
101      */
102     @Test
103     public void testStandardStreams() {
104         assertEquals(System.err, SystemFunctions.systemFunctions().err());
105         assertEquals(System.out, SystemFunctions.systemFunctions().out());
106         assertEquals(System.in, SystemFunctions.systemFunctions().in());
107     }
108
109     /**
110      * Test HTTP client wrapping
111      * (semi useless test)
112      */
113     @Test
114     public void testHttp() {
115         assertNotNull(SystemFunctions.systemFunctions().getHttpClient());
116     }
117 }