16 package org.openkilda.northbound.service;
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;
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;
67 import java.util.Collections;
68 import java.util.HashMap;
69 import java.util.List;
71 @RunWith(SpringRunner.class)
73 private int requestIdIndex = 0;
86 messageExchanger.resetMockedResponses();
88 String lastRequestId = idFactory.produceChained(
"dummy");
89 lastRequestId = lastRequestId.substring(0, lastRequestId.indexOf(
':')).trim();
90 requestIdIndex = Integer.valueOf(lastRequestId) + 1;
95 String correlationId =
"links-list";
99 Collections.singletonList(
new PathNode(switchId, 1, 0)),
103 messageExchanger.mockResponse(message);
106 List<LinkDto>
result = linkService.getLinks();
107 assertFalse(
"List of link shouldn't be empty",
result.isEmpty());
110 assertEquals(0, link.getSpeed());
113 assertFalse(link.getPath().isEmpty());
116 assertEquals(1, (
int)
path.getPortNo());
121 final String correlationId =
"empty-link-props";
123 messageExchanger.mockResponse(message);
126 List<LinkPropsDto>
result = linkService.getLinkProps(null, 0, null, 0);
127 assertTrue(
"List of link props should be empty",
result.isEmpty());
132 final String correlationId =
"non-empty-link-props";
137 Collections.singletonMap(
"cost",
"2"));
140 messageExchanger.mockResponse(message);
143 List<LinkPropsDto>
result = linkService.getLinkProps(null, 0, null, 0);
144 assertFalse(
"List of link props shouldn't be empty",
result.isEmpty());
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()));
155 final String correlationId = getClass().getCanonicalName();
157 HashMap<String, String> requestProps =
new HashMap<>();
158 requestProps.put(
"test",
"value");
166 String subCorrelationId = idFactory.produceChained(String.valueOf(requestIdIndex++), correlationId);
167 messageExchanger.mockResponse(
new InfoMessage(payload, System.currentTimeMillis(), subCorrelationId));
170 linkProps.getSource().getDatapath().toString(), linkProps.getSource().getPortNumber(),
171 linkProps.getDest().getDatapath().toString(), linkProps.getDest().getPortNumber(),
177 assertThat(
result.getFailures(), is(0));
178 assertThat(
result.getSuccesses(), is(1));
179 assertTrue(
result.getMessages().isEmpty());
184 final String correlationId = getClass().getCanonicalName();
187 "ff:fe:00:00:00:00:00:05", null, null);
200 String[] requestIdBatch =
new String[] {
201 idFactory.produceChained(String.valueOf(requestIdIndex++), correlationId),
202 idFactory.produceChained(String.valueOf(requestIdIndex++), correlationId)};
204 payload, System.currentTimeMillis(), requestIdBatch[0], requestIdBatch[1]));
206 null, System.currentTimeMillis(), requestIdBatch[1], null));
211 assertThat(
result.getFailures(), is(0));
212 assertThat(
result.getSuccesses(), is(1));
213 assertTrue(
result.getMessages().isEmpty());
219 "org.openkilda.northbound.converter",
220 "org.openkilda.northbound.utils"})
221 @PropertySource({
"classpath:northbound.properties"})
222 static class Config {
224 public CorrelationIdFactory idFactory() {
225 return new TestCorrelationIdFactory();
229 public MessageExchanger messageExchanger() {
230 return new MessageExchanger();
234 public MessageConsumer messageConsumer(MessageExchanger messageExchanger) {
235 return messageExchanger;
239 public MessageProducer messageProducer(MessageExchanger messageExchanger) {
240 return messageExchanger;
244 public RestTemplate restTemplate() {
245 return mock(RestTemplate.class);
249 public LinkService linkService() {
250 return new LinkServiceImpl();
static RequestCorrelationClosable create(String correlationId)
void shouldGetLinksList()
void shouldGetPropsList()
void shouldGetEmptyPropsList()