2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.net.MalformedURLException;
33 import javax.servlet.ServletContext;
34 import org.jetbrains.annotations.NotNull;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.portalsdk.core.util.SystemProperties;
39 import org.onap.vid.model.VersionAndFeatures;
40 import org.springframework.test.context.ContextConfiguration;
41 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.BeforeMethod;
44 import org.testng.annotations.DataProvider;
45 import org.testng.annotations.Test;
47 @ContextConfiguration(classes = {SystemProperties.class})
48 public class VersionServiceTest extends AbstractTestNGSpringContextTests {
50 private static final String VERSION_FILE_PATH = "/app/vid/scripts/constants/version.json";
52 ServletContext servletContext;
54 VersionService versionService;
57 public void initMocks() {
58 MockitoAnnotations.initMocks(this);
62 public void resetMocks() {
63 Mockito.reset(servletContext);
64 versionService = new VersionService(servletContext);
68 public static Object[][] majorVersionContainer() {
69 return new Object[][]{
70 {"features.properties", "1.0.2000", "features.properties.2000"},
71 {"", "1.0.2000", ".2000"},
72 {"kuku", "1.0.2000", "kuku.2000"},
73 {"/kuku", "1.0.2000", "kuku.2000"},
74 {"1810p.features.properties", "1.0.2000", "1810p.2000"},
75 {"/opt/app/dev.features.properties", "1.0.2000", "dev.2000"},
76 {"foo", "2000", "foo.2000"},
80 @Test(dataProvider = "majorVersionContainer")
81 public void testGetDisplayVersion(String majorVersionContainer, String buildNumberContainer, String expected) {
82 assertThat(versionService.buildDisplayVersion(majorVersionContainer, buildNumberContainer), is(expected));
86 public void testReadFeatureSet() {
87 assertEquals("onap.features.properties", versionService.readFeatureSet());
91 public void whenReadBuildNumber_thenTheRightBuildIsReturn_andReadOnlyOnce() throws MalformedURLException {
93 assertEquals("1.0.151", versionService.retrieveBuildNumber());
94 //second call shall not read resource
95 assertEquals("1.0.151", versionService.retrieveBuildNumber());
96 verify(servletContext).getResource(any());
99 private void mockForVersionFile() throws MalformedURLException {
100 URL versionFileExample = this.getClass().getResource("/version.example.json");
101 when(servletContext.getResource(eq(VERSION_FILE_PATH))).thenReturn(versionFileExample);
105 protected VersionAndFeatures retrieveAndAssertVersionWithGoodResult() throws MalformedURLException {
106 mockForVersionFile();
107 VersionAndFeatures expected = new VersionAndFeatures("onap.features.properties", "1.0.151", "onap.151");
108 assertEquals(expected, versionService.retrieveVersionAndFeatures());
112 @Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "abc")
113 public void whenExceptionThrownDuringGetBuildNumber_thenExceptionIsThrown() throws MalformedURLException {
114 when(servletContext.getResource(any())).thenThrow(new RuntimeException("abc"));
115 versionService.retrieveBuildNumber();
119 public void whenExceptionThrownDuringVersionAndFeatures_thenUnknownIsReturn() throws MalformedURLException {
120 //exception is thrown during retrieveVersionAndFeatures, so expect to "unknown" result
121 when(servletContext.getResource(eq(VERSION_FILE_PATH))).thenThrow(new RuntimeException());
122 assertEquals(VersionAndFeatures.Companion.getUnknown(), versionService.retrieveVersionAndFeatures());
124 //retrieveVersionAndFeatures going smoothly, so expecting to good result
125 retrieveAndAssertVersionWithGoodResult();
130 public void whenRetrieveVersionAndFeatures_expectedValuesReturn_andExecuteOnce() throws MalformedURLException {
131 VersionAndFeatures expected = retrieveAndAssertVersionWithGoodResult();
132 //second call shall not read resource
133 assertEquals(expected, versionService.retrieveVersionAndFeatures());
134 verify(servletContext).getResource(eq(VERSION_FILE_PATH));