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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.dmaap.datarouter.node;
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;
29 import java.io.IOException;
30 import java.util.Properties;
31 import org.apache.commons.lang3.reflect.FieldUtils;
32 import org.junit.AfterClass;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.dmaap.datarouter.node.delivery.Delivery;
38 import org.onap.dmaap.datarouter.node.utils.NodeTlsManager;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PowerMockIgnore;
41 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
42 import org.powermock.modules.junit4.PowerMockRunner;
44 @RunWith(PowerMockRunner.class)
45 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
46 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
47 public class NodeServerTest {
49 private final NodeConfigManager config = mock(NodeConfigManager.class);
50 private NodeTlsManager nodeTlsManager;
52 public void setUp() throws Exception {
55 setUpNodeMainDelivery();
56 createFilesAndDirectories();
60 public static void tearDown() {
61 deleteCreatedDirectories();
65 public void Verify_Node_Server_Is_Configured_Correctly() {
66 Assert.assertNotNull(NodeServer.getServerInstance(config));
69 private void setUpConfig() {
70 PowerMockito.mockStatic(NodeConfigManager.class);
71 when(config.isShutdown()).thenReturn(false);
72 when(config.isConfigured()).thenReturn(true);
73 when(config.getSpoolDir()).thenReturn("spool/f");
74 when(config.getSpoolBase()).thenReturn("spool");
75 when(config.getLogDir()).thenReturn("log/dir");
76 when(config.getPublishId()).thenReturn("User1");
77 when(config.isAnotherNode(anyString(), anyString())).thenReturn(true);
78 when(config.getEventLogInterval()).thenReturn("40");
79 when(config.isDeletePermitted("1")).thenReturn(true);
80 when(config.getAllDests()).thenReturn(new DestInfo[0]);
81 when(config.isTlsEnabled()).thenReturn(true);
82 PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
85 private void setUpNodeTlsMan() throws IOException {
86 Properties nodeProps = new Properties();
87 nodeProps.load(NodeTlsManagerTest.class.getClassLoader().getResourceAsStream("node_test.properties"));
88 nodeTlsManager = new NodeTlsManager(nodeProps);
89 PowerMockito.when(NodeConfigManager.getNodeTlsManager()).thenReturn(nodeTlsManager);
92 private void setUpNodeMainDelivery() throws IllegalAccessException{
93 Delivery delivery = mock(Delivery.class);
94 doNothing().when(delivery).resetQueue(anyObject());
95 FieldUtils.writeDeclaredStaticField(NodeServer.class, "delivery", delivery, true);
98 private void createFilesAndDirectories() throws IOException {
99 File nodeDir = new File("spool/n/172.0.0.1");
100 File spoolDir = new File("spool/s/0/1");
101 File dataFile = new File("spool/s/0/1/dmaap-dr-node.1234567");
102 File metaDataFile = new File("spool/s/0/1/dmaap-dr-node.1234567.M");
105 dataFile.createNewFile();
106 metaDataFile.createNewFile();
109 private static void deleteCreatedDirectories() {
110 File spoolDir = new File("spool");
114 private static void delete(File file) {
115 if (file.isDirectory()) {
116 for (File f: file.listFiles()) {
120 if (!file.delete()) {
121 System.out.println("Failed to delete: " + file);