Revert "Implementation for Simple Hexagonal Architecture"

This reverts commit ad893be8ac.
This commit is contained in:
Palaniappan Arunachalam 2022-02-09 13:21:48 +05:30
parent 3dd2802e2d
commit 5da21d6bba
21 changed files with 0 additions and 941 deletions

View File

@ -1,15 +0,0 @@
package com.baeldung.simplehexagonal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication(scanBasePackages = "com.baeldung.simplehexagonal")
@PropertySource(value = { "classpath:simple-hexagonal.properties" })
public class ConferenceApplication {
public static void main(String[] args) {
SpringApplication.run(ConferenceApplication.class, args);
}
}

View File

@ -1,54 +0,0 @@
package com.baeldung.simplehexagonal.application.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.simplehexagonal.domain.Session;
import com.baeldung.simplehexagonal.domain.services.SessionService;
@RestController
@RequestMapping("/api/v1/sessions")
public class SessionController {
private SessionService sessionService;
@Autowired
public SessionController(SessionService sessionService) {
this.sessionService = sessionService;
}
@GetMapping
public List<Session> findAll() {
return sessionService.findAll();
}
@GetMapping
@RequestMapping("{id}")
public Session get(@PathVariable Long id) {
return sessionService.get(id);
}
@PostMapping
public Session create(@RequestBody final Session session) {
return sessionService.create(session);
}
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable Long id) {
sessionService.delete(id);
}
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
public Session update(@PathVariable Long id, @RequestBody Session session) {
return sessionService.update(id, session);
}
}

View File

@ -1,57 +0,0 @@
package com.baeldung.simplehexagonal.application.controllers;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.simplehexagonal.domain.Speaker;
import com.baeldung.simplehexagonal.domain.services.SpeakerService;
@RestController
@RequestMapping("/api/v1/speakers")
public class SpeakerController {
private SpeakerService speakerService;
@Autowired
public SpeakerController(SpeakerService speakerService) {
this.speakerService = speakerService;
}
@GetMapping
public List<Speaker> findAll() {
return speakerService.findAll();
}
@GetMapping
@RequestMapping("{id}")
public Speaker get(@PathVariable Long id) {
return speakerService.get(id);
}
@PostMapping
public Speaker create(@RequestBody final Speaker speaker) {
return speakerService.save(speaker);
}
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable Long id) {
speakerService.delete(id);
}
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
public Speaker update(@PathVariable Long id, @RequestBody Speaker speaker) {
Speaker currentSpeaker = speakerService.get(id);
BeanUtils.copyProperties(speaker, currentSpeaker, "speaker_id");
return speakerService.save(currentSpeaker);
}
}

View File

@ -1,58 +0,0 @@
package com.baeldung.simplehexagonal.domain;
import java.util.List;
public class Session {
private Long sessionId;
private String sessionName;
private String sessionDescription;
private Integer sessionLength;
private List<Speaker> speakers;
public Session() {
}
public Long getSessionId() {
return sessionId;
}
public void setSessionId(Long sessionId) {
this.sessionId = sessionId;
}
public String getSessionName() {
return sessionName;
}
public void setSessionName(String sessionName) {
this.sessionName = sessionName;
}
public String getSessionDescription() {
return sessionDescription;
}
public void setSessionDescription(String sessionDescription) {
this.sessionDescription = sessionDescription;
}
public Integer getSessionLength() {
return sessionLength;
}
public void setSessionLength(Integer sessionLength) {
this.sessionLength = sessionLength;
}
public List<Speaker> getSpeakers() {
return speakers;
}
public void setSpeakers(List<Speaker> speakers) {
this.speakers = speakers;
}
}

View File

@ -1,90 +0,0 @@
package com.baeldung.simplehexagonal.domain;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class Speaker {
private Long speakerId;
private String firstName;
private String lastName;
private String title;
private String company;
private String speakerBio;
private byte[] speakerPhoto;
@JsonIgnore
private List<Session> sessions;
public Speaker() {
}
public Long getSpeakerId() {
return speakerId;
}
public void setSpeakerId(Long speakerId) {
this.speakerId = speakerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getSpeakerBio() {
return speakerBio;
}
public void setSpeakerBio(String speakerBio) {
this.speakerBio = speakerBio;
}
public byte[] getSpeakerPhoto() {
return speakerPhoto;
}
public void setSpeakerPhoto(byte[] speakerPhoto) {
this.speakerPhoto = speakerPhoto;
}
public List<Session> getSessions() {
return sessions;
}
public void setSessions(List<Session> sessions) {
this.sessions = sessions;
}
}

View File

@ -1,17 +0,0 @@
package com.baeldung.simplehexagonal.domain.repository;
import java.util.List;
import com.baeldung.simplehexagonal.domain.Session;
public interface SessionRepository {
List<Session> findAll();
Session findById(Long id);
Session save(Session session);
void deleteById(Long id);
}

View File

@ -1,17 +0,0 @@
package com.baeldung.simplehexagonal.domain.repository;
import java.util.List;
import com.baeldung.simplehexagonal.domain.Speaker;
public interface SpeakerRepository {
List<Speaker> findAll();
Speaker findById(Long id);
Speaker save(Speaker Speaker);
void deleteById(Long id);
}

View File

@ -1,19 +0,0 @@
package com.baeldung.simplehexagonal.domain.services;
import java.util.List;
import com.baeldung.simplehexagonal.domain.Session;
public interface SessionService {
List<Session> findAll();
Session get(Long id);
Session create(Session session);
void delete(Long id);
Session update(Long id, Session session);
}

View File

@ -1,40 +0,0 @@
package com.baeldung.simplehexagonal.domain.services;
import java.util.List;
import org.springframework.beans.BeanUtils;
import com.baeldung.simplehexagonal.domain.Session;
import com.baeldung.simplehexagonal.domain.repository.SessionRepository;
public class SessionServiceImpl implements SessionService {
private SessionRepository sessionRepository;
public SessionServiceImpl(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}
public List<Session> findAll() {
return sessionRepository.findAll();
}
public Session get(Long id) {
return sessionRepository.findById(id);
}
public Session create(Session session) {
return sessionRepository.save(session);
}
public void delete(Long id) {
sessionRepository.deleteById(id);
}
public Session update(Long id, Session session) {
Session currentSession = sessionRepository.findById(id);
BeanUtils.copyProperties(session, currentSession, "session_id");
return sessionRepository.save(currentSession);
}
}

View File

@ -1,19 +0,0 @@
package com.baeldung.simplehexagonal.domain.services;
import java.util.List;
import com.baeldung.simplehexagonal.domain.Speaker;
public interface SpeakerService {
List<Speaker> findAll();
Speaker get(Long id);
Speaker save(Speaker speaker);
void delete(Long id);
Speaker update(Long id, Speaker speaker);
}

View File

@ -1,39 +0,0 @@
package com.baeldung.simplehexagonal.domain.services;
import java.util.List;
import org.springframework.beans.BeanUtils;
import com.baeldung.simplehexagonal.domain.Speaker;
import com.baeldung.simplehexagonal.domain.repository.SpeakerRepository;
public class SpeakerServiceImpl implements SpeakerService {
private SpeakerRepository speakerRepository;
public SpeakerServiceImpl(SpeakerRepository speakerRepository) {
this.speakerRepository = speakerRepository;
}
public List<Speaker> findAll() {
return speakerRepository.findAll();
}
public Speaker get(Long id) {
return speakerRepository.findById(id);
}
public Speaker save(Speaker speaker) {
return speakerRepository.save(speaker);
}
public void delete(Long id) {
speakerRepository.deleteById(id);
}
public Speaker update(Long id, Speaker speaker) {
Speaker currentSpeaker = speakerRepository.findById(id);
BeanUtils.copyProperties(speaker, currentSpeaker, "speaker_id");
return speakerRepository.save(currentSpeaker);
}
}

View File

@ -1,30 +0,0 @@
package com.baeldung.simplehexagonal.infrastructure.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.baeldung.simplehexagonal.ConferenceApplication;
import com.baeldung.simplehexagonal.domain.repository.SessionRepository;
import com.baeldung.simplehexagonal.domain.repository.SpeakerRepository;
import com.baeldung.simplehexagonal.domain.services.SessionService;
import com.baeldung.simplehexagonal.domain.services.SessionServiceImpl;
import com.baeldung.simplehexagonal.domain.services.SpeakerService;
import com.baeldung.simplehexagonal.domain.services.SpeakerServiceImpl;
@Configuration
@ComponentScan(basePackageClasses = ConferenceApplication.class)
@EnableJpaRepositories(basePackages = "com.baeldung.simplehexagonal")
public class BeanConfiguration {
@Bean
SessionService sessionService(SessionRepository sessionRepository) {
return new SessionServiceImpl(sessionRepository);
}
@Bean
SpeakerService speakerService(SpeakerRepository speakerRepository) {
return new SpeakerServiceImpl(speakerRepository);
}
}

View File

@ -1,99 +0,0 @@
package com.baeldung.simplehexagonal.infrastructure.repositories;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import com.baeldung.simplehexagonal.domain.Session;
import com.baeldung.simplehexagonal.domain.Speaker;
@Entity(name = "sessions")
public class SessionEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long session_id;
private String session_name;
private String session_description;
private Integer session_length;
@ManyToMany
@JoinTable(name = "session_speakers", joinColumns = @JoinColumn(name = "session_id"), inverseJoinColumns = @JoinColumn(name = "speaker_id"))
private List<SpeakerEntity> speakerEntities;
public SessionEntity() {
}
public SessionEntity(Session session) {
this.setSession_id(session.getSessionId());
this.setSession_name(session.getSessionName());
this.setSession_description(session.getSessionDescription());
this.setSession_length(session.getSessionLength());
List<SpeakerEntity> speakerEntities = session.getSpeakers()
.stream()
.map(it -> new SpeakerEntity(it))
.toList();
this.speakerEntities = speakerEntities;
}
public Session toSession() {
Session session = new Session();
session.setSessionId(session_id);
session.setSessionName(session_name);
session.setSessionDescription(session_description);
session.setSessionLength(session_length);
List<Speaker> speakers = speakerEntities.stream()
.map(it -> it.toSpeaker())
.toList();
session.setSpeakers(speakers);
return session;
}
public Long getSession_id() {
return session_id;
}
public void setSession_id(Long session_id) {
this.session_id = session_id;
}
public String getSession_name() {
return session_name;
}
public void setSession_name(String session_name) {
this.session_name = session_name;
}
public String getSession_description() {
return session_description;
}
public void setSession_description(String session_description) {
this.session_description = session_description;
}
public Integer getSession_length() {
return session_length;
}
public void setSession_length(Integer session_length) {
this.session_length = session_length;
}
public List<SpeakerEntity> getSpeakerEntities() {
return speakerEntities;
}
public void setSpeakerEntities(List<SpeakerEntity> speakerEntities) {
this.speakerEntities = speakerEntities;
}
}

View File

@ -1,9 +0,0 @@
package com.baeldung.simplehexagonal.infrastructure.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SessionJpaRepository extends JpaRepository<SessionEntity, Long> {
}

View File

@ -1,42 +0,0 @@
package com.baeldung.simplehexagonal.infrastructure.repositories;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.simplehexagonal.domain.Session;
import com.baeldung.simplehexagonal.domain.repository.SessionRepository;
@Component
public class SessionRepositoryImpl implements SessionRepository {
@Autowired
private SessionJpaRepository sessionJpaRepository;
@Override
public List<Session> findAll() {
return sessionJpaRepository.findAll()
.stream()
.map(SessionEntity::toSession)
.collect(Collectors.toList());
}
@Override
public Session findById(Long id) {
SessionEntity sessionEntity = sessionJpaRepository.getById(id);
return sessionEntity.toSession();
}
@Override
public Session save(Session session) {
return sessionJpaRepository.saveAndFlush(new SessionEntity(session))
.toSession();
}
public void deleteById(Long id) {
sessionJpaRepository.deleteById(id);
}
}

View File

@ -1,129 +0,0 @@
package com.baeldung.simplehexagonal.infrastructure.repositories;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.Type;
import com.baeldung.simplehexagonal.domain.Speaker;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity(name = "speakers")
public class SpeakerEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long speaker_id;
private String first_name;
private String last_name;
private String title;
private String company;
private String speaker_bio;
@Lob
@Type(type = "org.hibernate.type.BinaryType")
private byte[] speaker_photo;
@JsonIgnore
@ManyToMany(mappedBy = "speakerEntities", fetch = FetchType.LAZY)
private List<SessionEntity> sessionEntities;
public SpeakerEntity() {
}
public SpeakerEntity(Speaker speaker) {
this.setSpeaker_id(speaker.getSpeakerId());
this.setFirst_name(speaker.getFirstName());
this.setLast_name(speaker.getLastName());
this.setTitle(speaker.getTitle());
this.setCompany(speaker.getCompany());
this.setSpeaker_bio(speaker.getSpeakerBio());
this.setSpeaker_photo(speaker.getSpeakerPhoto());
}
public Speaker toSpeaker() {
Speaker speaker = new Speaker();
speaker.setSpeakerId(speaker_id);
speaker.setFirstName(first_name);
speaker.setLastName(last_name);
speaker.setTitle(title);
speaker.setCompany(company);
speaker.setSpeakerBio(speaker_bio);
speaker.setSpeakerPhoto(speaker_photo);
return speaker;
}
public Long getSpeaker_id() {
return speaker_id;
}
public void setSpeaker_id(Long speaker_id) {
this.speaker_id = speaker_id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getSpeaker_bio() {
return speaker_bio;
}
public void setSpeaker_bio(String speaker_bio) {
this.speaker_bio = speaker_bio;
}
public byte[] getSpeaker_photo() {
return speaker_photo;
}
public void setSpeaker_photo(byte[] speaker_photo) {
this.speaker_photo = speaker_photo;
}
public List<SessionEntity> getSessionEntities() {
return sessionEntities;
}
public void setSessionEntities(List<SessionEntity> sessionEntities) {
this.sessionEntities = sessionEntities;
}
}

View File

@ -1,9 +0,0 @@
package com.baeldung.simplehexagonal.infrastructure.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SpeakerJpaRepository extends JpaRepository<SpeakerEntity, Long> {
}

View File

@ -1,42 +0,0 @@
package com.baeldung.simplehexagonal.infrastructure.repositories;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.simplehexagonal.domain.Speaker;
import com.baeldung.simplehexagonal.domain.repository.SpeakerRepository;
@Component
public class SpeakerRepositoryImpl implements SpeakerRepository {
@Autowired
private SpeakerJpaRepository speakerJpaRepository;
@Override
public List<Speaker> findAll() {
return speakerJpaRepository.findAll()
.stream()
.map(SpeakerEntity::toSpeaker)
.collect(Collectors.toList());
}
@Override
public Speaker findById(Long id) {
SpeakerEntity speakerEntity = speakerJpaRepository.getById(id);
return speakerEntity.toSpeaker();
}
@Override
public Speaker save(Speaker speaker) {
return speakerJpaRepository.saveAndFlush(new SpeakerEntity(speaker))
.toSpeaker();
}
public void deleteById(Long id) {
speakerJpaRepository.deleteById(id);
}
}

View File

@ -1,8 +0,0 @@
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

View File

@ -1,74 +0,0 @@
package com.baeldung.simplehexagonal.domain.services;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.util.Assert.notNull;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.baeldung.simplehexagonal.domain.Session;
import com.baeldung.simplehexagonal.domain.repository.SessionRepository;
import com.baeldung.simplehexagonal.domain.services.SessionServiceImpl;
public class SessionServiceUnitTest {
private SessionServiceImpl sessionService;
private SessionRepository sessionRepository;
Session session;
@BeforeEach
void setUp() {
sessionRepository = mock(SessionRepository.class);
sessionService = new SessionServiceImpl(sessionRepository);
session = new Session();
session.setSessionId(1L);
session.setSessionName("Introduction to Hexagonal Architecture");
session.setSessionDescription("A quick and practical eample of Hexagonal Architecture");
session.setSessionLength(30);
when(sessionRepository.save(Mockito.any())).thenReturn(session);
when(sessionRepository.findById(1L)).thenReturn(session);
}
@Test
void testFindAll() {
List<Session> list = sessionService.findAll();
notNull(list, "should not return null");
}
@Test
void testGet() {
Session mySession = sessionService.get(1L);
notNull(mySession, "should not return null");
}
@Test
void testCreate() {
session = sessionService.create(new Session());
notNull(session.getSessionId(), "Id should be populated");
}
@Test
void testDelete() {
try {
sessionService.delete(1L);
} catch (Exception e) {
fail("Should not throw error");
}
}
@Test
void testUpdate() {
Session updatedSession = sessionService.update(1L, new Session());
notNull(updatedSession, "Id should be populated");
}
}

View File

@ -1,74 +0,0 @@
package com.baeldung.simplehexagonal.domain.services;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.util.Assert.notNull;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.baeldung.simplehexagonal.domain.Speaker;
import com.baeldung.simplehexagonal.domain.repository.SpeakerRepository;
import com.baeldung.simplehexagonal.domain.services.SpeakerServiceImpl;
public class SpeakerServiceUnitTest {
private SpeakerServiceImpl speakerService;
private SpeakerRepository speakerRepository;
Speaker speaker;
@BeforeEach
void setUp() {
speakerRepository = mock(SpeakerRepository.class);
speakerService = new SpeakerServiceImpl(speakerRepository);
speaker = new Speaker();
speaker.setSpeakerId(1L);
speaker.setTitle("Mr");
speaker.setFirstName("Palani");
speaker.setLastName("Arun");
when(speakerRepository.save(Mockito.any())).thenReturn(speaker);
when(speakerRepository.findById(1L)).thenReturn(speaker);
}
@Test
void testFindAll() {
List<Speaker> list = speakerService.findAll();
notNull(list, "should not return null");
}
@Test
void testGet() {
Speaker mySpeaker = speakerService.get(1L);
notNull(mySpeaker, "should not return null");
}
@Test
void testCreate() {
speaker = speakerService.save(new Speaker());
notNull(speaker.getSpeakerId(), "Id should be populated");
}
@Test
void testDelete() {
try {
speakerService.delete(1L);
} catch (Exception e) {
fail("Should not throw error");
}
}
@Test
void testUpdate() {
Speaker updatedSpeaker = speakerService.update(1L, new Speaker());
notNull(updatedSpeaker, "Id should be populated");
}
}