Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / beans / IngressRouteTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning.beans;
24
25 import org.apache.commons.lang3.reflect.FieldUtils;
26 import org.json.JSONArray;
27 import org.json.JSONObject;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.powermock.api.mockito.PowerMockito;
33 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
34 import org.powermock.modules.junit4.PowerMockRunner;
35
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 import static org.mockito.Mockito.mock;
42
43 @RunWith(PowerMockRunner.class)
44 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Feed")
45 public class IngressRouteTest {
46
47   private IngressRoute ingressRoute;
48
49   @Before
50   public void setUp() throws IllegalAccessException{
51     PowerMockito.mockStatic(Feed.class);
52     Feed feed = mock(Feed.class);
53     PowerMockito.when(Feed.getFeedById(1)).thenReturn(feed);
54     Map<String, Integer> map = new HashMap<>();
55     FieldUtils.writeDeclaredStaticField(NodeClass.class, "nodesMap", map, true);
56   }
57
58   @Test
59   public void Validate_IngressRoute_Constructors_Create_Same_Object() {
60     List<String> nodes = new ArrayList<>();
61     nodes.add("node.datarouternew.com");
62     ingressRoute = new IngressRoute(1, 1, "user1", "172.100.0.0/25", nodes);
63     JSONObject ingressRouteJson = createIngressRouteJson();
64     Assert.assertEquals(ingressRoute, new IngressRoute(ingressRouteJson));
65   }
66
67   @Test
68   public void Validate_AsJsonObject_Returns_Same_Values() {
69     List<String> nodes = new ArrayList<>();
70     nodes.add("node.datarouternew.com");
71     ingressRoute = new IngressRoute(1, 1, "user1", "172.100.0.0/25", nodes);
72     JSONObject ingressRouteJson = createIngressRouteJson();
73     Assert.assertEquals(ingressRoute.asJSONObject().toString(), ingressRouteJson.toString());
74   }
75
76   private JSONObject createIngressRouteJson() {
77     JSONObject ingressRouteJson = new JSONObject();
78     ingressRouteJson.put("seq", 1);
79     ingressRouteJson.put("feedid", 1);
80     ingressRouteJson.put("user", "user1");
81     ingressRouteJson.put("subnet", "172.100.0.0/25");
82     JSONArray nodes = new JSONArray();
83     nodes.put("node.datarouternew.com");
84     ingressRouteJson.put("node", nodes);
85     return ingressRouteJson;
86   }
87
88 }