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