Skip to content

Commit 47c4e42

Browse files
authored
Change handling of backend instances (#248)
- It will now only be possible to add instances in the application.properties file. - Fix relevant tests
1 parent 8c64e8b commit 47c4e42

39 files changed

+481
-1511
lines changed

src/functionaltest/java/com/ericsson/ei/config/SeleniumConfig.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package com.ericsson.ei.config;
22

3-
import com.ericsson.ei.frontend.exception.OSNotSupportedException;
4-
import com.ericsson.ei.frontend.exception.PropertiesNotLoadedException;
5-
import com.google.common.io.Files;
6-
import org.apache.commons.lang3.SystemUtils;
7-
import org.openqa.selenium.firefox.*;
3+
import java.io.File;
4+
import java.util.Properties;
85

9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
116
import org.apache.commons.io.FilenameUtils;
127
import org.apache.commons.lang3.StringUtils;
8+
import org.apache.commons.lang3.SystemUtils;
9+
import org.openqa.selenium.firefox.FirefoxBinary;
10+
import org.openqa.selenium.firefox.FirefoxDriver;
11+
import org.openqa.selenium.firefox.FirefoxDriverLogLevel;
12+
import org.openqa.selenium.firefox.FirefoxOptions;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1315

14-
import java.util.Properties;
15-
16-
import java.io.File;
16+
import com.ericsson.ei.frontend.exception.OSNotSupportedException;
17+
import com.ericsson.ei.frontend.exception.PropertiesNotLoadedException;
18+
import com.google.common.io.Files;
1719

1820
public class SeleniumConfig {
1921
private static final Logger LOGGER = LoggerFactory.getLogger(SeleniumConfig.class);
@@ -27,10 +29,6 @@ public class SeleniumConfig {
2729
private static FirefoxDriver driver;
2830

2931
public static FirefoxDriver initFirefoxDriver() throws PropertiesNotLoadedException, OSNotSupportedException {
30-
if (driver != null) {
31-
return driver;
32-
}
33-
3432
FirefoxOptions firefoxOptions = new FirefoxOptions()
3533
.setHeadless(true)
3634
.setLogLevel(FirefoxDriverLogLevel.ERROR);

src/functionaltest/java/com/ericsson/ei/frontend/SeleniumBaseClass.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.mockito.Mockito;
2424
import org.mockito.MockitoAnnotations;
2525
import org.openqa.selenium.firefox.FirefoxDriver;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628
import org.springframework.beans.factory.annotation.Autowired;
2729
import org.springframework.boot.test.context.SpringBootTest;
2830
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -34,6 +36,8 @@
3436
@RunWith(SpringJUnit4ClassRunner.class)
3537
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
3638
public class SeleniumBaseClass extends TestBaseClass {
39+
private static final Logger LOG = LoggerFactory.getLogger(SeleniumBaseClass.class);
40+
3741
@Autowired
3842
@InjectMocks
3943
EIRequestsController eIRequestsController;
@@ -52,15 +56,18 @@ public void setUp() throws Exception {
5256

5357
@After
5458
public void tearDown() throws Exception {
59+
if (driver != null) {
60+
LOG.info("Closing firefoxdriver.");
61+
driver.quit();
62+
}
63+
5564
File tempDownloadDirectory = SeleniumConfig.getTempDownloadDirectory();
5665
FileUtils.deleteDirectory(tempDownloadDirectory);
5766

5867
String verificationErrorString = verificationErrors.toString();
5968
if (!verificationErrorString.equals("")) {
6069
fail(verificationErrorString);
6170
}
62-
63-
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
6471
}
6572

6673
protected void initBaseMocks(CloseableHttpClient mockedHttpClient) throws ClientProtocolException, IOException {

src/functionaltest/java/com/ericsson/ei/frontend/TestBaseClass.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ericsson.ei.frontend;
22

3-
import java.io.File;
43
import java.io.IOException;
54
import java.nio.charset.StandardCharsets;
65
import java.nio.file.Files;
@@ -18,9 +17,10 @@
1817
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1918
import org.springframework.test.web.servlet.MockMvc;
2019

21-
import com.ericsson.ei.frontend.utils.BackEndInstanceFileUtils;
22-
import com.ericsson.ei.frontend.utils.BackEndInstancesUtils;
20+
import com.ericsson.ei.frontend.model.BackendInstance;
21+
import com.ericsson.ei.frontend.utils.BackendInstancesHandler;
2322
import com.ericsson.ei.frontend.utils.WebControllerUtils;
23+
import com.google.gson.JsonArray;
2424

2525
@Ignore
2626
@RunWith(SpringJUnit4ClassRunner.class)
@@ -36,30 +36,34 @@ public class TestBaseClass {
3636
private String filePath = "";
3737

3838
@Autowired
39-
protected BackEndInstanceFileUtils backEndInstanceFileUtils;
40-
41-
@Autowired
42-
protected BackEndInstancesUtils backEndInstancesUtils;
39+
protected BackendInstancesHandler backendInstancesUtils;
4340

4441
@Autowired
4542
protected MockMvc mockMvc;
4643

4744
@PostConstruct
4845
public void init() throws Exception {
49-
File tempFile = File.createTempFile("tempfile", ".json");
50-
tempFile.deleteOnExit();
51-
52-
filePath = tempFile.getAbsolutePath().toString();
53-
Files.write(Paths.get(filePath), "[]".getBytes());
54-
backEndInstanceFileUtils.setEiInstancesPath(filePath);
55-
56-
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
57-
backEndInstancesUtils.setDefaultBackEndInstance("test", "localhost", 12345, "", true);
58-
5946
webControllerUtils.setFrontendServicePort(testServerPort);
6047
}
6148

6249
protected static String getJSONStringFromFile(String filepath) throws IOException {
6350
return new String(Files.readAllBytes(Paths.get(filepath)), StandardCharsets.UTF_8).replaceAll("[\\r\\n ]", "");
6451
}
52+
53+
protected void setBackendInstance(String name, String host, int port, String contextPath, boolean isDefaultBackend) {
54+
BackendInstance backendInstance = new BackendInstance();
55+
backendInstance.setName(name);
56+
backendInstance.setHost(host);
57+
backendInstance.setPort(Integer.toString(port));
58+
backendInstance.setContextPath(contextPath);
59+
backendInstance.setDefaultBackend(true);
60+
61+
JsonArray backendInstances = new JsonArray();
62+
backendInstances.add(backendInstance.getAsJsonObject());
63+
backendInstancesUtils.setBackendInstances(backendInstances);
64+
}
65+
66+
protected void setBackendInstances(JsonArray backendInstances) {
67+
backendInstancesUtils.setBackendInstances(backendInstances);
68+
}
6569
}

src/functionaltest/java/com/ericsson/ei/frontend/TestBridgeCurlFunctions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.mockserver.client.MockServerClient;
2323
import org.mockserver.integration.ClientAndServer;
2424

25-
public class TestBridgeCurlFunctions extends TestBaseClass {
25+
public class TestBridgeCurlFunctions extends SeleniumBaseClass {
2626
private static MockServerClient mockClient1;
2727
private static MockServerClient mockClient2;
2828
private static ClientAndServer mockServer1;
@@ -36,7 +36,9 @@ public class TestBridgeCurlFunctions extends TestBaseClass {
3636

3737
@Before
3838
public void before() throws Exception {
39-
backEndInstancesUtils.setDefaultBackEndInstance("test", BASE_URL, mockServer1.getLocalPort(), "", true);
39+
int serverPort = mockServer1.getLocalPort();
40+
41+
setBackendInstance("test", BASE_URL, serverPort, "", true);
4042
setupMockEndpoints();
4143
}
4244

src/functionaltest/java/com/ericsson/ei/frontend/TestNavigation.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public void testPageNavigationButtons() throws IOException {
2828
indexPage.clickEiInfoBtn();
2929
indexPage.clickInformationBtn();
3030
indexPage.clickRulesBtn();
31-
indexPage.clickAdminBackendInstancesBtn();
32-
indexPage.clickAddBackendInstanceBtn();
3331
indexPage.clickSwitchBackendInstanceBtn();
3432
indexPage.clickSubscriptionPage();
3533
}

src/functionaltest/java/com/ericsson/ei/frontend/TestRulesFunctionality.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.ericsson.ei.config.SeleniumConfig;
2424
import com.ericsson.ei.frontend.pageobjects.TestRulesPage;
2525

26+
2627
public class TestRulesFunctionality extends SeleniumBaseClass {
2728

2829
private static MockServerClient mockClient;
@@ -52,8 +53,8 @@ public class TestRulesFunctionality extends SeleniumBaseClass {
5253
@Before
5354
public void before() throws IOException {
5455
int serverPort = mockServer.getLocalPort();
55-
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
56-
backEndInstancesUtils.setDefaultBackEndInstance("new_instance_default", "localhost", serverPort, "", true);
56+
setBackendInstance("new_instance_default", "localhost", serverPort, "", true);
57+
5758
testRulesPage = new TestRulesPage(null, driver, baseUrl);
5859
testRulesPage.loadPage();
5960
}
@@ -75,6 +76,8 @@ public void testJourneyToFindAggregatedObjectButton() throws Exception {
7576
public static void setUpMocks() throws IOException {
7677
mockServer = startClientAndServer();
7778
mockClient = new MockServerClient(BASE_URL, mockServer.getLocalPort());
79+
mockClient.when(request().withMethod("GET").withPath("/auth/checkStatus"))
80+
.respond(response().withStatusCode(200));
7881
}
7982

8083
@AfterClass

src/functionaltest/java/com/ericsson/ei/frontend/TestSubscriptionCRUD.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import com.google.gson.JsonParser;
2323

24-
public class TestSubscriptionCRUD extends TestBaseClass {
24+
public class TestSubscriptionCRUD extends SeleniumBaseClass {
2525
private static final String SUBSCRIPTION_ENDPOINT = "/subscriptions";
2626
private static final String SUBSCRIPTION_DELETE_ENDPOINT = "/subscriptions/Subscription_1";
2727
private static final String SUBSCRIPTION_FILE_PATH = "src/functionaltest/resources/responses/subscription.json";
@@ -42,7 +42,9 @@ public class TestSubscriptionCRUD extends TestBaseClass {
4242
@Override
4343
@Before
4444
public void init() throws Exception {
45-
backEndInstancesUtils.setDefaultBackEndInstance("test", "localhost", mockServerRule.getPort(), "", false);
45+
int serverPort = mockServerRule.getPort();
46+
setBackendInstance("test", "localhost", serverPort, "", false);
47+
4648
subscriptionRequestBody = getJSONStringFromFile(SUBSCRIPTION_FILE_PATH);
4749

4850
String auth = USERNAME + ":" + PASSWORD;

src/functionaltest/java/com/ericsson/ei/frontend/TestSubscriptionHandling.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ public static void setUpMocks() throws IOException {
7676

7777
@Before
7878
public void before() throws IOException {
79-
int portServer = mockServer.getLocalPort();
80-
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
81-
backEndInstancesUtils.setDefaultBackEndInstance("new_instance_default", "localhost", portServer, "", true);
79+
int serverPort = mockServer.getLocalPort();
80+
setBackendInstance("new_instance_default", "localhost", serverPort, "", true);
8281
subscriptionPage = new SubscriptionPage(mockedHttpClient, driver, baseUrl);
8382
}
8483

src/functionaltest/java/com/ericsson/ei/frontend/TestSwitchBackend.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.io.File;
99
import java.io.IOException;
1010

11-
import org.apache.http.client.ClientProtocolException;
1211
import org.apache.http.impl.client.CloseableHttpClient;
1312
import org.junit.AfterClass;
1413
import org.junit.Before;
@@ -18,9 +17,10 @@
1817
import org.mockserver.integration.ClientAndServer;
1918
import org.springframework.boot.test.mock.mockito.MockBean;
2019

21-
import com.ericsson.ei.frontend.pageobjects.AddBackendPage;
20+
import com.ericsson.ei.frontend.model.BackendInstance;
2221
import com.ericsson.ei.frontend.pageobjects.SubscriptionPage;
2322
import com.ericsson.ei.frontend.pageobjects.SwitchBackendPage;
23+
import com.google.gson.JsonArray;
2424

2525
public class TestSwitchBackend extends SeleniumBaseClass {
2626
private static MockServerClient mockClient1;
@@ -38,28 +38,22 @@ public class TestSwitchBackend extends SeleniumBaseClass {
3838
@MockBean
3939
protected CloseableHttpClient mockedHttpClient;
4040

41-
private AddBackendPage addBackendPage;
4241
private SwitchBackendPage switchBackendPage;
4342
private SubscriptionPage subscriptionPage;
4443

4544
@Before
4645
public void before() throws IOException {
47-
int portServer1 = mockServer1.getLocalPort();
48-
backEndInstancesUtils.setDefaultBackEndInstanceToNull();
49-
backEndInstancesUtils.setDefaultBackEndInstance("new_instance_default", "localhost", portServer1, "", true);
50-
addBackendPage = new AddBackendPage(mockedHttpClient, driver, baseUrl);
46+
addBackendInstances();
5147
switchBackendPage = new SwitchBackendPage(mockedHttpClient, driver, baseUrl);
5248
subscriptionPage = new SubscriptionPage(mockedHttpClient, driver, baseUrl);
5349
}
5450

5551
@Test
5652
public void testSwitchBackend() throws Exception {
57-
addAndVerifySecondBackendInstance();
5853
switchToSecondBackendInstance();
5954
verifySubscriptionForSecondBackendInstance();
6055
switchToFirstBackendInstance();
6156
verifySubscriptionForFirstBackendInstance();
62-
removeAndVerifySecondBackendInstance();
6357
}
6458

6559
@BeforeClass
@@ -82,20 +76,6 @@ public static void tearDownMocks() throws IOException {
8276
mockServer2.stop();
8377
}
8478

85-
private void addAndVerifySecondBackendInstance() throws ClientProtocolException, IOException {
86-
addBackendPage.loadPage();
87-
addSecondBackendInstance();
88-
switchBackendPage.loadPage();
89-
verifyAddedBackendInstance();
90-
}
91-
92-
private void removeAndVerifySecondBackendInstance() {
93-
switchBackendPage.loadPage();
94-
switchBackendPage.removeInstanceNumber(1);
95-
assertEquals("switchBackendPage.presenceOfInstance returned true when it should have been false", false,
96-
switchBackendPage.presenceOfInstance(1));
97-
}
98-
9979
private void switchToSecondBackendInstance() {
10080
switchBackendInstance(1);
10181
}
@@ -122,13 +102,28 @@ private void verifySubscription(String subscription) {
122102
assertEquals(subscription, subscriptionPage.getSubscriptionNameFromSubscription());
123103
}
124104

125-
private void verifyAddedBackendInstance() {
126-
assertEquals("new_instance", switchBackendPage.getInstanceNameAtPosition(1));
127-
}
128-
129-
private void addSecondBackendInstance() throws ClientProtocolException, IOException {
130-
int portServer2 = mockServer2.getLocalPort();
131-
addBackendPage.addBackendInstance("new_instance", BASE_URL, portServer2, "");
105+
private void addBackendInstances() {
106+
int server1Port = mockServer1.getLocalPort();
107+
108+
BackendInstance backend1 = new BackendInstance();
109+
backend1.setName("new_instance_default");
110+
backend1.setHost("localhost");
111+
backend1.setPort(Integer.toString(server1Port));
112+
backend1.setContextPath("");
113+
backend1.setDefaultBackend(true);
114+
115+
int server2Port = mockServer2.getLocalPort();
116+
BackendInstance backend2 = new BackendInstance();
117+
backend2.setName("new_instance");
118+
backend2.setHost(BASE_URL);
119+
backend2.setPort(Integer.toString(server2Port));
120+
backend2.setContextPath("");
121+
backend2.setDefaultBackend(true);
122+
123+
JsonArray backendInstances = new JsonArray();
124+
backendInstances.add(backend1.getAsJsonObject());
125+
backendInstances.add(backend2.getAsJsonObject());
126+
setBackendInstances(backendInstances);
132127
}
133128

134129
private static void setupMockEndpoints() throws IOException {

src/functionaltest/java/com/ericsson/ei/frontend/pageobjects/AddBackendPage.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)