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 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());
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.getKSType()).thenReturn("PKCS12");
77 when(config.getKSFile()).thenReturn("src/test/resources/aaf/org.onap.dmaap-dr.p12");
78 when(config.getKSPass()).thenReturn("tVac2#@Stx%tIOE^x[c&2fgZ");
79 when(config.getTstype()).thenReturn("jks");
80 when(config.getTsfile()).thenReturn("src/test/resources/aaf/org.onap.dmaap-dr.trust.jks");
81 when(config.getTspass()).thenReturn("XHX$2Vl?Lk*2CB.i1+ZFAhZd");
82 FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true);
83 FieldUtils.writeDeclaredStaticField(NodeRunner.class, "nodeConfigManager", config, true);
84 PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
87 private void setUpNodeMainDelivery() throws IllegalAccessException{
88 Delivery delivery = mock(Delivery.class);
89 doNothing().when(delivery).resetQueue(anyObject());
90 FieldUtils.writeDeclaredStaticField(NodeServer.class, "delivery", delivery, true);
93 private void createFilesAndDirectories() throws IOException {
94 File nodeDir = new File("spool/n/172.0.0.1");
95 File spoolDir = new File("spool/s/0/1");
96 File dataFile = new File("spool/s/0/1/dmaap-dr-node.1234567");
97 File metaDataFile = new File("spool/s/0/1/dmaap-dr-node.1234567.M");
100 dataFile.createNewFile();
101 metaDataFile.createNewFile();
104 private static void deleteCreatedDirectories() {
105 File spoolDir = new File("spool");
109 private static void delete(File file) {
110 if (file.isDirectory()) {
111 for (File f: file.listFiles()) {
115 if (!file.delete()) {
116 System.out.println("Failed to delete: " + file);