Skip to content

Commit a57cb79

Browse files
authored
Reduce and improve logs during test executions (#313)
1 parent 2b1cca1 commit a57cb79

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,13 @@ public ResponseEntity<?> getSubscriptionByNames(@PathVariable String subscriptio
114114
foundSubscriptionList.add(subscription);
115115
LOGGER.debug("Subscription [{}] fetched successfully.", subscriptionName);
116116
} catch (SubscriptionNotFoundException e) {
117-
LOGGER.error("Subscription not found: {}",subscriptionName , e);
117+
LOGGER.error("Subscription not found: {}", subscriptionName);
118+
LOGGER.debug("Subscription not found traceback:\n {}", e);
118119
notFoundSubscriptionList.add(subscriptionName);
119120
} catch (Exception e) {
120-
LOGGER.error("Failed to fetch subscription {}", subscriptionName, e);
121+
LOGGER.error("Failed to fetch subscription {}", subscriptionName);
122+
LOGGER.debug("Failed to fetch subscription:\n {}", e);
123+
121124
notFoundSubscriptionList.add(subscriptionName);
122125
}
123126
});

src/main/java/com/ericsson/ei/handlers/MongoDBHandler.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.mongodb.MongoClient;
3636
import com.mongodb.MongoCommandException;
3737
import com.mongodb.MongoCredential;
38+
import com.mongodb.MongoInterruptedException;
39+
import com.mongodb.MongoSocketReadException;
3840
import com.mongodb.MongoWriteException;
3941
import com.mongodb.ServerAddress;
4042
import com.mongodb.client.MongoCollection;
@@ -266,8 +268,34 @@ public void createTTLIndex(String dataBaseName, String collectionName, String fi
266268
private MongoCollection<Document> getMongoCollection(String dataBaseName, String collectionName) {
267269
if (mongoClient == null)
268270
return null;
269-
MongoDatabase db = mongoClient.getDatabase(dataBaseName);
270-
List<String> collectionList = db.listCollectionNames().into(new ArrayList<String>());
271+
MongoDatabase db;
272+
List<String> collectionList;
273+
try {
274+
db = mongoClient.getDatabase(dataBaseName);
275+
collectionList = db.listCollectionNames().into(new ArrayList<String>());
276+
}
277+
catch (MongoCommandException e) {
278+
LOGGER.error("MongoCommandException, Something went wrong with MongoDb connection. Error: " + e.getErrorMessage() + "\nStacktrace\n" + e);
279+
closeMongoDbConecction();
280+
return null;
281+
}
282+
catch (MongoInterruptedException e) {
283+
LOGGER.error(" MongoInterruptedException, MongoDB shutdown or interrupted. Error: " + e.getMessage() + "\nStacktrace\n" + e);
284+
closeMongoDbConecction();
285+
return null;
286+
}
287+
catch (MongoSocketReadException e) {
288+
LOGGER.error("MongoSocketReadException, MongoDB shutdown or interrupted. Error: " + e.getMessage() + "\nStacktrace\n" + e);
289+
closeMongoDbConecction();
290+
return null;
291+
}
292+
293+
catch (IllegalStateException e) {
294+
LOGGER.error("IllegalStateException, MongoDB state not good. Error: " + e.getMessage());
295+
closeMongoDbConecction();
296+
return null;
297+
}
298+
271299
if (!collectionList.contains(collectionName)) {
272300
LOGGER.debug("The requested database({}) / collection({}) not available in mongodb, Creating ........", dataBaseName, collectionName);
273301
try {
@@ -286,6 +314,13 @@ private MongoCollection<Document> getMongoCollection(String dataBaseName, String
286314
return collection;
287315
}
288316

317+
private void closeMongoDbConecction() {
318+
if (mongoClient != null) {
319+
mongoClient.close();
320+
}
321+
mongoClient = null;
322+
}
323+
289324
/**
290325
* This method is used to drop a collection.
291326
*

src/main/java/com/ericsson/ei/notifications/HttpRequestSender.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.http.*;
2626
import org.springframework.stereotype.Component;
2727
import org.springframework.web.client.HttpClientErrorException;
28+
import org.springframework.web.client.HttpServerErrorException;
2829
import org.springframework.web.client.RestOperations;
2930

3031
/**
@@ -62,6 +63,9 @@ public boolean postDataMultiValue(String url, HttpEntity<?> request)
6263
LOGGER.error("HTTP request failed, bad request! When trying to connect to URL: {}\n{}",
6364
url, e);
6465
return false;
66+
} catch (HttpServerErrorException e) {
67+
LOGGER.error("HttpServerErrorException, HTTP request to url {} failed\n", url, e);
68+
return false;
6569
} catch (Exception e) {
6670
LOGGER.error("HTTP request to url {} failed\n", url, e);
6771
return false;

src/test/java/com/ericsson/ei/subscription/SubscriptionRepeatDbHandlerTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@
2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
2828
import org.springframework.beans.factory.annotation.Autowired;
29-
import org.springframework.boot.test.context.SpringBootContextLoader;
30-
import org.springframework.boot.test.context.SpringBootTest;
31-
import org.springframework.test.context.ContextConfiguration;
3229
import org.springframework.test.context.TestPropertySource;
3330
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3431

35-
import com.ericsson.ei.App;
3632
import com.ericsson.ei.handlers.MongoDBHandler;
37-
import com.ericsson.ei.utils.TestContextInitializer;
33+
import com.ericsson.ei.utils.FunctionalTestBase;
3834
import com.mongodb.BasicDBObject;
3935
import com.mongodb.util.JSON;
4036

@@ -43,10 +39,8 @@
4339
"missedNotificationDataBaseName: SubscriptionRepeatDbHandlerTest-missedNotifications",
4440
"rabbitmq.exchange.name: SubscriptionRepeatDbHandlerTest-exchange",
4541
"rabbitmq.consumerName: SubscriptionRepeatDbHandlerTest" })
46-
@ContextConfiguration(classes = App.class, loader = SpringBootContextLoader.class, initializers = TestContextInitializer.class)
4742
@RunWith(SpringJUnit4ClassRunner.class)
48-
@SpringBootTest(classes = { App.class })
49-
public class SubscriptionRepeatDbHandlerTest {
43+
public class SubscriptionRepeatDbHandlerTest extends FunctionalTestBase {
5044

5145
static Logger log = LoggerFactory.getLogger(SubscriptionRepeatDbHandlerTest.class);
5246

src/test/resources/logback.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
<root level="info">
9+
<appender-ref ref="STDOUT" />
10+
</root>
11+
<logger name="org.mockserver.mock" level="error" />
12+
</configuration>

0 commit comments

Comments
 (0)