Skip to content

Commit

Permalink
control-service: fix concatAddresses NPE
Browse files Browse the repository at this point in the history
This change aims to fix the NPE when null is passed to EmailNotification.concatAddresses().

Testing done: unit tests

Signed-off-by: Miroslav Ivanov [email protected]
  • Loading branch information
mivanov1988 committed Apr 5, 2022
1 parent 72f7c90 commit 65787ae
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@

package com.vmware.taurus.service.notification;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
* The EmailNotification class is responsible for implementing the email notifications functionality.
* The single exposed send method accepts pre-build notification object.
Expand Down Expand Up @@ -97,10 +101,12 @@ private boolean recipientsExist(Address[] recipients) {
return true;
}

private String concatAddresses(Address[] addresses) {
return Arrays.asList(addresses)
.stream()
.map(addr -> addr.toString())
.collect(Collectors.joining(" "));
String concatAddresses(Address[] addresses) {
return addresses == null ?
null :
Arrays.asList(addresses)
.stream()
.map(addr -> addr.toString())
.collect(Collectors.joining(" "));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 VMware, Inc.
* SPDX-License-Identifier: Apache-2.0
*/

package com.vmware.taurus.service.notification;

import javax.mail.Address;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.vmware.taurus.ControlplaneApplication;

@SpringBootTest(classes = ControlplaneApplication.class)
public class EmailNotificationTest {

@Autowired
private EmailNotification emailNotification;

@Test
public void testConcatAddresses_nullAddresses_shouldReturnNull() {
String result = emailNotification.concatAddresses(null);
Assertions.assertNull(result);
}

@Test
public void testConcatAddresses_validAddresses_shouldReturnNull() {
String address1 = "[email protected]";
String address2 = "[email protected]";
String result = emailNotification.concatAddresses(
new Address[]{createAddress(address1), createAddress(address2)});

Assertions.assertEquals(address1 + " " + address2, result);
}

private static Address createAddress(String address) {
return new Address() {
@Override
public String getType() {
return null;
}

@Override
public String toString() {
return address;
}

@Override
public boolean equals(Object address) {
return false;
}
};
}
}

0 comments on commit 65787ae

Please sign in to comment.