More bug fix and refactoring
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / NodeServerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.dmaap.datarouter.node;
21
22 import static org.mockito.Matchers.anyObject;
23 import static org.mockito.Matchers.anyString;
24 import static org.mockito.Mockito.doNothing;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.File;
29 import java.io.IOException;
30 import org.apache.commons.lang3.reflect.FieldUtils;
31 import org.junit.AfterClass;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
38 import org.powermock.modules.junit4.PowerMockRunner;
39
40 @RunWith(PowerMockRunner.class)
41 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
42 public class NodeServerTest {
43
44     private NodeConfigManager config = mock(NodeConfigManager.class);
45     @Before
46     public void setUp() throws Exception {
47         setUpConfig();
48         setUpNodeMainDelivery();
49         createFilesAndDirectories();
50         mock(Delivery.class);
51     }
52
53     @AfterClass
54     public static void tearDown() {
55         deleteCreatedDirectories();
56     }
57
58     @Test
59     public void Verify_Node_Server_Is_Configured_Correctly() {
60         Assert.assertNotNull(NodeServer.getServerInstance());
61     }
62
63     private void setUpConfig() throws IllegalAccessException {
64         PowerMockito.mockStatic(NodeConfigManager.class);
65         when(config.isShutdown()).thenReturn(false);
66         when(config.isConfigured()).thenReturn(true);
67         when(config.getSpoolDir()).thenReturn("spool/f");
68         when(config.getSpoolBase()).thenReturn("spool");
69         when(config.getLogDir()).thenReturn("log/dir");
70         when(config.getPublishId()).thenReturn("User1");
71         when(config.isAnotherNode(anyString(), anyString())).thenReturn(true);
72         when(config.getEventLogInterval()).thenReturn("40");
73         when(config.isDeletePermitted("1")).thenReturn(true);
74         when(config.getAllDests()).thenReturn(new DestInfo[0]);
75         when(config.getKSType()).thenReturn("PKCS12");
76         when(config.getKSFile()).thenReturn("src/test/resources/aaf/org.onap.dmaap-dr.p12");
77         when(config.getKSPass()).thenReturn("tVac2#@Stx%tIOE^x[c&2fgZ");
78         when(config.getTstype()).thenReturn("jks");
79         when(config.getTsfile()).thenReturn("src/test/resources/aaf/org.onap.dmaap-dr.trust.jks");
80         when(config.getTspass()).thenReturn("XHX$2Vl?Lk*2CB.i1+ZFAhZd");
81         FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true);
82         FieldUtils.writeDeclaredStaticField(NodeRunner.class, "nodeConfigManager", config, true);
83         PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
84     }
85
86     private void setUpNodeMainDelivery() throws IllegalAccessException{
87         Delivery delivery = mock(Delivery.class);
88         doNothing().when(delivery).resetQueue(anyObject());
89         FieldUtils.writeDeclaredStaticField(NodeServer.class, "delivery", delivery, true);
90     }
91
92     private void createFilesAndDirectories() throws IOException {
93         File nodeDir = new File("spool/n/172.0.0.1");
94         File spoolDir = new File("spool/s/0/1");
95         File dataFile = new File("spool/s/0/1/dmaap-dr-node.1234567");
96         File metaDataFile = new File("spool/s/0/1/dmaap-dr-node.1234567.M");
97         nodeDir.mkdirs();
98         spoolDir.mkdirs();
99         dataFile.createNewFile();
100         metaDataFile.createNewFile();
101     }
102
103     private static void deleteCreatedDirectories() {
104         File spoolDir = new File("spool");
105         delete(spoolDir);
106     }
107
108     private static void delete(File file) {
109         if (file.isDirectory()) {
110             for (File f: file.listFiles()) {
111                 delete(f);
112             }
113         }
114         if (!file.delete()) {
115             System.out.println("Failed to delete: " + file);
116         }
117     }
118
119 }