From 5efec2c3cfcead17394beb825200e132f827eb31 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Tue, 30 Jul 2019 18:01:21 +0300 Subject: Fix /version endpoint * Modify maven-replacer-plugin to set value in version.json * Configure features.set.filename=onap.features.properties Issue-ID: VID-507 Change-Id: I692ab3e9221118b836ae7249f483b5277a70bf6b Signed-off-by: Ittay Stern --- .../vid/controller/open/VersionControllerTest.java | 52 -------- .../org/onap/vid/services/VersionServiceTest.java | 136 +++++++++++++++++++++ 2 files changed, 136 insertions(+), 52 deletions(-) delete mode 100644 vid-app-common/src/test/java/org/onap/vid/controller/open/VersionControllerTest.java create mode 100644 vid-app-common/src/test/java/org/onap/vid/services/VersionServiceTest.java (limited to 'vid-app-common/src/test/java/org') diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/open/VersionControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/open/VersionControllerTest.java deleted file mode 100644 index 835ea4a66..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/controller/open/VersionControllerTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.controller.open; - -import org.onap.vid.controller.open.VersionController; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - - -public class VersionControllerTest { - - @DataProvider - public static Object[][] majorVersionContainer() { - return new Object[][]{ - {"features.properties", "1.0.2000", "features.properties.2000"}, - {"", "1.0.2000", ".2000"}, - {"kuku", "1.0.2000", "kuku.2000"}, - {"/kuku", "1.0.2000", "kuku.2000"}, - {"1810p.features.properties", "1.0.2000", "1810p.2000"}, - {"/opt/app/dev.features.properties", "1.0.2000", "dev.2000"}, - {"foo", "2000", "foo.2000"}, - }; - } - - final VersionController versionController = new VersionController(null); - - @Test(dataProvider = "majorVersionContainer") - public void testGetDisplayVersion(String majorVersionContainer, String buildNumberContainer, String expected) { - assertThat(versionController.getDisplayVersion(majorVersionContainer, buildNumberContainer), is(expected)); - } -} diff --git a/vid-app-common/src/test/java/org/onap/vid/services/VersionServiceTest.java b/vid-app-common/src/test/java/org/onap/vid/services/VersionServiceTest.java new file mode 100644 index 000000000..7299ba312 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/services/VersionServiceTest.java @@ -0,0 +1,136 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.services; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.net.MalformedURLException; +import java.net.URL; +import javax.servlet.ServletContext; +import org.jetbrains.annotations.NotNull; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.model.VersionAndFeatures; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +@ContextConfiguration(classes = {SystemProperties.class}) +public class VersionServiceTest extends AbstractTestNGSpringContextTests { + + private static final String VERSION_FILE_PATH = "/app/vid/scripts/constants/version.json"; + @Mock + ServletContext servletContext; + + VersionService versionService; + + @BeforeClass + public void initMocks() { + MockitoAnnotations.initMocks(this); + } + + @BeforeMethod + public void resetMocks() { + Mockito.reset(servletContext); + versionService = new VersionService(servletContext); + } + + @DataProvider + public static Object[][] majorVersionContainer() { + return new Object[][]{ + {"features.properties", "1.0.2000", "features.properties.2000"}, + {"", "1.0.2000", ".2000"}, + {"kuku", "1.0.2000", "kuku.2000"}, + {"/kuku", "1.0.2000", "kuku.2000"}, + {"1810p.features.properties", "1.0.2000", "1810p.2000"}, + {"/opt/app/dev.features.properties", "1.0.2000", "dev.2000"}, + {"foo", "2000", "foo.2000"}, + }; + } + + @Test(dataProvider = "majorVersionContainer") + public void testGetDisplayVersion(String majorVersionContainer, String buildNumberContainer, String expected) { + assertThat(versionService.buildDisplayVersion(majorVersionContainer, buildNumberContainer), is(expected)); + } + + @Test + public void testReadFeatureSet() { + assertEquals("onap.features.properties", versionService.readFeatureSet()); + } + + @Test + public void whenReadBuildNumber_thenTheRightBuildIsReturn_andReadOnlyOnce() throws MalformedURLException { + mockForVersionFile(); + assertEquals("1.0.151", versionService.retrieveBuildNumber()); + //second call shall not read resource + assertEquals("1.0.151", versionService.retrieveBuildNumber()); + verify(servletContext).getResource(any()); + } + + private void mockForVersionFile() throws MalformedURLException { + URL versionFileExample = this.getClass().getResource("/version.example.json"); + when(servletContext.getResource(eq(VERSION_FILE_PATH))).thenReturn(versionFileExample); + } + + @NotNull + protected VersionAndFeatures retrieveAndAssertVersionWithGoodResult() throws MalformedURLException { + mockForVersionFile(); + VersionAndFeatures expected = new VersionAndFeatures("onap.features.properties", "1.0.151", "onap.151"); + assertEquals(expected, versionService.retrieveVersionAndFeatures()); + return expected; + } + + @Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "abc") + public void whenExceptionThrownDuringGetBuildNumber_thenExceptionIsThrown() throws MalformedURLException { + when(servletContext.getResource(any())).thenThrow(new RuntimeException("abc")); + versionService.retrieveBuildNumber(); + } + + @Test + public void whenExceptionThrownDuringVersionAndFeatures_thenUnknownIsReturn() throws MalformedURLException { + //exception is thrown during retrieveVersionAndFeatures, so expect to "unknown" result + when(servletContext.getResource(eq(VERSION_FILE_PATH))).thenThrow(new RuntimeException()); + assertEquals(VersionAndFeatures.Companion.getUnknown(), versionService.retrieveVersionAndFeatures()); + + //retrieveVersionAndFeatures going smoothly, so expecting to good result + retrieveAndAssertVersionWithGoodResult(); + } + + + @Test + public void whenRetrieveVersionAndFeatures_expectedValuesReturn_andExecuteOnce() throws MalformedURLException { + VersionAndFeatures expected = retrieveAndAssertVersionWithGoodResult(); + //second call shall not read resource + assertEquals(expected, versionService.retrieveVersionAndFeatures()); + verify(servletContext).getResource(eq(VERSION_FILE_PATH)); + } +} -- cgit 1.2.3-korg