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