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