1b34d28b559e9032a15fc9f44ebe54a890b7fef5
[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 java.util.concurrent.atomic.AtomicBoolean;
23 import org.junit.Test;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.Useless;
26 import org.springframework.boot.SpringApplication;
27
28 import static org.junit.Assert.*;
29
30 public class TestSystemFunctions {
31
32     /**
33      * test sleep
34      */
35     @Test
36     public void testSleep() throws Exception {
37         long start = System.currentTimeMillis();
38         SystemFunctions.systemFunctions().sleep(123);
39         long end = System.currentTimeMillis();
40         assertTrue(end - start >= 123);
41     }
42
43     /**
44      * test interrupted sleep
45      */
46     @Test
47     @SuppressWarnings("squid:S2925") //testing asynchronous execution
48     public void testInterruptedSleep() throws Exception {
49         AtomicBoolean entered = new AtomicBoolean(false);
50         long start = System.currentTimeMillis();
51         Set<RuntimeException> exceptions = new HashSet<>();
52         class Inter extends Thread {
53             @Override
54             public void run() {
55                 try {
56                     entered.set(true);
57                     SystemFunctions.systemFunctions().sleep(1000000);
58                 } catch (RuntimeException e) {
59                     exceptions.add(e);
60                     throw e;
61                 }
62             }
63         }
64         Inter inter = new Inter();
65         inter.start();
66         //wait for thread to enter waiting
67         while(!entered.get() && inter.getState() != Thread.State.TIMED_WAITING && (System.currentTimeMillis() < start + 60*1000) ){
68             Thread.sleep(10);
69         }
70         if(!(System.currentTimeMillis() < start + 60*1000)){
71             throw new RuntimeException("Thread did not enter waiting state");
72         }
73         //when
74         inter.interrupt();
75         //verify
76         while (exceptions.size() != 1 && (System.currentTimeMillis() < start + 60*1000)) {
77             Thread.sleep(10);
78         }
79         assertEquals(1, exceptions.size());
80         assertEquals("Interrupted while sleeping", exceptions.iterator().next().getMessage());
81     }
82
83     /**
84      * test current time
85      */
86     @Test
87     public void testCurrentTime() {
88         long now = System.currentTimeMillis();
89         long now2 = SystemFunctions.systemFunctions().currentTimeMillis();
90         assertTrue(Math.abs(now2 - now) < 1000);
91     }
92
93     /**
94      * test file load
95      */
96     @Test
97     public void testFileLoad() {
98         byte[] bytes = SystemFunctions.systemFunctions().loadFile("unittests/empty.zip");
99         assertEquals("UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==", Base64.getEncoder().encodeToString(bytes));
100     }
101
102     /**
103      * missing file results in error
104      */
105     @Test
106     public void testMissingFileLoad() {
107         try {
108             SystemFunctions.systemFunctions().loadFile("unittests/missing");
109             fail();
110         } catch (Exception e) {
111             assertEquals("Unable to load unittests/missing", e.getMessage());
112         }
113     }
114
115     /**
116      * Test standard stream wrapping
117      */
118     @Test
119     public void testStandardStreams() {
120         assertEquals(System.err, SystemFunctions.systemFunctions().err());
121         assertEquals(System.out, SystemFunctions.systemFunctions().out());
122         assertEquals(System.in, SystemFunctions.systemFunctions().in());
123     }
124
125     /**
126      * Test HTTP client wrapping
127      */
128     @Test
129     @Useless //more less already ensured by Java type safety
130     public void testHttp() {
131         assertNotNull(SystemFunctions.systemFunctions().getHttpClient());
132     }
133
134     /**
135      * Test spring application wrapping
136      */
137     @Test
138     public void testSpring() {
139         SpringApplication springApplication = SystemFunctions.systemFunctions().newSpringApplication(NokiaSvnfmApplication.class);
140
141         assertEquals(1, springApplication.getAllSources().size());
142     }
143 }