Open Kilda Java Documentation
JsonSerializationTest.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.dto;
17 
18 import static java.util.Collections.singletonList;
19 import static org.junit.Assert.assertEquals;
20 
34 
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import org.junit.Test;
37 
38 import java.io.IOException;
39 import java.util.Collections;
40 
41 public class JsonSerializationTest {
42 
43  private static final String SWITCH_ID = "switch-test";
44  private static final String FLOW_ID = "flow-test";
45 
46  private final ObjectMapper mapper = new ObjectMapper();
47 
48  private <T> T pass(T entity, Class<T> clazz) throws IOException {
49  return mapper.readValue(mapper.writeValueAsString(entity), clazz);
50  }
51 
52  @Test
53  public void pathDiscrepancyDtoTest() throws IOException {
54  PathDiscrepancyDto dto = new PathDiscrepancyDto("rule", "field", "expected", "actual");
55  assertEquals(dto, pass(dto, PathDiscrepancyDto.class));
56  }
57 
58  @Test
59  public void flowValidationDtoTest() throws IOException {
60  PathDiscrepancyDto discrepancyDto = new PathDiscrepancyDto("rule", "field", "expected", "actual");
62  FLOW_ID, true, singletonList(0L), singletonList(1L), singletonList(discrepancyDto), 10, 11);
63  assertEquals(dto, pass(dto, FlowValidationDto.class));
64  }
65 
66  @Test
67  public void uniFlowVerificationOutputTest() throws IOException {
68  UniFlowVerificationOutput dto = new UniFlowVerificationOutput(true, "err-test", 10);
69  assertEquals(dto, pass(dto, UniFlowVerificationOutput.class));
70  }
71 
72  @Test
73  public void verificationInputTest() throws IOException {
75  assertEquals(dto, pass(dto, VerificationInput.class));
76  }
77 
78  @Test
79  public void verificationOutputTest() throws IOException {
80  UniFlowVerificationOutput verification = new UniFlowVerificationOutput(true, "err-test", 10);
81  VerificationOutput dto = new VerificationOutput(FLOW_ID, verification, verification);
82  assertEquals(dto, pass(dto, VerificationOutput.class));
83  }
84 
85 
86  @Test
87  public void linksDtoTest() throws IOException {
88  LinkDto dto = new LinkDto(
89  1, 0, LinkStatus.DISCOVERED, singletonList(new PathDto(SWITCH_ID, 1, 0, 10L)));
90  assertEquals(dto, pass(dto, LinkDto.class));
91  }
92 
93  @Test
94  public void pathDtoTest() throws IOException {
95  PathDto dto = new PathDto(SWITCH_ID, 1, 0, 10L);
96  assertEquals(dto, pass(dto, PathDto.class));
97  }
98 
99  @Test
100  public void linksPropsDtoTest() throws IOException {
101  LinkPropsDto dto = new LinkPropsDto(SWITCH_ID, 0, SWITCH_ID, 1, Collections.singletonMap("key", "val"));
102  assertEquals(dto, pass(dto, LinkPropsDto.class));
103  }
104 
105 
106  @Test
107  public void deleteMeterResultTest() throws IOException {
108  DeleteMeterResult dto = new DeleteMeterResult(true);
109  assertEquals(dto, pass(dto, DeleteMeterResult.class));
110  }
111 
112  @Test
113  public void rulesValidationResultTest() throws IOException {
115  singletonList(0L), singletonList(1L), singletonList(2L));
116  assertEquals(dto, pass(dto, RulesValidationResult.class));
117  }
118 
119  @Test
120  public void rulesSyncResultTest() throws IOException {
122  singletonList(0L), singletonList(1L), singletonList(2L), singletonList(3L));
123  assertEquals(dto, pass(dto, RulesSyncResult.class));
124  }
125 
126  @Test
127  public void switchDtoTest() throws IOException {
128  SwitchDto dto = new SwitchDto(SWITCH_ID, "address-test", "host", "desc", "state");
129  assertEquals(dto, pass(dto, SwitchDto.class));
130  }
131 
132  @Test
133  public void batchResultsTest() throws IOException {
134  BatchResults dto = new BatchResults(1, 0, singletonList("qwerty"));
135  assertEquals(dto, pass(dto, BatchResults.class));
136  }
137 }