1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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));
}
}
|