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