More license header updates to appc-common files
[appc.git] / appc-common / src / test / java / org / onap / appc / metadata / impl / MetadataServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 Nokia Solutions and Networks
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23 package org.onap.appc.metadata.impl;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.sql.SQLException;
35 import java.util.ArrayList;
36 import javax.sql.rowset.CachedRowSet;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.appc.cache.MetadataCache;
40 import org.onap.appc.metadata.objects.DependencyModelIdentifier;
41 import org.onap.ccsdk.sli.core.dblib.DbLibService;
42
43 public class MetadataServiceImplTest {
44
45
46     private MetadataServiceImpl metadataService = new MetadataServiceImpl();
47     private DbLibService mockDbLibService = mock(DbLibService.class);
48     private CachedRowSet mockCachedRowSet = mock(CachedRowSet.class);
49     private MetadataCache<DependencyModelIdentifier, String> mockCache = mock(MetadataCache.class);
50     private DependencyModelIdentifier mockModelIdentifier = mock(DependencyModelIdentifier.class);
51
52     @Before
53     public void setup() throws SQLException {
54         metadataService.setDbLibService(mockDbLibService);
55         metadataService.setCache(mockCache);
56     }
57
58     @Test
59     public void getVnfModel_should_return_vnfModel_when_present_in_cache() throws SQLException {
60         when(mockCache.getObject(mockModelIdentifier)).thenReturn("test-vnf-model");
61
62         assertEquals("test-vnf-model", metadataService.getVnfModel(mockModelIdentifier));
63
64         verify(mockCache).getObject(mockModelIdentifier);
65         verify(mockDbLibService, never()).getData(anyString(), any(ArrayList.class), anyString());
66         verify(mockCache, never()).putObject(any(DependencyModelIdentifier.class), anyString());
67     }
68
69     @Test
70     public void getVnfModel_should_read_from_database_when_null_vnfName_and_return_when_found()
71         throws SQLException {
72
73         when(mockCache.getObject(any(DependencyModelIdentifier.class))).thenReturn(null);
74         when(mockModelIdentifier.getCatalogVersion()).thenReturn("test-vnf-catalog-version");
75         when(mockDbLibService.getData(anyString(), any(ArrayList.class), anyString())).thenReturn(mockCachedRowSet);
76         when(mockCachedRowSet.first()).thenReturn(true);
77         when(mockCachedRowSet.getString("ARTIFACT_CONTENT")).thenReturn("test-vnf-model");
78
79         assertEquals("test-vnf-model", metadataService.getVnfModel(mockModelIdentifier));
80
81         verify(mockDbLibService).getData(anyString(), any(ArrayList.class), anyString());
82         verify(mockCachedRowSet).getString("ARTIFACT_CONTENT");
83         verify(mockCache).putObject(mockModelIdentifier, "test-vnf-model");
84     }
85
86     @Test(expected = RuntimeException.class)
87     public void getVnfModel_should_read_from_database_when_null_vnfName_and_throw_when_invalid_dependency_model()
88         throws SQLException {
89
90         when(mockCache.getObject(any(DependencyModelIdentifier.class))).thenReturn(null);
91         when(mockModelIdentifier.getCatalogVersion()).thenReturn(null);
92
93         when(mockDbLibService.getData(anyString(), any(ArrayList.class), anyString())).thenReturn(mockCachedRowSet);
94         when(mockCachedRowSet.first()).thenReturn(true);
95         when(mockCachedRowSet.getString("ARTIFACT_CONTENT")).thenReturn(null);
96
97         assertEquals(null, metadataService.getVnfModel(mockModelIdentifier));
98
99         verify(mockDbLibService).getData(anyString(), any(ArrayList.class), anyString());
100         verify(mockCachedRowSet).getString("ARTIFACT_CONTENT");
101         verify(mockCache, never()).putObject(mockModelIdentifier, "test-vnf-model");
102
103     }
104
105     @Test(expected = RuntimeException.class)
106     public void getVnfModel_should_read_from_database_when_null_vnfName_and_throw_when_database_error()
107         throws SQLException {
108
109         when(mockCache.getObject(any(DependencyModelIdentifier.class))).thenReturn(null);
110         when(mockModelIdentifier.getCatalogVersion()).thenReturn(null);
111         when(mockDbLibService.getData(anyString(), any(ArrayList.class), anyString())).thenThrow(new SQLException());
112
113         assertEquals(null, metadataService.getVnfModel(mockModelIdentifier));
114
115         verify(mockDbLibService).getData(anyString(), any(ArrayList.class), anyString());
116         verify(mockCachedRowSet, times(0)).getString("ARTIFACT_CONTENT");
117         verify(mockCache, never()).putObject(any(DependencyModelIdentifier.class), anyString());
118     }
119
120
121     @Test
122     public void getVnfModel_should_read_from_database_when_null_vnfName_and_return_null_when_not_found()
123         throws SQLException {
124
125         when(mockCache.getObject(any(DependencyModelIdentifier.class))).thenReturn(null);
126         when(mockModelIdentifier.getCatalogVersion()).thenReturn(null);
127         when(mockDbLibService.getData(anyString(), any(ArrayList.class), anyString())).thenReturn(mockCachedRowSet);
128         when(mockCachedRowSet.first()).thenReturn(false);
129
130         assertEquals(null, metadataService.getVnfModel(mockModelIdentifier));
131
132         verify(mockDbLibService).getData(anyString(), any(ArrayList.class), anyString());
133         verify(mockCachedRowSet, times(0)).getString("ARTIFACT_CONTENT");
134         verify(mockCache, never()).putObject(any(DependencyModelIdentifier.class), anyString());
135     }
136
137 }