Open Kilda Java Documentation
LinkServiceTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.service;
17 
18 import static org.hamcrest.Matchers.is;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.mock;
24 
54 
55 import org.junit.Before;
56 import org.junit.Test;
57 import org.junit.runner.RunWith;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.boot.test.context.TestConfiguration;
60 import org.springframework.context.annotation.Bean;
61 import org.springframework.context.annotation.ComponentScan;
62 import org.springframework.context.annotation.Import;
63 import org.springframework.context.annotation.PropertySource;
64 import org.springframework.test.context.junit4.SpringRunner;
65 import org.springframework.web.client.RestTemplate;
66 
67 import java.util.Collections;
68 import java.util.HashMap;
69 import java.util.List;
70 
71 @RunWith(SpringRunner.class)
72 public class LinkServiceTest {
73  private int requestIdIndex = 0;
74 
75  @Autowired
76  private CorrelationIdFactory idFactory;
77 
78  @Autowired
79  private LinkService linkService;
80 
81  @Autowired
82  private MessageExchanger messageExchanger;
83 
84  @Before
85  public void reset() {
86  messageExchanger.resetMockedResponses();
87 
88  String lastRequestId = idFactory.produceChained("dummy");
89  lastRequestId = lastRequestId.substring(0, lastRequestId.indexOf(':')).trim();
90  requestIdIndex = Integer.valueOf(lastRequestId) + 1;
91  }
92 
93  @Test
94  public void shouldGetLinksList() {
95  String correlationId = "links-list";
96  SwitchId switchId = new SwitchId(1L);
97 
98  IslInfoData islInfoData = new IslInfoData(
99  Collections.singletonList(new PathNode(switchId, 1, 0)),
101 
102  Message message = new ChunkedInfoMessage(islInfoData, 0, correlationId, null);
103  messageExchanger.mockResponse(message);
104  RequestCorrelationId.create(correlationId);
105 
106  List<LinkDto> result = linkService.getLinks();
107  assertFalse("List of link shouldn't be empty", result.isEmpty());
108 
109  LinkDto link = result.get(0);
110  assertEquals(0, link.getSpeed());
111  assertEquals(LinkStatus.DISCOVERED, link.getState());
112 
113  assertFalse(link.getPath().isEmpty());
114  PathDto path = link.getPath().get(0);
115  assertEquals(switchId.toString(), path.getSwitchId());
116  assertEquals(1, (int) path.getPortNo());
117  }
118 
119  @Test
120  public void shouldGetEmptyPropsList() {
121  final String correlationId = "empty-link-props";
122  Message message = new ChunkedInfoMessage(null, 0, correlationId, null);
123  messageExchanger.mockResponse(message);
124  RequestCorrelationId.create(correlationId);
125 
126  List<LinkPropsDto> result = linkService.getLinkProps(null, 0, null, 0);
127  assertTrue("List of link props should be empty", result.isEmpty());
128  }
129 
130  @Test
131  public void shouldGetPropsList() {
132  final String correlationId = "non-empty-link-props";
133 
134  LinkProps linkProps = new LinkProps(
135  new NetworkEndpoint(new SwitchId("00:00:00:00:00:00:00:01"), 1),
136  new NetworkEndpoint(new SwitchId("00:00:00:00:00:00:00:02"), 2),
137  Collections.singletonMap("cost", "2"));
138  LinkPropsData linkPropsData = new LinkPropsData(linkProps);
139  Message message = new ChunkedInfoMessage(linkPropsData, 0, correlationId, null);
140  messageExchanger.mockResponse(message);
141  RequestCorrelationId.create(correlationId);
142 
143  List<LinkPropsDto> result = linkService.getLinkProps(null, 0, null, 0);
144  assertFalse("List of link props shouldn't be empty", result.isEmpty());
145 
146  LinkPropsDto dto = result.get(0);
147  assertThat(dto.getSrcSwitch(), is(linkPropsData.getLinkProps().getSource().getDatapath().toString()));
148  assertThat(dto.getSrcPort(), is(linkPropsData.getLinkProps().getSource().getPortNumber()));
149  assertThat(dto.getDstSwitch(), is(linkPropsData.getLinkProps().getDest().getDatapath().toString()));
150  assertThat(dto.getDstPort(), is(linkPropsData.getLinkProps().getDest().getPortNumber()));
151  }
152 
153  @Test
154  public void putLinkProps() throws Exception {
155  final String correlationId = getClass().getCanonicalName();
156 
157  HashMap<String, String> requestProps = new HashMap<>();
158  requestProps.put("test", "value");
159  LinkProps linkProps = new LinkProps(
160  new NetworkEndpoint(new SwitchId("ff:fe:00:00:00:00:00:01"), 8),
161  new NetworkEndpoint(new SwitchId("ff:fe:00:00:00:00:00:02"), 9),
162  requestProps);
163  LinkPropsRequest request = new LinkPropsPut(linkProps);
164 
165  LinkPropsResponse payload = new LinkPropsResponse(request, linkProps, null);
166  String subCorrelationId = idFactory.produceChained(String.valueOf(requestIdIndex++), correlationId);
167  messageExchanger.mockResponse(new InfoMessage(payload, System.currentTimeMillis(), subCorrelationId));
168 
169  LinkPropsDto inputItem = new LinkPropsDto(
170  linkProps.getSource().getDatapath().toString(), linkProps.getSource().getPortNumber(),
171  linkProps.getDest().getDatapath().toString(), linkProps.getDest().getPortNumber(),
172  requestProps);
173 
174  RequestCorrelationId.create(correlationId);
175  BatchResults result = linkService.setLinkProps(Collections.singletonList(inputItem));
176 
177  assertThat(result.getFailures(), is(0));
178  assertThat(result.getSuccesses(), is(1));
179  assertTrue(result.getMessages().isEmpty());
180  }
181 
182  @Test
183  public void dropLinkProps() throws Exception {
184  final String correlationId = getClass().getCanonicalName();
185 
186  LinkPropsDto input = new LinkPropsDto("ff:fe:00:00:00:00:00:01", 8,
187  "ff:fe:00:00:00:00:00:05", null, null);
188 
189  LinkPropsDrop request = new LinkPropsDrop(new LinkPropsMask(
190  new NetworkEndpointMask(new SwitchId(input.getSrcSwitch()), input.getSrcPort()),
191  new NetworkEndpointMask(new SwitchId(input.getDstSwitch()), input.getDstPort())));
192 
193  LinkProps linkProps = new LinkProps(
194  new NetworkEndpoint(new SwitchId(input.getSrcSwitch()), input.getSrcPort()),
195  new NetworkEndpoint(new SwitchId("ff:fe:00:00:00:00:00:02"), 9),
196  new HashMap<>());
197 
198  LinkPropsResponse payload = new LinkPropsResponse(request, linkProps, null);
199 
200  String[] requestIdBatch = new String[] {
201  idFactory.produceChained(String.valueOf(requestIdIndex++), correlationId),
202  idFactory.produceChained(String.valueOf(requestIdIndex++), correlationId)};
203  messageExchanger.mockResponse(new ChunkedInfoMessage(
204  payload, System.currentTimeMillis(), requestIdBatch[0], requestIdBatch[1]));
205  messageExchanger.mockResponse(new ChunkedInfoMessage(
206  null, System.currentTimeMillis(), requestIdBatch[1], null));
207 
208  RequestCorrelationId.create(correlationId);
209  BatchResults result = linkService.delLinkProps(Collections.singletonList(input));
210 
211  assertThat(result.getFailures(), is(0));
212  assertThat(result.getSuccesses(), is(1));
213  assertTrue(result.getMessages().isEmpty());
214  }
215 
216  @TestConfiguration
217  @Import(KafkaConfig.class)
218  @ComponentScan({
219  "org.openkilda.northbound.converter",
220  "org.openkilda.northbound.utils"})
221  @PropertySource({"classpath:northbound.properties"})
222  static class Config {
223  @Bean
224  public CorrelationIdFactory idFactory() {
225  return new TestCorrelationIdFactory();
226  }
227 
228  @Bean
229  public MessageExchanger messageExchanger() {
230  return new MessageExchanger();
231  }
232 
233  @Bean
234  public MessageConsumer messageConsumer(MessageExchanger messageExchanger) {
235  return messageExchanger;
236  }
237 
238  @Bean
239  public MessageProducer messageProducer(MessageExchanger messageExchanger) {
240  return messageExchanger;
241  }
242 
243  @Bean
244  public RestTemplate restTemplate() {
245  return mock(RestTemplate.class);
246  }
247 
248  @Bean
249  public LinkService linkService() {
250  return new LinkServiceImpl();
251  }
252  }
253 }
static RequestCorrelationClosable create(String correlationId)
list result
Definition: plan-d.py:72