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 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.PowerMockIgnore;
38 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 @RunWith(PowerMockRunner.class)
42 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
43 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
44 public class NodeServerTest {
46 private final NodeConfigManager config = mock(NodeConfigManager.class);
48 public void setUp() throws Exception {
50 setUpNodeMainDelivery();
51 createFilesAndDirectories();
55 public static void tearDown() {
56 deleteCreatedDirectories();
60 public void Verify_Node_Server_Is_Configured_Correctly() {
61 Assert.assertNotNull(NodeServer.getServerInstance(config));
64 private void setUpConfig() throws IllegalAccessException {
65 PowerMockito.mockStatic(NodeConfigManager.class);
66 when(config.isShutdown()).thenReturn(false);
67 when(config.isConfigured()).thenReturn(true);
68 when(config.getSpoolDir()).thenReturn("spool/f");
69 when(config.getSpoolBase()).thenReturn("spool");
70 when(config.getLogDir()).thenReturn("log/dir");
71 when(config.getPublishId()).thenReturn("User1");
72 when(config.isAnotherNode(anyString(), anyString())).thenReturn(true);
73 when(config.getEventLogInterval()).thenReturn("40");
74 when(config.isDeletePermitted("1")).thenReturn(true);
75 when(config.getAllDests()).thenReturn(new DestInfo[0]);
76 when(config.isTlsEnabled()).thenReturn(true);
77 when(config.getKSType()).thenReturn("PKCS12");
78 when(config.getKSFile()).thenReturn("src/test/resources/aaf/org.onap.dmaap-dr.p12");
79 when(config.getKSPass()).thenReturn("tVac2#@Stx%tIOE^x[c&2fgZ");
80 when(config.getTstype()).thenReturn("jks");
81 when(config.getTsfile()).thenReturn("src/test/resources/aaf/org.onap.dmaap-dr.trust.jks");
82 when(config.getTspass()).thenReturn("XHX$2Vl?Lk*2CB.i1+ZFAhZd");
83 PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
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);
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");
99 dataFile.createNewFile();
100 metaDataFile.createNewFile();
103 private static void deleteCreatedDirectories() {
104 File spoolDir = new File("spool");
108 private static void delete(File file) {
109 if (file.isDirectory()) {
110 for (File f: file.listFiles()) {
114 if (!file.delete()) {
115 System.out.println("Failed to delete: " + file);