4e69f44ee7ed75bb8e2399a85b6882ce218d7f4a
[policy/apex-pdp.git] / core / core-infrastructure / src / test / java / org / onap / policy / apex / core / infrastructure / java / classes / ClassUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (c) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.apex.core.infrastructure.java.classes;
21
22 import static org.junit.Assert.*;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30 import org.junit.Test;
31 import org.mockito.Mockito;
32
33 public class ClassUtilsTest {
34
35     @Test
36     public void testGetClassNames() throws IOException {
37         InputStream input = null;
38         ClassUtils.getClassNames();
39         assertEquals(new TreeSet<>(), ClassUtils.processJar(input));
40     }
41
42     @Test
43     public void testProcessFileName() {
44         assertEquals("testing.txt",ClassUtils.processFileName("testing.txt"));
45         assertNull(ClassUtils.processFileName(null));
46         assertEquals("",ClassUtils.processFileName("/classes/"));
47     }
48
49     @Test
50     public void testProcessDir() throws Exception {
51         File mockFile = Mockito.mock(File.class);
52         File mockChildFile = Mockito.mock(File.class);
53         Mockito.when(mockFile.isDirectory()).thenReturn(false);
54         assertEquals(new TreeSet<>(),ClassUtils.processDir(mockFile, "Here"));
55         assertEquals(new TreeSet<>(),ClassUtils.processDir(null, "Test"));
56         Mockito.when(mockFile.isDirectory()).thenReturn(true);
57         File[] files = {mockChildFile};
58         Mockito.when(mockFile.listFiles()).thenReturn(files);
59         Mockito.when(mockChildFile.getName()).thenReturn("test.class");
60         Mockito.when(mockChildFile.getAbsolutePath()).thenReturn("/test/");
61         assertEquals(Set.of(".test."),ClassUtils.processDir(mockFile, "Here"));
62         Mockito.when(mockChildFile.getName()).thenReturn("test.class");
63         assertEquals(Set.of(".test."),ClassUtils.processDir(mockFile, "Here"));
64         Mockito.when(mockChildFile.getName()).thenReturn("$test.class");
65         assertEquals(new TreeSet<>(),ClassUtils.processDir(mockFile, "Here"));
66     }
67
68 }