BUG-1520 coverage tests for config-api
[controller.git] / opendaylight / config / config-api / src / test / java / org / opendaylight / controller / config / api / ValidationExceptionTest.java
1 package org.opendaylight.controller.config.api;
2
3 import static junit.framework.Assert.assertEquals;
4 import static junit.framework.Assert.assertTrue;
5 import static junit.framework.Assert.fail;
6 import static org.junit.Assert.assertNotEquals;
7 import static org.junit.Assert.assertThat;
8 import static org.junit.matchers.JUnitMatchers.containsString;
9
10 import com.google.common.collect.Lists;
11 import java.util.Map;
12
13 import org.junit.Assert;
14 import org.junit.Test;
15
16 public class ValidationExceptionTest {
17
18     private String instance = "instance";
19     private final ModuleIdentifier mi = new ModuleIdentifier("module", instance);
20     private String instance2 = "instance2";
21     private final ModuleIdentifier mi2 = new ModuleIdentifier("module", instance2);
22     private final String message = "ex message";
23     private final Exception e = new IllegalStateException(message);
24
25     @Test
26     public void testCreateFromCollectedValidationExceptions() throws Exception {
27         ValidationException single = ValidationException.createForSingleException(mi, e);
28         ValidationException single2 = ValidationException.createForSingleException(mi2, e);
29
30         ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
31
32         Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
33         assertEquals(1, failedMap.size());
34         assertTrue(failedMap.containsKey("module"));
35
36         Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
37         assertEquals(2, failedModule.size());
38         assertTrue(failedModule.containsKey(instance));
39         assertEquals(message, failedModule.get(instance).getMessage());
40         assertEquals(message, failedModule.get(instance2).getMessage());
41         assertEquals(failedModule.get(instance), failedModule.get(instance2));
42     }
43
44     @Test
45     public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
46         ValidationException single = ValidationException.createForSingleException(mi, e);
47         ValidationException single2 = ValidationException.createForSingleException(mi, e);
48         try {
49             ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
50         } catch (IllegalArgumentException ex) {
51             // Duplicate exception
52             assertThat(ex.getMessage(), containsString("Cannot merge"));
53             return;
54         }
55         fail("Duplicate exception should have failed");
56     }
57
58     @Test
59     public void testGetTrace() throws Exception {
60         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
61         exp.setTrace("trace");
62         Assert.assertEquals(exp.getTrace(), "trace");
63     }
64
65     @Test
66     public void testSetMessage() throws Exception {
67         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
68         exp.setMessage("message");
69         Assert.assertEquals(exp.getMessage(), "message");
70     }
71
72     @Test
73     public void testHashCode() throws Exception {
74         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
75         Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
76     }
77
78     @Test
79     public void testExceptionMessageWithStackTraceConstructor() throws Exception {
80         ValidationException.ExceptionMessageWithStackTrace exp =
81                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
82         Assert.assertEquals(exp, exp);
83     }
84
85     @Test
86     public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
87         ValidationException.ExceptionMessageWithStackTrace exp =
88                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
89         Assert.assertNotEquals(exp, null);
90     }
91
92     @Test
93     public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
94         ValidationException.ExceptionMessageWithStackTrace exp =
95                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
96         Assert.assertNotEquals(exp, new Exception());
97     }
98
99     @Test
100     public void testExceptionMessageWithStackTraceConstructor4() throws Exception {
101         ValidationException.ExceptionMessageWithStackTrace exp =
102                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
103         Assert.assertEquals(exp, new ValidationException.ExceptionMessageWithStackTrace("string1", "string2"));
104     }
105
106     @Test
107     public void testEqual() throws Exception {
108         ValidationException.ExceptionMessageWithStackTrace exp =
109                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
110         ValidationException.ExceptionMessageWithStackTrace exp2 =
111                 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
112         Assert.assertNotEquals(exp, exp2);
113     }
114
115     @Test
116     public void testEqual2() throws Exception {
117         ValidationException.ExceptionMessageWithStackTrace exp =
118                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
119         ValidationException.ExceptionMessageWithStackTrace exp2 =
120                 new ValidationException.ExceptionMessageWithStackTrace("different", "string2");
121         Assert.assertNotEquals(exp, exp2);
122     }
123
124
125     @Test
126     public void testEqual3() throws Exception {
127         ValidationException.ExceptionMessageWithStackTrace exp =
128                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
129         ValidationException.ExceptionMessageWithStackTrace exp2 =
130                 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
131         Assert.assertNotEquals(exp, exp2);
132     }
133
134     @Test
135     public void testEqual4() throws Exception {
136         ValidationException.ExceptionMessageWithStackTrace exp =
137                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
138         ValidationException.ExceptionMessageWithStackTrace exp2 =
139                 new ValidationException.ExceptionMessageWithStackTrace("string1", "different");
140         Assert.assertNotEquals(exp, exp2);
141     }
142
143     @Test
144     public void testEqual5() throws Exception {
145         ValidationException.ExceptionMessageWithStackTrace exp =
146                 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
147         ValidationException.ExceptionMessageWithStackTrace exp2 =
148                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
149         Assert.assertNotEquals(exp, exp2);
150     }
151
152     @Test
153     public void testEqual6() throws Exception {
154         ValidationException.ExceptionMessageWithStackTrace exp =
155                 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
156         ValidationException.ExceptionMessageWithStackTrace exp2 =
157                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
158         Assert.assertNotEquals(exp, exp2);
159     }
160 }