Commit c82623ad authored by Selvakumar Kamatchinathan's avatar Selvakumar Kamatchinathan
Browse files

changes suggested by zl

parent 71953b1d
/*
*
* Copyright 2018 EMBL - European Bioinformatics Institute
*
* 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.
*
*/
package uk.ac.ebi.ampt2d.registry.exceptionhandling;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.MailSendException;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
@ControllerAdvice
public class ExceptionHandlers {
private static final Logger exceptionLogger = Logger.getLogger(ExceptionHandlers.class.getSimpleName());
@ExceptionHandler(value = TransactionSystemException.class)
public ResponseEntity<String> handleMailSendException(TransactionSystemException ex) {
if (ex.getOriginalException().getCause().getClass().equals(MailSendException.class)) {
exceptionLogger.log(Level.SEVERE, ex.getOriginalException().getCause().getMessage());
return new ResponseEntity("Mail send exception, please contact amp-dev@ebi.ac.uk", HttpStatus
.INTERNAL_SERVER_ERROR);
}
throw ex;
}
}
......@@ -29,6 +29,9 @@ public class MailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("${mail.notify:false}")
private Boolean nofify;
@Value("${mail.to}")
private String to;
......@@ -39,11 +42,13 @@ public class MailService {
private String subject;
public void send(String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setFrom(from);
message.setSubject(subject);
message.setText(text);
javaMailSender.send(message);
if (nofify == true) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setFrom(from);
message.setSubject(subject);
message.setText(text);
javaMailSender.send(message);
}
}
}
\ No newline at end of file
/*
*
* Copyright 2018 EMBL - European Bioinformatics Institute
*
* 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.
*
*/
package uk.ac.ebi.ampt2d.registry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"mail.notify=true", "spring.mail.host=invalid_host"})
@AutoConfigureMockMvc
public class RegistryUpdateNotificationFailure {
@Autowired
private MockMvc mockMvc;
@Test
public void testPhenotypeEvent() throws Exception {
String phenotypeContent = "{\"id\":\"BMI\"," + "\"phenotypeGroup\":\"ANTHROPOMETRIC\"}";
mockMvc.perform(post("/phenotypes").content(phenotypeContent))
.andExpect(status().isInternalServerError())
.andExpect(content().string("Mail send exception, please contact amp-dev@ebi.ac.uk"));
}
}
......@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import uk.ac.ebi.ampt2d.registry.service.mail.MailService;
......@@ -32,6 +33,7 @@ import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_CLASS;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
......@@ -40,6 +42,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext(classMode = BEFORE_CLASS)
public class RegistryUpdateNotificationTest {
@Autowired
......
......@@ -11,4 +11,13 @@ management.endpoint.health.show-details=always
endpoints.health.sensitive=false
registry.protocols=http
spring.jpa.hibernate.ddl-auto=none
\ No newline at end of file
spring.jpa.hibernate.ddl-auto=none
spring.mail.port=587
spring.mail.test-connection=false
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.ssl.enable=false
mail.from=amp-dev@ebi.ac.uk
mail.to=amp-dev@ebi.ac.uk
mail.subject=AMP-T2D Registry Update
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment