Containerization feature of SO
[so.git] / adapters / mso-adapters-rest-interface / src / test / java / org / onap / so / openstack / mappers / NetworkInfoMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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
21 package org.onap.so.openstack.mappers;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Optional;
29
30 import org.javatuples.Pair;
31 import org.junit.Test;
32 import org.onap.so.openstack.beans.NetworkInfo;
33 import org.onap.so.openstack.beans.NetworkStatus;
34
35 import com.woorea.openstack.quantum.model.Network;
36 import com.woorea.openstack.quantum.model.Segment;
37
38 public class NetworkInfoMapperTest {
39
40         @Test
41         public void checkNetworkStatusMap() {
42                 NetworkInfoMapper mapper = new NetworkInfoMapper(new Network());
43                 assertEquals(NetworkStatus.ACTIVE, mapper.mapStatus("ACTIVE"));
44                 assertEquals(NetworkStatus.BUILD, mapper.mapStatus("BUILD"));
45                 assertEquals(NetworkStatus.ERROR, mapper.mapStatus("ERROR"));
46                 assertEquals(NetworkStatus.DOWN, mapper.mapStatus("DOWN"));
47                 assertEquals(NetworkStatus.UNKNOWN, mapper.mapStatus("NOT IN MAP"));
48                 assertEquals(NetworkStatus.UNKNOWN, mapper.mapStatus(null));
49         }
50         
51         @Test
52         public void checkLocateVlanInformationNoSegments() {
53                 Network network = new Network();
54                 network.setProviderPhysicalNetwork("test-physical-network");
55                 network.setProviderNetworkType("vlan");
56                 network.setProviderSegmentationId(2);
57                 NetworkInfoMapper mapper = new NetworkInfoMapper(network);
58                 NetworkInfo result = mapper.map();
59                 assertEquals("test-physical-network", result.getProvider());
60                 assertEquals(1, result.getVlans().size());
61                 assertEquals(2, result.getVlans().get(0).intValue());
62         }
63         
64         @Test
65         public void checkLocateVlanInformationSegments() {
66                 Network network = new Network();
67                 addSegments(network);
68
69                 NetworkInfoMapper mapper = new NetworkInfoMapper(network);
70                 NetworkInfo result = mapper.map();
71                 assertEquals("type1", result.getProvider());
72                 assertEquals(2, result.getVlans().size());
73                 assertEquals(Arrays.asList(1, 2).toString(), result.getVlans().toString());
74         }
75         
76         @Test
77         public void checkLocateVlanInformationSegmentsAndPhysical() {
78                 Network network = new Network();
79                 addSegments(network);
80                 network.setProviderPhysicalNetwork("test-physical-network");
81                 network.setProviderNetworkType("vlan");
82                 network.setProviderSegmentationId(2);
83                 NetworkInfoMapper mapper = new NetworkInfoMapper(network);
84                 NetworkInfo result = mapper.map();
85                 assertEquals("test-physical-network", result.getProvider());
86                 assertEquals(1, result.getVlans().size());
87                 assertEquals(2, result.getVlans().get(0).intValue());
88         }
89         
90         @Test
91         public void nullNetwork() {
92                 NetworkInfoMapper mapper = new NetworkInfoMapper(null);
93                 assertEquals(NetworkStatus.NOTFOUND, mapper.map().getStatus());
94         }
95         
96         @Test
97         public void mapFields() {
98                 Network network = new Network();
99                 network.setId("id");
100                 network.setName("name");
101                 network.setSubnets(Arrays.asList("string1", "string2"));
102                 NetworkInfoMapper mapper = new NetworkInfoMapper(network);
103                 NetworkInfo mapped = mapper.map();
104                 assertEquals("name", mapped.getName());
105                 assertEquals("id", mapped.getId());
106                 assertEquals(network.getSubnets(), mapped.getSubnets());
107         }
108         
109         private Network addSegments(Network network) {
110                 List<Segment> segments = new ArrayList<>();
111                 Segment segment1 = new Segment();
112                 segment1.setProviderPhysicalNetwork("type1");
113                 segment1.setProviderNetworkType("vlan");
114                 segment1.setProviderSegmentationId(1);
115                 segments.add(segment1);
116                 Segment segment2 = new Segment();
117                 segment2.setProviderPhysicalNetwork("type2");
118                 segment2.setProviderNetworkType("vlan");
119                 segment2.setProviderSegmentationId(2);
120                 segments.add(segment2);
121                 network.setSegments(segments);
122                 return network;
123         }
124 }