190dce7bb2bdc5bcc5e824bce4e2ec327727d8d7
[dcaegen2/analytics/tca.git] / dcae-analytics-common / src / test / java / org / openecomp / dcae / apod / analytics / common / validation / GenericValidationResponseTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.common.validation;\r
22 \r
23 import org.junit.Test;\r
24 import org.openecomp.dcae.apod.analytics.common.BaseAnalyticsCommonUnitTest;\r
25 \r
26 import static org.hamcrest.CoreMatchers.is;\r
27 import static org.junit.Assert.assertThat;\r
28 \r
29 /**\r
30  * @author Rajiv Singla . Creation Date: 12/12/2016.\r
31  */\r
32 public class GenericValidationResponseTest extends BaseAnalyticsCommonUnitTest {\r
33 \r
34 \r
35     @Test\r
36     public void testHasErrorsWhenResponseHasErrors() throws Exception {\r
37 \r
38         final String fieldName = "testField";\r
39         final String errorMessage = "Some error message";\r
40         final GenericValidationResponse<CDAPTestAppSettings> validationResponse =\r
41                 createTestValidationResponse(fieldName, errorMessage);\r
42 \r
43         validationResponse.addErrorMessage("testField", "Some error message");\r
44         assertThat("Validation Response must has errors", validationResponse.hasErrors(), is(true));\r
45     }\r
46 \r
47     @Test\r
48     public void testHasErrorsWhenResponseDoesNotHaveErrors() throws Exception {\r
49         GenericValidationResponse<CDAPTestAppSettings> validationResponse = new\r
50                 GenericValidationResponse<>();\r
51         assertThat("Validation Response must has errors", validationResponse.hasErrors(), is(false));\r
52     }\r
53 \r
54     @Test\r
55     public void testGetFieldNamesWithError() throws Exception {\r
56 \r
57         final String fieldName = "testField";\r
58         final String errorMessage = "Some error message";\r
59         final GenericValidationResponse<CDAPTestAppSettings> validationResponse =\r
60                 createTestValidationResponse(fieldName, errorMessage);\r
61 \r
62         assertThat("Validation Response must has errors", validationResponse.hasErrors(), is(true));\r
63         assertThat("Validation Field Name must match",\r
64                 validationResponse.getFieldNamesWithError().iterator().next(), is(fieldName));\r
65     }\r
66 \r
67     @Test\r
68     public void testGetErrorMessages() throws Exception {\r
69 \r
70         final String fieldName = "testField";\r
71         final String errorMessage = "Some error message";\r
72         final GenericValidationResponse<CDAPTestAppSettings> validationResponse =\r
73                 createTestValidationResponse(fieldName, errorMessage);\r
74 \r
75         assertThat("Validation Response must has errors", validationResponse.hasErrors(), is(true));\r
76         assertThat("Validation Error Message must match",\r
77                 validationResponse.getErrorMessages().iterator().next(), is(errorMessage));\r
78     }\r
79 \r
80     @Test\r
81     public void getValidationResultsAsMap() throws Exception {\r
82         final String fieldName = "testField";\r
83         final String errorMessage = "Some error message";\r
84         final GenericValidationResponse<CDAPTestAppSettings> validationResponse =\r
85                 createTestValidationResponse(fieldName, errorMessage);\r
86         assertThat("Validation Response must has errors", validationResponse.hasErrors(), is(true));\r
87         assertThat("Validation Field Name must match",\r
88                 validationResponse.getValidationResultsAsMap().keySet().iterator().next(), is(fieldName));\r
89         assertThat("Validation Error Message must match",\r
90                 validationResponse.getValidationResultsAsMap().values().iterator().next(), is(errorMessage));\r
91     }\r
92 \r
93     @Test\r
94     public void getAllErrorMessage() throws Exception {\r
95         final String fieldName = "testField";\r
96         final String errorMessage = "Some error message";\r
97         final GenericValidationResponse<CDAPTestAppSettings> validationResponse =\r
98                 createTestValidationResponse(fieldName, errorMessage);\r
99         final String allErrorMessage = validationResponse.getAllErrorMessage();\r
100         assertThat("All Error messages should match", allErrorMessage, is(errorMessage));\r
101     }\r
102 \r
103     @Test\r
104     public void addErrorMessage() throws Exception {\r
105         final String fieldName = "testField";\r
106         final String errorMessage = "Some error message";\r
107         GenericValidationResponse<CDAPTestAppSettings> validationResponse = new\r
108                 GenericValidationResponse<>();\r
109         validationResponse.addErrorMessage(fieldName, errorMessage);\r
110 \r
111         assertThat("Validation Response must has errors", validationResponse.hasErrors(), is(true));\r
112         assertThat("Validation Field Name must match",\r
113                 validationResponse.getValidationResultsAsMap().keySet().iterator().next(), is(fieldName));\r
114         assertThat("Validation Error Message must match",\r
115                 validationResponse.getValidationResultsAsMap().values().iterator().next(), is(errorMessage));\r
116     }\r
117 \r
118     private static GenericValidationResponse<CDAPTestAppSettings> createTestValidationResponse(\r
119             final String fieldName, final String errorMessage) {\r
120         GenericValidationResponse<CDAPTestAppSettings> validationResponse = new\r
121                 GenericValidationResponse<>();\r
122         if (fieldName != null || errorMessage != null) {\r
123             validationResponse.addErrorMessage(fieldName, errorMessage);\r
124         }\r
125 \r
126         return validationResponse;\r
127     }\r
128 \r
129 }\r