Commit 71953b1d authored by Selvakumar Kamatchinathan's avatar Selvakumar Kamatchinathan
Browse files

Auto Email Notification of Registry property updates

parent 171a3926
......@@ -14,6 +14,7 @@ pipeline {
stagingHost = credentials('STAGINGHOST')
fallbackHost = credentials('FALLBACKHOST')
productionHost = credentials('PRODUCTIONHOST')
smtpHost = credentials('SMTPHOST')
}
parameters {
booleanParam(name: 'DeployToStaging' , defaultValue: false , description: '')
......@@ -22,7 +23,7 @@ pipeline {
stages {
stage('Default Build pointing to Staging DB') {
steps {
sh "mvn clean package -DskipTests -DbuildDirectory=staging/target -Dampt2d-property-registry-db.url=${stagingPostgresDbUrl} -Dampt2d-property-registry-db.username=${postgresDBUserName} -Dampt2d-property-registry-db.password=${postgresDBPassword}"
sh "mvn clean package -DskipTests -DbuildDirectory=staging/target -Dampt2d-property-registry-db.url=${stagingPostgresDbUrl} -Dampt2d-property-registry-db.username=${postgresDBUserName} -Dampt2d-property-registry-db.password=${postgresDBPassword} -Dsmtp-host=${smtpHost}"
}
}
stage('Build For FallBack And Production') {
......@@ -33,9 +34,9 @@ pipeline {
}
steps {
echo 'Build pointing to FallBack DB'
sh "mvn clean package -DskipTests -DbuildDirectory=fallback/target -Dampt2d-property-registry-db.url=${fallBackPostgresDbUrl} -Dampt2d-property-registry-db.username=${postgresDBUserName} -Dampt2d-property-registry-db.password=${postgresDBPassword}"
sh "mvn clean package -DskipTests -DbuildDirectory=fallback/target -Dampt2d-property-registry-db.url=${fallBackPostgresDbUrl} -Dampt2d-property-registry-db.username=${postgresDBUserName} -Dampt2d-property-registry-db.password=${postgresDBPassword} -Dsmtp-host=${smtpHost}"
echo 'Build pointing to Production DB'
sh "mvn clean package -DskipTests -DbuildDirectory=production/target -Dampt2d-property-registry-db.url=${productionPostgresDbUrl} -Dampt2d-property-registry-db.username=${postgresDBUserName} -Dampt2d-property-registry-db.password=${postgresDBPassword}"
sh "mvn clean package -DskipTests -DbuildDirectory=production/target -Dampt2d-property-registry-db.url=${productionPostgresDbUrl} -Dampt2d-property-registry-db.username=${postgresDBUserName} -Dampt2d-property-registry-db.password=${postgresDBPassword} -Dsmtp-host=${smtpHost}"
}
}
stage('Deploy To Staging') {
......
......@@ -31,6 +31,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
......
/*
*
* 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.entities;
import uk.ac.ebi.ampt2d.registry.util.BeanUtil;
import uk.ac.ebi.ampt2d.registry.service.mail.MailService;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
public class EntityEventListener {
private enum Event {
CREATED,
UPDATED,
REMOVED
}
@PostPersist
public void onPersistEntity(EntityIdentifier entity) {
sendNotification(entity, Event.CREATED);
}
@PostUpdate
public void onUpdateEntity(EntityIdentifier entity) {
sendNotification(entity, Event.UPDATED);
}
@PostRemove
public void onRemoveEntity(EntityIdentifier entity) {
sendNotification(entity, Event.REMOVED);
}
private void sendNotification(EntityIdentifier entity, Event event) {
BeanUtil.getBean(MailService.class).send(entity.getClass().getSimpleName() + " " + entity.getId() + " " + event);
}
}
/*
*
* 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.entities;
public interface EntityIdentifier<ENTITY_ID> {
ENTITY_ID getId();
}
\ No newline at end of file
......@@ -34,8 +34,8 @@ import javax.validation.constraints.Size;
import java.time.ZonedDateTime;
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Phenotype {
@EntityListeners({AuditingEntityListener.class,EntityEventListener.class})
public class Phenotype implements EntityIdentifier<String>{
public enum Group {
......@@ -68,4 +68,8 @@ public class Phenotype {
@LastModifiedDate
private ZonedDateTime lastModifiedDate;
public String getId() {
return id;
}
}
......@@ -35,8 +35,8 @@ import javax.validation.constraints.Size;
import java.time.ZonedDateTime;
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Property {
@EntityListeners({AuditingEntityListener.class,EntityEventListener.class})
public class Property implements EntityIdentifier<String>{
public enum Type {
......@@ -102,4 +102,7 @@ public class Property {
@LastModifiedDate
private ZonedDateTime lastModifiedDate;
public String getId() {
return id;
}
}
\ 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.service.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class MailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("${mail.to}")
private String to;
@Value("${mail.from}")
private String from;
@Value("${mail.subject}")
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);
}
}
\ 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.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class BeanUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
}
\ No newline at end of file
......@@ -21,4 +21,15 @@ security.enabled=true
security.oauth2.resource.userInfoUri=https://www.googleapis.com/userinfo/v2/me
security.oauth2.resource.filter-order=3
registry.protocols=https
spring.mail.host=@smtp-host@
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
registry.protocols=https
\ No newline at end of file
......@@ -23,12 +23,16 @@ 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.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import uk.ac.ebi.ampt2d.registry.repositories.PhenotypeRepository;
import uk.ac.ebi.ampt2d.registry.repositories.PropertyRepository;
import uk.ac.ebi.ampt2d.registry.service.mail.MailService;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
......@@ -54,8 +58,12 @@ public class PropertyRegistryServiceApplicationTests {
@Autowired
private PropertyRepository propertyRepository;
@MockBean
private MailService mailService;
@Before
public void deleteAllBeforeTests() throws Exception {
public void setUp() throws Exception {
doNothing().when(mailService).send(anyString());
phenotypeRepository.deleteAll();
propertyRepository.deleteAll();
}
......
/*
*
* 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.Before;
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.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import uk.ac.ebi.ampt2d.registry.service.mail.MailService;
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.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;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class RegistryUpdateNotificationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private MailService mailService;
@Before
public void setUp() throws Exception {
doNothing().when(mailService).send(anyString());
}
@Test
public void testPhenotypeEvent() throws Exception {
String phenotypeContent = "{\"id\":\"BMI\"," + "\"phenotypeGroup\":\"ANTHROPOMETRIC\"}";
mockMvc.perform(post("/phenotypes").content(phenotypeContent))
.andExpect(status().isCreated());
verify(mailService, times(1)).send("Phenotype BMI CREATED");
mockMvc.perform(patch("/phenotypes/BMI").content("{\"phenotypeGroup\":\"RENAL\"}"))
.andExpect(status().is2xxSuccessful());
verify(mailService, times(1)).send("Phenotype BMI UPDATED");
mockMvc.perform(delete("/phenotypes/BMI"))
.andExpect(status().isNoContent());
verify(mailService, times(1)).send("Phenotype BMI REMOVED");
}
@Test
public void testPropertyEvent() throws Exception {
String propertiesContent = "{\"id\":\"AF\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"NONE\"," +
"\"description\":\"AF\"}";
mockMvc.perform(post("/properties").content(propertiesContent))
.andExpect(status().isCreated());
verify(mailService, times(1)).send("Property AF CREATED");
mockMvc.perform(patch("/properties/AF").content("{\"description\":\"Allele Freq\"}"))
.andExpect(status().is2xxSuccessful());
verify(mailService, times(1)).send("Property AF UPDATED");
mockMvc.perform(delete("/properties/AF"))
.andExpect(status().isNoContent());
verify(mailService, times(1)).send("Property AF REMOVED");
}
}
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