2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
23 package org.onap.appc.metadata.impl;
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;
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;
43 public class MetadataServiceImplTest {
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);
53 public void setup() throws SQLException {
54 metadataService.setDbLibService(mockDbLibService);
55 metadataService.setCache(mockCache);
59 public void getVnfModel_should_return_vnfModel_when_present_in_cache() throws SQLException {
60 when(mockCache.getObject(mockModelIdentifier)).thenReturn("test-vnf-model");
62 assertEquals("test-vnf-model", metadataService.getVnfModel(mockModelIdentifier));
64 verify(mockCache).getObject(mockModelIdentifier);
65 verify(mockDbLibService, never()).getData(anyString(), any(ArrayList.class), anyString());
66 verify(mockCache, never()).putObject(any(DependencyModelIdentifier.class), anyString());
70 public void getVnfModel_should_read_from_database_when_null_vnfName_and_return_when_found()
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");
79 assertEquals("test-vnf-model", metadataService.getVnfModel(mockModelIdentifier));
81 verify(mockDbLibService).getData(anyString(), any(ArrayList.class), anyString());
82 verify(mockCachedRowSet).getString("ARTIFACT_CONTENT");
83 verify(mockCache).putObject(mockModelIdentifier, "test-vnf-model");
86 @Test(expected = RuntimeException.class)
87 public void getVnfModel_should_read_from_database_when_null_vnfName_and_throw_when_invalid_dependency_model()
90 when(mockCache.getObject(any(DependencyModelIdentifier.class))).thenReturn(null);
91 when(mockModelIdentifier.getCatalogVersion()).thenReturn(null);
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);
97 assertEquals(null, metadataService.getVnfModel(mockModelIdentifier));
99 verify(mockDbLibService).getData(anyString(), any(ArrayList.class), anyString());
100 verify(mockCachedRowSet).getString("ARTIFACT_CONTENT");
101 verify(mockCache, never()).putObject(mockModelIdentifier, "test-vnf-model");
105 @Test(expected = RuntimeException.class)
106 public void getVnfModel_should_read_from_database_when_null_vnfName_and_throw_when_database_error()
107 throws SQLException {
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());
113 assertEquals(null, metadataService.getVnfModel(mockModelIdentifier));
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());
122 public void getVnfModel_should_read_from_database_when_null_vnfName_and_return_null_when_not_found()
123 throws SQLException {
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);
130 assertEquals(null, metadataService.getVnfModel(mockModelIdentifier));
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());