Test coverage for RequestValidationPolicy
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / test / java / org / onap / appc / validationpolicy / RequestValidationPolicyTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
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  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.validationpolicy;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import java.sql.SQLException;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.mockito.Mockito;
32 import org.onap.ccsdk.sli.core.dblib.DbLibService;
33 import com.sun.rowset.CachedRowSetImpl;
34
35 public class RequestValidationPolicyTest {
36
37     private final String TEST_CONTENT = "TEST_CONTENT";
38     private final String ARTIFACT_CONTENT = "ARTIFACT_CONTENT";
39     private DbLibService db;
40     private RequestValidationPolicy policy;
41     private CachedRowSetImpl rowset;
42
43     @Rule
44     public ExpectedException expectedEx = ExpectedException.none();
45
46     @Before
47     public void setup() throws SQLException {
48         db = Mockito.mock(DbLibService.class);
49         rowset = Mockito.mock(CachedRowSetImpl.class);
50         Mockito.when(rowset.next()).thenReturn(true);
51         Mockito.when(db.getData(Mockito.anyString(), Mockito.any(), Mockito.anyString())).thenReturn(rowset);
52         policy = new RequestValidationPolicy();
53         policy.setDbLibService(db);
54     }
55
56     @Test
57     public void testGetPolicyJson() throws SQLException {
58         Mockito.when(rowset.getString(ARTIFACT_CONTENT)).thenReturn(TEST_CONTENT);
59         assertEquals(TEST_CONTENT, policy.getPolicyJson());
60     }
61
62     @Test
63     public void testGetPolicyJsonBlank() throws SQLException {
64         Mockito.when(rowset.next()).thenThrow(new SQLException());
65         expectedEx.expect(RuntimeException.class);
66         policy.getPolicyJson();
67     }
68
69     @Test
70     public void testGetPolicyJsonError() throws SQLException {
71         Mockito.when(rowset.getString(ARTIFACT_CONTENT)).thenReturn("");
72         assertEquals("", policy.getPolicyJson());
73     }
74
75     @Test
76     public void testGetInProgressRuleExecutor() {
77         expectedEx.expect(RuntimeException.class);
78         expectedEx.expectMessage("Rule executor not available, initialization of RequestValidationPolicy failed");
79         policy.getInProgressRuleExecutor();
80     }
81 }