Skip to content

Commit 2360625

Browse files
Unit test
1 parent c2e0a20 commit 2360625

File tree

8 files changed

+421
-36
lines changed

8 files changed

+421
-36
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@
9292
<artifactId>quartz-jobs</artifactId>
9393
<version>2.2.1</version>
9494
</dependency>
95+
96+
<dependency>
97+
<groupId>org.springframework</groupId>
98+
<artifactId>spring-test</artifactId>
99+
<scope>test</scope>
100+
</dependency>
101+
102+
<dependency>
103+
<groupId>org.mockito</groupId>
104+
<artifactId>mockito-all</artifactId>
105+
<version>1.10.19</version>
106+
<scope>test</scope>
107+
</dependency>
95108

96109
</dependencies>
97110
</project>

src/main/java/com/nexttimespace/cdnservice/MainApplication.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import java.net.URISyntaxException;
66
import java.sql.SQLException;
77
import java.util.Arrays;
8-
import java.util.LinkedHashMap;
98
import java.util.List;
10-
import java.util.Map;
119
import java.util.Properties;
1210

1311
import org.apache.commons.lang3.StringUtils;
@@ -51,31 +49,35 @@ public static boolean validateConfiguration(Properties appConf) {
5149
return isValid;
5250
}
5351

54-
public static boolean validateServer(Properties appConf) {
52+
private static boolean validateServer(Properties appConf) {
5553
boolean isValid = true;
56-
String httpPort = appConf.get("server.http.port").toString();
57-
String sslConfError = "";
58-
if(!StringUtils.isNumeric(httpPort)) {
54+
if(appConf != null) {
55+
String httpPort = appConf.get("server.http.port").toString();
56+
String sslConfError = "";
57+
if(!StringUtils.isNumeric(httpPort)) {
58+
isValid = false;
59+
logger.error("Invalid data for server.http.port");
60+
}
61+
62+
if(!StringUtils.isNumeric(appConf.get("server.ssl-config.port").toString())) {
63+
sslConfError = "server.ssl-config.port";
64+
} else if(!(new File(appConf.get("server.ssl-config.key-store").toString()).exists() && new File(appConf.get("server.ssl-config.key-store").toString()).isFile())) {
65+
sslConfError = "server.ssl-config.key-store";
66+
} else if(!(new File(appConf.get("server.ssl-config.trust-store").toString()).exists() && new File(appConf.get("server.ssl-config.key-store").toString()).isFile())) {
67+
sslConfError = "server.ssl-config.trust-store";
68+
} else if(StringUtils.isBlank("server.ssl-config.key-store-password")) {
69+
sslConfError = "server.ssl-config.key-store-password";
70+
} else if(StringUtils.isBlank("server.ssl-config.trust-store-password")) {
71+
sslConfError = "server.ssl-config.trust-store-password";
72+
}
73+
74+
if(!sslConfError.isEmpty()) {
75+
logger.error("Invalid data for "+ sslConfError + ", publish will be disabled");
76+
}
77+
} else {
5978
isValid = false;
60-
logger.error("Invalid data for server.http.port");
79+
logger.error("Invalid data for app.conf");
6180
}
62-
63-
if(!StringUtils.isNumeric(appConf.get("server.ssl-config.port").toString())) {
64-
sslConfError = "server.ssl-config.port";
65-
} else if(!(new File(appConf.get("server.ssl-config.key-store").toString()).exists() && new File(appConf.get("server.ssl-config.key-store").toString()).isFile())) {
66-
sslConfError = "server.ssl-config.key-store";
67-
} else if(!(new File(appConf.get("server.ssl-config.trust-store").toString()).exists() && new File(appConf.get("server.ssl-config.key-store").toString()).isFile())) {
68-
sslConfError = "server.ssl-config.trust-store";
69-
} else if(StringUtils.isBlank("server.ssl-config.key-store-password")) {
70-
sslConfError = "server.ssl-config.key-store-password";
71-
} else if(StringUtils.isBlank("server.ssl-config.trust-store-password")) {
72-
sslConfError = "server.ssl-config.trust-store-password";
73-
}
74-
75-
if(!sslConfError.isEmpty()) {
76-
logger.error("Invalid data for "+ sslConfError + ", publish will be disabled");
77-
}
78-
7981
return isValid;
8082
}
8183
public static boolean validateRepo(Properties appConf) {
@@ -161,7 +163,7 @@ private static boolean validateCacheManager(Properties appConf, String readerKey
161163

162164
private static boolean validateResponseHeader(String header) {
163165
boolean isValid = true;
164-
if(header != null) {
166+
if(header != null && !header.isEmpty()) {
165167
String[] mainString = header.split("\\|");
166168
if(mainString.length > 0) {
167169
for(String headerString : mainString) {

src/main/java/com/nexttimespace/cdnservice/config/ApplicationConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class ApplicationConfig {
1616

1717
@Autowired
18-
UtilityComponent utilityComponent;
18+
private UtilityComponent utilityComponent;
1919

2020
@Bean
2121
public EmbeddedServletContainerFactory servletContainer() {

src/main/java/com/nexttimespace/cdnservice/controller/ServeContentController.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.nexttimespace.cdnservice.controller;
1616

17-
import java.io.IOException;
1817
import java.util.List;
1918

2019
import javax.annotation.PostConstruct;
@@ -27,17 +26,13 @@
2726
import org.springframework.http.HttpHeaders;
2827
import org.springframework.http.HttpStatus;
2928
import org.springframework.http.ResponseEntity;
30-
import org.springframework.stereotype.Component;
3129
import org.springframework.stereotype.Controller;
3230
import org.springframework.web.bind.annotation.PathVariable;
3331
import org.springframework.web.bind.annotation.RequestMapping;
3432
import org.springframework.web.bind.annotation.RequestMethod;
3533
import org.springframework.web.bind.annotation.RequestParam;
3634
import org.springframework.web.bind.annotation.ResponseBody;
37-
import org.springframework.web.multipart.MultipartFile;
38-
3935
import com.nexttimespace.cdnservice.publisher.MasterPublisher;
40-
import com.nexttimespace.cdnservice.reader.DirectoryReader;
4136
import com.nexttimespace.cdnservice.reader.MasterReader;
4237
import com.nexttimespace.cdnservice.reader.ReaderUtility;
4338
import com.nexttimespace.cdnservice.reader.TrafficRouter;
@@ -48,8 +43,8 @@
4843
@Controller
4944
public class ServeContentController {
5045

51-
int httpPort;
52-
int httpsPort;
46+
private int httpPort;
47+
private int httpsPort;
5348

5449
@PostConstruct
5550
public void setup() {
@@ -77,14 +72,14 @@ public ResponseEntity<String> publishToDirectory(@PathVariable String alias, @Re
7772
try {
7873
if(request.getServerPort() == httpsPort) {
7974
Part filePart = request.getPart("file");
80-
if(filePart != null && path != null && !path.isEmpty()) {
75+
if(filePart != null && path != null && !path.isEmpty() && alias != null && !alias.isEmpty()) {
8176
if(masterPublisher.publish(filePart.getInputStream(), alias, path)) {
8277
responseEntity = new ResponseEntity<String>("Published successfully", HttpStatus.OK);
8378
} else {
8479
responseEntity = new ResponseEntity<String>("Publish disabled", HttpStatus.OK);
8580
}
8681
} else {
87-
responseEntity = new ResponseEntity<String>("file or path request attribute missing", HttpStatus.BAD_REQUEST);
82+
responseEntity = new ResponseEntity<String>("file or path or alias request attribute missing", HttpStatus.BAD_REQUEST);
8883
}
8984

9085
} else {
@@ -93,7 +88,7 @@ public ResponseEntity<String> publishToDirectory(@PathVariable String alias, @Re
9388

9489
} catch (Exception e) {
9590
logger.error("Error on publish: ", e);
96-
responseEntity = new ResponseEntity<String>("", HttpStatus.INTERNAL_SERVER_ERROR);
91+
responseEntity = new ResponseEntity<String>("Publish failed due to internal error", HttpStatus.INTERNAL_SERVER_ERROR);
9792
}
9893
return responseEntity;
9994
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.nexttimespace.cdnservice;
2+
3+
import java.util.Properties;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class MainApplicationTest {
9+
10+
@Test
11+
public void validateConfigurationTest() {
12+
Properties properties = null;
13+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
14+
15+
properties = TestUtils.getClonedPropety();
16+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
17+
}
18+
19+
@Test
20+
public void validateConfigurationTestServerConfigNegativeCase() {
21+
Properties properties = TestUtils.getClonedPropety();
22+
properties.setProperty("server.http.port", "abc");
23+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
24+
25+
properties = TestUtils.getClonedPropety();
26+
properties.setProperty("server.ssl-config.port", "abc");
27+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
28+
29+
properties = TestUtils.getClonedPropety();
30+
properties.setProperty("server.ssl-config.key-store", "abc");
31+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
32+
33+
properties = TestUtils.getClonedPropety();
34+
properties.setProperty("server.ssl-config.trust-store", "abc");
35+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
36+
37+
properties = TestUtils.getClonedPropety();
38+
properties.setProperty("server.ssl-config.key-store-password", "");
39+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
40+
41+
properties = TestUtils.getClonedPropety();
42+
properties.setProperty("server.ssl-config.trust-store-password", "");
43+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
44+
}
45+
46+
@Test
47+
public void validateConfigurationTestRepoConfig() {
48+
Properties properties = TestUtils.getClonedPropety();
49+
properties.setProperty("repo[0].type", "abc");
50+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
51+
52+
properties = TestUtils.getClonedPropety();
53+
properties.setProperty("repo[0].alias", "");
54+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
55+
56+
properties = TestUtils.getClonedPropety();
57+
properties.setProperty("repo[0].allow-publish", "");
58+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
59+
60+
properties = TestUtils.getClonedPropety();
61+
properties.setProperty("repo[0].allow-publish", "true");
62+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
63+
64+
properties = TestUtils.getClonedPropety();
65+
properties.setProperty("repo[0].allow-publish", "false");
66+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
67+
68+
properties = TestUtils.getClonedPropety();
69+
properties.setProperty("repo[0].traffic", "abc");
70+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
71+
72+
properties = TestUtils.getClonedPropety();
73+
properties.setProperty("repo[0].traffic", "80");
74+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
75+
76+
properties = TestUtils.getClonedPropety();
77+
properties.setProperty("repo[0].directory.path", "abc");
78+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
79+
80+
properties = TestUtils.getClonedPropety();
81+
properties.setProperty("repo[0].response.header", "abc");
82+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
83+
84+
properties = TestUtils.getClonedPropety();
85+
properties.setProperty("repo[0].response.header", "");
86+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
87+
88+
properties = TestUtils.getClonedPropety();
89+
properties.setProperty("repo[0].response.header", "|");
90+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
91+
92+
properties = TestUtils.getClonedPropety();
93+
properties.setProperty("repo[0].response.header", "abc|cdb");
94+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
95+
96+
properties = TestUtils.getClonedPropety();
97+
properties.setProperty("repo[0].response.header", "abc:|cdb:");
98+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
99+
100+
properties = TestUtils.getClonedPropety();
101+
properties.remove("repo[0].response.header");
102+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
103+
104+
properties = TestUtils.getClonedPropety();
105+
properties.setProperty("repo[0].cache-manager.enable", "false");
106+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
107+
108+
properties = TestUtils.getClonedPropety();
109+
properties.setProperty("repo[0].cache-manager.enable", "abc");
110+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
111+
112+
properties = TestUtils.getClonedPropety();
113+
properties.remove("repo[0].cache-manager.enable");
114+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
115+
116+
properties = TestUtils.getClonedPropety();
117+
properties.setProperty("repo[0].cache-manager.type", "abc");
118+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
119+
120+
121+
properties = TestUtils.getClonedPropety();
122+
properties.setProperty("repo[0].cache-manager.clear-strategy.type", "abc");
123+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
124+
125+
properties = TestUtils.getClonedPropety();
126+
properties.remove("repo[0].cache-manager.clear-strategy.type");
127+
Assertions.assertTrue(MainApplication.validateConfiguration(properties));
128+
129+
properties = TestUtils.getClonedPropety();
130+
properties.setProperty("repo[0].cache-manager.clear-strategy.tic", "abc");
131+
Assertions.assertFalse(MainApplication.validateConfiguration(properties));
132+
133+
}
134+
135+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.nexttimespace.cdnservice;
2+
3+
import java.util.Properties;
4+
5+
import org.apache.commons.lang3.SerializationUtils;
6+
import org.junit.jupiter.api.BeforeAll;
7+
8+
public class TestUtils {
9+
private static Properties goodProperty = new Properties();
10+
static {
11+
goodProperty.setProperty("server.http.port", "8080");
12+
goodProperty.setProperty("server.ssl-config.port", "8443");
13+
goodProperty.setProperty("server.ssl-config.key-store", "pom.xml");
14+
goodProperty.setProperty("server.ssl-config.trust-store", "pom.xml");
15+
goodProperty.setProperty("server.ssl-config.key-store-password", "pass");
16+
goodProperty.setProperty("server.ssl-config.trust-store-password", "pass");
17+
goodProperty.setProperty("repo[0].type", "directory");
18+
goodProperty.setProperty("repo[0].alias", "cdn1");
19+
goodProperty.setProperty("repo[0].allow-publish", "true");
20+
goodProperty.setProperty("repo[0].traffic", "100");
21+
goodProperty.setProperty("repo[0].directory.path", "/");
22+
goodProperty.setProperty("repo[0].response.header", "app:beta|app1:beta1");
23+
goodProperty.setProperty("repo[0].cache-manager.enable", "true");
24+
goodProperty.setProperty("repo[0].cache-manager.type", "in-memory");
25+
goodProperty.setProperty("repo[0].cache-manager.clear-strategy.type", "timer");
26+
goodProperty.setProperty("repo[0].cache-manager.clear-strategy.tic", "1000");
27+
}
28+
29+
public static Properties getClonedPropety() {
30+
return SerializationUtils.clone(goodProperty);
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.nexttimespace.cdnservice.config;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
7+
import com.nexttimespace.cdnservice.TestUtils;
8+
import com.nexttimespace.cdnservice.utility.UtilityComponent;
9+
10+
public class ApplicationConfigTest {
11+
12+
ApplicationConfig applicationConfig = new ApplicationConfig();
13+
@Test
14+
public void servletContainerTest() {
15+
UtilityComponent utilityComponent = new UtilityComponent();
16+
17+
ReflectionTestUtils.setField(utilityComponent, "confProperties", TestUtils.getClonedPropety());
18+
19+
ReflectionTestUtils.setField(applicationConfig, "utilityComponent", utilityComponent);
20+
Assertions.assertNotNull(applicationConfig.servletContainer());
21+
}
22+
23+
}

0 commit comments

Comments
 (0)