From fc31e54a91740a681f7b0372c06ca0854e06e790 Mon Sep 17 00:00:00 2001 From: Stefan Petrescu Date: Tue, 1 Mar 2022 09:02:18 +0200 Subject: [PATCH 1/4] added classes for: - lab 2 - spring app context, beans, bean naming, stereotype, lifecycle and wiring - lab 5 - eureka server --- eureka/.gitignore | 33 ++++ eureka/discovery-server/.gitignore | 33 ++++ eureka/discovery-server/pom.xml | 57 +++++++ .../example/discovery/DiscoveryServer.java | 54 ++++++ .../src/main/resources/discovery-server.yml | 23 +++ .../DiscoveryServerApplicationTests.java | 13 ++ eureka/persons-server/.gitignore | 33 ++++ eureka/persons-server/pom.xml | 92 ++++++++++ .../java/com/eureka/persons/PersonRepo.java | 64 +++++++ .../com/eureka/persons/PersonsController.java | 161 ++++++++++++++++++ .../com/eureka/persons/PersonsException.java | 45 +++++ .../com/eureka/persons/PersonsServer.java | 58 +++++++ .../eureka/persons/base/AbstractEntity.java | 131 ++++++++++++++ .../eureka/persons/ex/NotFoundException.java | 34 ++++ .../com/eureka/persons/person/Person.java | 157 +++++++++++++++++ .../eureka/persons/services/Initializer.java | 92 ++++++++++ .../persons/services/PersonService.java | 43 +++++ .../persons/services/PersonServiceImpl.java | 70 ++++++++ .../eureka/persons/util/DateProcessor.java | 44 +++++ .../eureka/persons/util/NumberGenerator.java | 53 ++++++ .../src/main/resources/banner.txt | 7 + .../src/main/resources/persons-server.yml | 51 ++++++ .../PersonsServerApplicationTests.java | 16 ++ eureka/pom.xml | 61 +++++++ .../com/example/eureka/EurekaApplication.java | 13 ++ .../src/main/resources/application.properties | 1 + .../eureka/EurekaApplicationTests.java | 13 ++ spring1/.gitignore | 33 ++++ spring1/pom.xml | 53 ++++++ .../java/com/unitbv/Spring1Application.java | 13 ++ .../com/unitbv/autowiring/AutowiredCfg.java | 9 + .../FormatServiceConstructorInjection.java | 14 ++ .../service/FormatServiceFieldInjection.java | 14 ++ .../service/FormatServiceSetterInjection.java | 14 ++ .../unitbv/autowiring/util/FormatUtil.java | 13 ++ .../src/main/java/com/unitbv/beans/Book.java | 38 +++++ .../src/main/java/com/unitbv/beans/Human.java | 6 + .../java/com/unitbv/beans/HumanAppCfg.java | 36 ++++ .../src/main/java/com/unitbv/beans/Item.java | 5 + .../main/java/com/unitbv/beans/Person.java | 38 +++++ .../com/unitbv/beansnaming/DependantBean.java | 31 ++++ .../unitbv/beansnaming/DependantBeanImpl.java | 37 ++++ .../com/unitbv/beansnaming/SimpleBean.java | 4 + .../unitbv/beansnaming/SimpleBeanImpl.java | 46 +++++ .../beansnaming/SimpleDependantCfg.java | 48 ++++++ .../java/com/unitbv/lifecycle/DepBean.java | 34 ++++ .../java/com/unitbv/lifecycle/FunBean.java | 43 +++++ .../java/com/unitbv/lifecycle/FunBeanCfg.java | 41 +++++ .../com/unitbv/stereotype/StereotypeCfg.java | 9 + .../stereotype/controller/UserController.java | 4 + .../com/unitbv/stereotype/model/User.java | 6 + .../stereotype/repository/UserRepository.java | 7 + .../repository/impl/UserRepositoryImpl.java | 160 +++++++++++++++++ .../stereotype/service/UserService.java | 4 + .../service/impl/UserServiceImpl.java | 6 + .../com/unitbv/stereotype/util/UserUtil.java | 4 + .../src/main/resources/application.properties | 1 + .../com/unitbv/Spring1ApplicationTests.java | 13 ++ .../unitbv/autowiring/AutowiredCfgTest.java | 39 +++++ .../com/unitbv/beans/HumanAppCfgTest.java | 53 ++++++ .../beansnaming/SimpleDependantCfgTest.java | 65 +++++++ .../com/unitbv/lifecycle/FunBeanCfgTest.java | 23 +++ .../unitbv/stereotype/StereotypeCfgTest.java | 35 ++++ 63 files changed, 2451 insertions(+) create mode 100644 eureka/.gitignore create mode 100644 eureka/discovery-server/.gitignore create mode 100644 eureka/discovery-server/pom.xml create mode 100644 eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java create mode 100644 eureka/discovery-server/src/main/resources/discovery-server.yml create mode 100644 eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java create mode 100644 eureka/persons-server/.gitignore create mode 100644 eureka/persons-server/pom.xml create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java create mode 100644 eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java create mode 100644 eureka/persons-server/src/main/resources/banner.txt create mode 100644 eureka/persons-server/src/main/resources/persons-server.yml create mode 100644 eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java create mode 100644 eureka/pom.xml create mode 100644 eureka/src/main/java/com/example/eureka/EurekaApplication.java create mode 100644 eureka/src/main/resources/application.properties create mode 100644 eureka/src/test/java/com/example/eureka/EurekaApplicationTests.java create mode 100644 spring1/.gitignore create mode 100644 spring1/pom.xml create mode 100644 spring1/src/main/java/com/unitbv/Spring1Application.java create mode 100644 spring1/src/main/java/com/unitbv/autowiring/AutowiredCfg.java create mode 100644 spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceConstructorInjection.java create mode 100644 spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceFieldInjection.java create mode 100644 spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceSetterInjection.java create mode 100644 spring1/src/main/java/com/unitbv/autowiring/util/FormatUtil.java create mode 100644 spring1/src/main/java/com/unitbv/beans/Book.java create mode 100644 spring1/src/main/java/com/unitbv/beans/Human.java create mode 100644 spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java create mode 100644 spring1/src/main/java/com/unitbv/beans/Item.java create mode 100644 spring1/src/main/java/com/unitbv/beans/Person.java create mode 100644 spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java create mode 100644 spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java create mode 100644 spring1/src/main/java/com/unitbv/beansnaming/SimpleBean.java create mode 100644 spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java create mode 100644 spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java create mode 100644 spring1/src/main/java/com/unitbv/lifecycle/DepBean.java create mode 100644 spring1/src/main/java/com/unitbv/lifecycle/FunBean.java create mode 100644 spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/StereotypeCfg.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/controller/UserController.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/model/User.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/repository/UserRepository.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/repository/impl/UserRepositoryImpl.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/service/UserService.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/service/impl/UserServiceImpl.java create mode 100644 spring1/src/main/java/com/unitbv/stereotype/util/UserUtil.java create mode 100644 spring1/src/main/resources/application.properties create mode 100644 spring1/src/test/java/com/unitbv/Spring1ApplicationTests.java create mode 100644 spring1/src/test/java/com/unitbv/autowiring/AutowiredCfgTest.java create mode 100644 spring1/src/test/java/com/unitbv/beans/HumanAppCfgTest.java create mode 100644 spring1/src/test/java/com/unitbv/beansnaming/SimpleDependantCfgTest.java create mode 100644 spring1/src/test/java/com/unitbv/lifecycle/FunBeanCfgTest.java create mode 100644 spring1/src/test/java/com/unitbv/stereotype/StereotypeCfgTest.java diff --git a/eureka/.gitignore b/eureka/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/eureka/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/eureka/discovery-server/.gitignore b/eureka/discovery-server/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/eureka/discovery-server/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/eureka/discovery-server/pom.xml b/eureka/discovery-server/pom.xml new file mode 100644 index 0000000..d9e9e18 --- /dev/null +++ b/eureka/discovery-server/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.4 + + + com.example + discovery-server + 0.0.1-SNAPSHOT + discovery-server + discovery-server + + 11 + 2021.0.1 + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java new file mode 100644 index 0000000..2f61fa9 --- /dev/null +++ b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java @@ -0,0 +1,54 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.example.discovery; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +import java.io.IOException; + +@SpringBootApplication +@EnableEurekaServer +public class DiscoveryServer { + + private static Logger logger = LoggerFactory.getLogger(DiscoveryServer.class); + + public static void main(String... args) throws IOException { + // Look for configuration in discovery-server.properties or discovery-server.yml + System.setProperty("spring.config.name", "discovery-server"); + + var ctx = SpringApplication.run(DiscoveryServer.class, args); + assert (ctx != null); + logger.info("Started ..."); + System.in.read(); + ctx.close(); + } +} diff --git a/eureka/discovery-server/src/main/resources/discovery-server.yml b/eureka/discovery-server/src/main/resources/discovery-server.yml new file mode 100644 index 0000000..d8540ee --- /dev/null +++ b/eureka/discovery-server/src/main/resources/discovery-server.yml @@ -0,0 +1,23 @@ +spring: + application: + name: discovery-service +# Configure this Discovery Server +eureka: + instance: + hostname: localhost + client: + registerWithEureka: false # do not auto-register as client + fetchRegistry: false + +server: + port: 3000 # where this discovery server is accessible + waitTimeInMsWhenSyncEmpty: 0 + address: 0.0.0.0 + +logging: + pattern: + console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" + level: + root: INFO + org.springframework: DEBUG + com.apress.cems: DEBUG diff --git a/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java b/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java new file mode 100644 index 0000000..f102053 --- /dev/null +++ b/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.discoveryserver; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DiscoveryServerApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/eureka/persons-server/.gitignore b/eureka/persons-server/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/eureka/persons-server/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/eureka/persons-server/pom.xml b/eureka/persons-server/pom.xml new file mode 100644 index 0000000..245d2cd --- /dev/null +++ b/eureka/persons-server/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.4 + + + com.example + persons-server + 0.0.1-SNAPSHOT + persons-server + persons-server + + 11 + 2021.0.1 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + org.springframework.boot + spring-boot-starter-test + test + + + com.h2database + h2 + 2.1.210 + runtime + + + com.sun.jersey.contribs + jersey-apache-client4 + 1.19.4 + + + com.fasterxml.jackson.core + jackson-databind + 2.13.1 + + + com.fasterxml.jackson.core + jackson-core + 2.13.1 + + + javax.validation + validation-api + 2.0.1.Final + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java new file mode 100644 index 0000000..767436d --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java @@ -0,0 +1,64 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons; + +import com.eureka.persons.person.Person; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +@Repository +public interface PersonRepo extends JpaRepository { + + @Query("select p from Person p where p.username like %?1%") + Optional findByUsername(String username); + + @Query("select p from Person p where p.username like %?1%") + List findByUsernameLike(String username); + + + @Query("select p from Person p where p.firstName=:fn") + List findByFirstName(@Param("fn") String firstName); + + @Query("select p from Person p where p.firstName like %?1%") + List findByFirstNameLike(String firstName); + + @Query("select p from Person p where p.lastName=:ln") + List findByLastName(@Param("ln") String lastName); + + @Query("select p from Person p where p.lastName like %?1%") + List findByLastNameLike(String lastName); + + @Query("select p from Person p where p.hiringDate=:hd") + List findByHiringDate(@Param("hd") LocalDateTime date); +} diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java new file mode 100644 index 0000000..7545a0b --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java @@ -0,0 +1,161 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons; + +import com.eureka.persons.ex.NotFoundException; +import com.eureka.persons.person.Person; +import com.eureka.persons.services.PersonService; +import com.eureka.persons.util.NumberGenerator; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.util.StringUtils; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.util.UriTemplate; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.Optional; + +import static com.eureka.persons.base.AbstractEntity.COMPARATOR_BY_ID; + +@RestController +@RequestMapping("/persons") +public class PersonsController { + private PersonService personService; + + public PersonsController(PersonService personService) { + this.personService = personService; + } + + /** + * Handles requests to list all persons. + */ + @ResponseStatus(HttpStatus.OK) + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + public List list() { + List persons = personService.findAll(); + persons.sort(COMPARATOR_BY_ID); + return persons; + } + + /** + * Handles requests to create a person. + */ + @ResponseStatus(HttpStatus.CREATED) + @PostMapping + public void create(@RequestBody Person person, BindingResult result, @Value("#{request.requestURL}") + StringBuffer originalUrl, HttpServletResponse response) { + if (result.hasErrors()) { + var errString = createErrorString(result); + throw new PersonsException(HttpStatus.BAD_REQUEST, "Cannot save entry because: " + errString); + } + if (StringUtils.isEmpty(person.getPassword())) { + person.setPassword(NumberGenerator.getPassword()); + } + try { + var newPerson = personService.save(person); + response.setHeader("Location", getLocationForUser(originalUrl, newPerson.getId())); + } catch (Exception e) { + throw new PersonsException(HttpStatus.UNPROCESSABLE_ENTITY, e); + } + } + + /** + * Determines URL of user resource based on the full URL of the given request, + * appending the path info with the given childIdentifier using a UriTemplate. + */ + static String getLocationForUser(StringBuffer url, Object childIdentifier) { + var template = new UriTemplate(url.toString() + "/{id}"); + return template.expand(childIdentifier).toASCIIString(); + } + + private String createErrorString(BindingResult result) { + var sb = new StringBuilder(); + result.getAllErrors().forEach(error -> { + if (error instanceof FieldError) { + FieldError err = (FieldError) error; + sb.append("Field '").append(err.getField()).append("' value error: ").append(err.getDefaultMessage()).append("\n"); + } + }); + return sb.toString(); + } + + /** + * Returns the {@code Person} instance with id {@code id} + * + * @param id + * @return + */ + @ResponseStatus(HttpStatus.OK) + @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public Person show(@PathVariable Long id) { + var personOpt = personService.findById(id); + if (personOpt.isPresent()) { + return personOpt.get(); + } else { + throw new NotFoundException(Person.class, id); + } + } + + /** + * Updates the {@code Person} instance with id {@code id} + * + * @param updatedPerson + * @param id + * @return + */ + @ResponseStatus(HttpStatus.NO_CONTENT) + @PutMapping("/{id}") + public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { + Optional personOpt = personService.findById(id); + if (personOpt.isPresent()) { + var person = personOpt.get(); + person.setUsername(updatedPerson.getUsername()); + person.setFirstName(updatedPerson.getFirstName()); + person.setLastName(updatedPerson.getLastName()); + personService.save(person); + } else { + throw new NotFoundException(Person.class, id); + } + } + + /** + * Delete the {@code Person} instance with id {@code id} + * + * @param id + */ + @ResponseStatus(HttpStatus.NO_CONTENT) + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) { + var personOpt = personService.findById(id); + personOpt.ifPresent(value -> personService.delete(value)); + } +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java new file mode 100644 index 0000000..c5c6ccf --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java @@ -0,0 +1,45 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons; + +import org.springframework.http.HttpStatus; + + +public class PersonsException extends RuntimeException{ + private HttpStatus status; + + public PersonsException(HttpStatus status, String message) { + super(message); + this.status = status; + } + + public PersonsException(HttpStatus status, Throwable cause) { + super(cause); + this.status = status; + } +} diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java new file mode 100644 index 0000000..bde4308 --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java @@ -0,0 +1,58 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +import java.io.IOException; + + +@EntityScan(basePackages = "com.eureka.persons") +@SpringBootApplication +@EnableEurekaClient +public class PersonsServer { + + private static Logger logger = LoggerFactory.getLogger(PersonsServer.class); + + public static void main(String... args) throws IOException { + // Look for configuration in persons-server.properties or persons-server.yml + System.setProperty("spring.config.name", "persons-server"); + + var ctx = SpringApplication.run(PersonsServer.class, args); + assert (ctx != null); + logger.info("Started ..."); + System.in.read(); + ctx.close(); + } +} diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java new file mode 100644 index 0000000..e017cfc --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java @@ -0,0 +1,131 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.base; + +import com.eureka.persons.util.DateProcessor; +import com.fasterxml.jackson.annotation.JsonFormat; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.persistence.*; +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.Comparator; +import java.util.Objects; + +@MappedSuperclass +public abstract class AbstractEntity implements Serializable { + + public static Comparator COMPARATOR_BY_ID = Comparator.comparing(AbstractEntity::getId); + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(updatable = false) + protected Long id; + + @Version + protected int version; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DateProcessor.DATE_FORMAT) + @Column(name = "created_at", nullable = false) + @DateTimeFormat(pattern = DateProcessor.DATE_FORMAT) + protected LocalDateTime createdAt; + + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DateProcessor.DATE_FORMAT) + @Column(name = "modified_at", nullable = false) + @DateTimeFormat(pattern = DateProcessor.DATE_FORMAT) + protected LocalDateTime modifiedAt; + + /** + * This constructor is required by JPA. All subclasses of this class will inherit this constructor. + */ + protected AbstractEntity() { + createdAt = LocalDateTime.now(); + modifiedAt = LocalDateTime.now(); + } + + /** + * Returns the entity identifier. This identifier is unique per entity. It is used by persistence frameworks used in a project, + * and although is public, it should not be used by application code. + * This identifier is mapped by ORM (Object Relational Mapper) to the database primary key of the Person record to which + * the entity instance is mapped. + * + * @return the unique entity identifier + */ + public Long getId() { + return id; + } + + /** + * Sets the entity identifier. This identifier is unique per entity. Is is used by persistence frameworks + * and although is public, it should never be set by application code. + * + * @param id the unique entity identifier + */ + public void setId(Long id) { + this.id = id; + } + + public LocalDateTime getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(LocalDateTime createdAt) { + this.createdAt = createdAt; + } + + public LocalDateTime getModifiedAt() { + return modifiedAt; + } + + public void setModifiedAt(LocalDateTime modifiedAt) { + this.modifiedAt = modifiedAt; + } + + // IDE generated methods + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + var that = (AbstractEntity) o; + if (!Objects.equals(id, that.id)) return false; + return true; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return String.format("AbstractEntity[id='%d%n', createdAt='%s', modifiedAt='%s', version='%d%n']", + id, DateProcessor.toString(createdAt), DateProcessor.toString(modifiedAt), version); + } +} diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java b/eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java new file mode 100644 index 0000000..f131fd7 --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java @@ -0,0 +1,34 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.ex; + +public class NotFoundException extends RuntimeException { + public NotFoundException(Class cls, Long id) { + super(cls.getSimpleName() + " with id: " + id + " does not exist!"); + } +} diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java new file mode 100644 index 0000000..b936b1c --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java @@ -0,0 +1,157 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.person; + + +import com.eureka.persons.base.AbstractEntity; +import com.eureka.persons.util.DateProcessor; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Transient; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import java.time.LocalDateTime; +import java.util.Objects; + + +@Entity +public class Person extends AbstractEntity { + interface BasicValidation{} + + @NotNull(groups = BasicValidation.class) + @Size(min = 3, max = 30, groups = BasicValidation.class) + @Column(nullable = false, unique = true) + private String username; + + @NotNull(groups = BasicValidation.class) + @Size(min = 3, max = 30, groups = BasicValidation.class) + @Column(nullable = false) + private String firstName; + + @NotNull(groups = BasicValidation.class) + @Size(min = 3, max = 30, groups = BasicValidation.class) + @Column(nullable = false) + private String lastName; + + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + @NotNull + @Size(min = 4, max = 50) + @Column(nullable = false) + private String password; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DateProcessor.DATE_FORMAT) + @NotNull(groups = BasicValidation.class) + @Column(nullable = false) + @DateTimeFormat(pattern = DateProcessor.DATE_FORMAT) + private LocalDateTime hiringDate; + + @JsonIgnore + @Transient + private String newPassword; + + public Person() { + super(); + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + 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 getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public LocalDateTime getHiringDate() { + return hiringDate; + } + + public void setHiringDate(LocalDateTime hiringDate) { + this.hiringDate = hiringDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + var person = (Person) o; + if (!Objects.equals(id, person.id)) return false; + return Objects.equals(firstName, person.firstName) && + Objects.equals(lastName, person.lastName) && + Objects.equals(hiringDate.toLocalDate(), person.hiringDate.toLocalDate()); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), firstName, lastName, hiringDate.toLocalDate()); + } + + @Override + public String toString() { + return String.format("Person[username='%s', firstName='%s', lastName='%s', hiringDate='%s']\n", + username, firstName, lastName, hiringDate.toString()); + + } + + public String getNewPassword() { + return newPassword; + } + + public void setNewPassword(String newPassword) { + this.newPassword = newPassword; + } + +} diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java b/eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java new file mode 100644 index 0000000..ab311ab --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java @@ -0,0 +1,92 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.services; + +import com.eureka.persons.person.Person; +import com.eureka.persons.util.DateProcessor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.PostConstruct; + +@Service +@Transactional +public class Initializer { + private Logger logger = LoggerFactory.getLogger(Initializer.class); + + private PersonService personService; + + public Initializer(PersonService personService) { + this.personService = personService; + } + + @PostConstruct + public void init() { + logger.info(" -->> Starting database initialization..."); + if (personService.findAll().isEmpty()) { + createPersons(); + } + logger.info(" -->> Database initialization finished."); + } + + private void createPersons() { + Person person = new Person(); + person.setUsername("sherlock.holmes"); + person.setFirstName("Sherlock"); + person.setLastName("Holmes"); + person.setPassword("dudu"); + person.setHiringDate(DateProcessor.toDate("1983-08-15 00:23")); + personService.save(person); + + person = new Person(); + person.setUsername("jackson.brodie"); + person.setFirstName("Jackson"); + person.setLastName("Brodie"); + person.setPassword("bagy"); + person.setHiringDate(DateProcessor.toDate("1983-06-22 00:23")); + personService.save(person); + + person = new Person(); + person.setUsername("nancy.drew"); + person.setFirstName("Nancy"); + person.setLastName("Drew"); + person.setPassword("dada45"); + person.setHiringDate(DateProcessor.toDate("1990-05-21 00:23")); + personService.save(person); + + person = new Person(); + person.setUsername("irene.adler"); + person.setFirstName("Irene"); + person.setLastName("Adler"); + person.setPassword("xxxyy"); + person.setHiringDate(DateProcessor.toDate("1987-03-11 00:23")); + personService.save(person); + } +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java new file mode 100644 index 0000000..3f30795 --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java @@ -0,0 +1,43 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.services; + +import com.eureka.persons.person.Person; + +import java.util.List; +import java.util.Optional; + +public interface PersonService { + List findAll(); + + Optional findById(Long id); + + Person save(Person person); + + void delete(Person person); +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java new file mode 100644 index 0000000..b2b2e5e --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java @@ -0,0 +1,70 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.services; + + +import com.eureka.persons.PersonRepo; +import com.eureka.persons.person.Person; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +@Service +@Transactional +public class PersonServiceImpl implements PersonService { + private PersonRepo personRepo; + + public PersonServiceImpl(PersonRepo personRepo) { + this.personRepo = personRepo; + } + + @Override + public List findAll() { + return personRepo.findAll(); + } + + @Override + public Optional findById(Long id) { + return personRepo.findById(id); + } + + + @Override + public Person save(Person person) { + personRepo.save(person); + return person; + } + + @Override + public void delete(Person person) { + personRepo.delete(person); + } +} + diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java b/eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java new file mode 100644 index 0000000..7becc70 --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java @@ -0,0 +1,44 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.util; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class DateProcessor { + public static final String DATE_FORMAT= "yyyy-MM-dd HH:mm"; + private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT); + + public static LocalDateTime toDate(final String date) { + return LocalDateTime.parse(date, formatter); + } + + public static String toString(final LocalDateTime date){ + return date.format(formatter); + } +} diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java b/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java new file mode 100644 index 0000000..e8caafa --- /dev/null +++ b/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java @@ -0,0 +1,53 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.eureka.persons.util; + +import java.util.Random; + +public final class NumberGenerator { + private static final Random RAND = new Random(); + private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static final String DIGITS = "0123456789"; + + public static String getPassword(){ + final var sb = new StringBuilder(); + for (int i = 0; i < 8; ++i) { + sb.append(randomCharacter()); + } + return sb.toString(); + } + + private static Character randomCharacter() { + final var all = UPPER.concat(UPPER.toLowerCase()).concat(DIGITS); + return all.charAt(RAND.nextInt(all.length() - 1)); + } + + private NumberGenerator() { + // prevent initialization fo this class + } +} diff --git a/eureka/persons-server/src/main/resources/banner.txt b/eureka/persons-server/src/main/resources/banner.txt new file mode 100644 index 0000000..be21922 --- /dev/null +++ b/eureka/persons-server/src/main/resources/banner.txt @@ -0,0 +1,7 @@ +__________ _________ .__ +\______ \ ___________ __________ ____ ______ / _____/ ______________ _|__| ____ ____ + | ___// __ \_ __ \/ ___/ _ \ / \ / ___/ \_____ \_/ __ \_ __ \ \/ / |/ ___\/ __ \ + | | \ ___/| | \/\___ ( <_> ) | \\___ \ / \ ___/| | \/\ /| \ \__\ ___/ + |____| \___ >__| /____ >____/|___| /____ > /_______ /\___ >__| \_/ |__|\___ >___ > + \/ \/ \/ \/ \/ \/ \/ \/ + :: Spring Boot :: (v2.2.4.RELEASE) diff --git a/eureka/persons-server/src/main/resources/persons-server.yml b/eureka/persons-server/src/main/resources/persons-server.yml new file mode 100644 index 0000000..0d60f33 --- /dev/null +++ b/eureka/persons-server/src/main/resources/persons-server.yml @@ -0,0 +1,51 @@ +spring: + application: + name: persons-service # Service registers under this name + datasource: + driver-class-name: org.h2.Driver + jdbc-url: jdbc:h2:mem:personsdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + username: sa + password: password + maximum-pool-size: 5 + connection-test-query: "SELECT 1" + pool-name: cemsPool + connection-timeout: 60000 + jpa: + generate-ddl: true + hibernate: + ddl-auto: create-drop + database-platform: org.hibernate.dialect.H2Dialect + h2: + console: + enabled: true +# HTTP Server +server: + port: 4001 # HTTP (Tomcat) port + address: 0.0.0.0 + +# Discovery Server Access +eureka: + client: + registerWithEureka: true + fetchRegistry: false + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + + + instance: + leaseRenewalIntervalInSeconds: 5 + preferIpAddress: false + +info: + app: + name: persons-server + description: Spring Cloud Application Managing Person Instances + version: 1.0-SNAPSHOT + +logging: + pattern: + console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" + level: + root: INFO + org.springframework: DEBUG + com.apress.cems: DEBUG diff --git a/eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java b/eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java new file mode 100644 index 0000000..4153297 --- /dev/null +++ b/eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java @@ -0,0 +1,16 @@ +package com.example.personsserver; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PersonsServerApplicationTests { + + //TODO configure discovery-server to be the Eureka's server and persons-server to be the client server + // hint1: If discovery-server was configured correctly it can be accessed on localhost:3000 + // hint2: If persons-server was configured correctly it should appear on localhost:3000 under DS Replicas section + @Test + void contextLoads() { + } + +} diff --git a/eureka/pom.xml b/eureka/pom.xml new file mode 100644 index 0000000..3a7d06f --- /dev/null +++ b/eureka/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.4 + + + com.example + eureka + 0.0.1-SNAPSHOT + eureka + eureka + + 11 + 2021.0.1 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/eureka/src/main/java/com/example/eureka/EurekaApplication.java b/eureka/src/main/java/com/example/eureka/EurekaApplication.java new file mode 100644 index 0000000..20c67a9 --- /dev/null +++ b/eureka/src/main/java/com/example/eureka/EurekaApplication.java @@ -0,0 +1,13 @@ +package com.example.eureka; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EurekaApplication { + + public static void main(String[] args) { + SpringApplication.run(EurekaApplication.class, args); + } + +} diff --git a/eureka/src/main/resources/application.properties b/eureka/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/eureka/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/eureka/src/test/java/com/example/eureka/EurekaApplicationTests.java b/eureka/src/test/java/com/example/eureka/EurekaApplicationTests.java new file mode 100644 index 0000000..1415105 --- /dev/null +++ b/eureka/src/test/java/com/example/eureka/EurekaApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.eureka; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class EurekaApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/spring1/.gitignore b/spring1/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/spring1/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/spring1/pom.xml b/spring1/pom.xml new file mode 100644 index 0000000..55666da --- /dev/null +++ b/spring1/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + com.example + spring1 + 0.0.1-SNAPSHOT + spring1 + spring1 + + 1.8 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.data + spring-data-jpa + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + 10 + 10 + + + + + + diff --git a/spring1/src/main/java/com/unitbv/Spring1Application.java b/spring1/src/main/java/com/unitbv/Spring1Application.java new file mode 100644 index 0000000..2d9a6da --- /dev/null +++ b/spring1/src/main/java/com/unitbv/Spring1Application.java @@ -0,0 +1,13 @@ +package com.unitbv; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Spring1Application { + + public static void main(String[] args) { + SpringApplication.run(Spring1Application.class, args); + } + +} diff --git a/spring1/src/main/java/com/unitbv/autowiring/AutowiredCfg.java b/spring1/src/main/java/com/unitbv/autowiring/AutowiredCfg.java new file mode 100644 index 0000000..db8f32e --- /dev/null +++ b/spring1/src/main/java/com/unitbv/autowiring/AutowiredCfg.java @@ -0,0 +1,9 @@ +package com.unitbv.autowiring; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = {"com.unitbv.autowiring"}) +public class AutowiredCfg { +} diff --git a/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceConstructorInjection.java b/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceConstructorInjection.java new file mode 100644 index 0000000..526880d --- /dev/null +++ b/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceConstructorInjection.java @@ -0,0 +1,14 @@ +package com.unitbv.autowiring.service; + +import com.unitbv.autowiring.util.FormatUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class FormatServiceConstructorInjection { + private final FormatUtil formatUtil; + + public void checkFormatted() { + System.out.println(this.formatUtil.formatted(true)); + } +} diff --git a/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceFieldInjection.java b/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceFieldInjection.java new file mode 100644 index 0000000..8873634 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceFieldInjection.java @@ -0,0 +1,14 @@ +package com.unitbv.autowiring.service; + +import com.unitbv.autowiring.util.FormatUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class FormatServiceFieldInjection { + public FormatUtil formatUtil; + + public void checkFormatted() { + System.out.println(this.formatUtil.formatted(true)); + } +} diff --git a/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceSetterInjection.java b/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceSetterInjection.java new file mode 100644 index 0000000..f2ec37e --- /dev/null +++ b/spring1/src/main/java/com/unitbv/autowiring/service/FormatServiceSetterInjection.java @@ -0,0 +1,14 @@ +package com.unitbv.autowiring.service; + +import com.unitbv.autowiring.util.FormatUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class FormatServiceSetterInjection { + private FormatUtil formatUtil; + + public void checkFormatted() { + System.out.println(this.formatUtil.formatted(true)); + } +} diff --git a/spring1/src/main/java/com/unitbv/autowiring/util/FormatUtil.java b/spring1/src/main/java/com/unitbv/autowiring/util/FormatUtil.java new file mode 100644 index 0000000..39025aa --- /dev/null +++ b/spring1/src/main/java/com/unitbv/autowiring/util/FormatUtil.java @@ -0,0 +1,13 @@ +package com.unitbv.autowiring.util; + +import org.springframework.stereotype.Component; + +@Component +public class FormatUtil { + public String formatted(boolean isFormatted) { + if (isFormatted) { + return "Everything was formatted."; + } + return "Couldn't format"; + } +} diff --git a/spring1/src/main/java/com/unitbv/beans/Book.java b/spring1/src/main/java/com/unitbv/beans/Book.java new file mode 100644 index 0000000..539f3af --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beans/Book.java @@ -0,0 +1,38 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beans; + +public class Book implements Item { + + private String title; + + @Override + public String getTitle() { + return title; + } +} diff --git a/spring1/src/main/java/com/unitbv/beans/Human.java b/spring1/src/main/java/com/unitbv/beans/Human.java new file mode 100644 index 0000000..5bbd304 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beans/Human.java @@ -0,0 +1,6 @@ +package com.unitbv.beans; + +public interface Human { + + Item getItem(); +} diff --git a/spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java b/spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java new file mode 100644 index 0000000..a004d5f --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java @@ -0,0 +1,36 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beans; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = {"com.unitbv.beans"} ) +public class HumanAppCfg { +} diff --git a/spring1/src/main/java/com/unitbv/beans/Item.java b/spring1/src/main/java/com/unitbv/beans/Item.java new file mode 100644 index 0000000..a06892b --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beans/Item.java @@ -0,0 +1,5 @@ +package com.unitbv.beans; + +public interface Item { + String getTitle(); +} diff --git a/spring1/src/main/java/com/unitbv/beans/Person.java b/spring1/src/main/java/com/unitbv/beans/Person.java new file mode 100644 index 0000000..617d8c3 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beans/Person.java @@ -0,0 +1,38 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beans; + +public class Person implements Human { + + private Item item; + + @Override + public Item getItem() { + return item; + } +} diff --git a/spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java b/spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java new file mode 100644 index 0000000..3189d49 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java @@ -0,0 +1,31 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beansnaming; + +public interface DependantBean { +} diff --git a/spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java b/spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java new file mode 100644 index 0000000..c263e7b --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java @@ -0,0 +1,37 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beansnaming; + + +public class DependantBeanImpl implements DependantBean { + private SimpleBean simpleBean; + + public DependantBeanImpl(SimpleBean simpleBean) { + this.simpleBean = simpleBean; + } +} diff --git a/spring1/src/main/java/com/unitbv/beansnaming/SimpleBean.java b/spring1/src/main/java/com/unitbv/beansnaming/SimpleBean.java new file mode 100644 index 0000000..d4b2076 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beansnaming/SimpleBean.java @@ -0,0 +1,4 @@ +package com.unitbv.beansnaming; + +public interface SimpleBean { +} diff --git a/spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java b/spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java new file mode 100644 index 0000000..24d9e22 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java @@ -0,0 +1,46 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beansnaming; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +//TODO test what will happen when you add a stereotype annotation +public class SimpleBeanImpl implements SimpleBean { + + public SimpleBeanImpl() { + Logger logger = LoggerFactory.getLogger(SimpleBeanImpl.class); + logger.info("[SimpleBeanImpl instantiation]"); + } + + @Override + public String toString() { + return "SimpleBeanImpl{ code: " + hashCode() + "}"; + } + +} diff --git a/spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java b/spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java new file mode 100644 index 0000000..aa336d4 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java @@ -0,0 +1,48 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beansnaming; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = {"com.unitbv.beansnaming"}) +public class SimpleDependantCfg { + + private final Logger logger = LoggerFactory.getLogger(SimpleDependantCfg.class); + + SimpleBean simpleBean() { + return new SimpleBeanImpl(); + } + + DependantBean dependantBean() { + return new DependantBeanImpl(simpleBean()); + } +} diff --git a/spring1/src/main/java/com/unitbv/lifecycle/DepBean.java b/spring1/src/main/java/com/unitbv/lifecycle/DepBean.java new file mode 100644 index 0000000..016da09 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/lifecycle/DepBean.java @@ -0,0 +1,34 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.lifecycle; + +import org.springframework.stereotype.Component; + +@Component +public class DepBean { +} diff --git a/spring1/src/main/java/com/unitbv/lifecycle/FunBean.java b/spring1/src/main/java/com/unitbv/lifecycle/FunBean.java new file mode 100644 index 0000000..9fe10a2 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/lifecycle/FunBean.java @@ -0,0 +1,43 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.lifecycle; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +public class FunBean { + private Logger logger = LoggerFactory.getLogger(FunBean.class); + + private DepBean depBean; +} diff --git a/spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java b/spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java new file mode 100644 index 0000000..f07c7c4 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java @@ -0,0 +1,41 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.lifecycle; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + + +@Configuration +@ComponentScan(basePackages = {"com.unitbv.lifecycle"}) +public class FunBeanCfg { + + FunBean funBean() { + return new FunBean(); + } +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/StereotypeCfg.java b/spring1/src/main/java/com/unitbv/stereotype/StereotypeCfg.java new file mode 100644 index 0000000..cb4586e --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/StereotypeCfg.java @@ -0,0 +1,9 @@ +package com.unitbv.stereotype; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = "com.unitbv") +public class StereotypeCfg { +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/controller/UserController.java b/spring1/src/main/java/com/unitbv/stereotype/controller/UserController.java new file mode 100644 index 0000000..797feeb --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/controller/UserController.java @@ -0,0 +1,4 @@ +package com.unitbv.stereotype.controller; + +public class UserController { +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/model/User.java b/spring1/src/main/java/com/unitbv/stereotype/model/User.java new file mode 100644 index 0000000..e7b6094 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/model/User.java @@ -0,0 +1,6 @@ +package com.unitbv.stereotype.model; + +public class User { + private Integer id; + private String name; +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/repository/UserRepository.java b/spring1/src/main/java/com/unitbv/stereotype/repository/UserRepository.java new file mode 100644 index 0000000..01ea991 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/repository/UserRepository.java @@ -0,0 +1,7 @@ +package com.unitbv.stereotype.repository; + +import com.unitbv.stereotype.model.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/repository/impl/UserRepositoryImpl.java b/spring1/src/main/java/com/unitbv/stereotype/repository/impl/UserRepositoryImpl.java new file mode 100644 index 0000000..9081e1e --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/repository/impl/UserRepositoryImpl.java @@ -0,0 +1,160 @@ +package com.unitbv.stereotype.repository.impl; + +import com.unitbv.stereotype.model.User; +import com.unitbv.stereotype.repository.UserRepository; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.repository.query.FluentQuery; + +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + +public class UserRepositoryImpl implements UserRepository { + @Override + public List findAll() { + return null; + } + + @Override + public List findAll(Sort sort) { + return null; + } + + @Override + public Page findAll(Pageable pageable) { + return null; + } + + @Override + public List findAllById(Iterable users) { + return null; + } + + @Override + public long count() { + return 0; + } + + @Override + public void deleteById(User user) { + + } + + @Override + public void delete(Integer entity) { + + } + + @Override + public void deleteAllById(Iterable users) { + + } + + @Override + public void deleteAll(Iterable entities) { + + } + + @Override + public void deleteAll() { + + } + + @Override + public S save(S entity) { + return null; + } + + @Override + public List saveAll(Iterable entities) { + return null; + } + + @Override + public Optional findById(User user) { + return Optional.empty(); + } + + @Override + public boolean existsById(User user) { + return false; + } + + @Override + public void flush() { + + } + + @Override + public S saveAndFlush(S entity) { + return null; + } + + @Override + public List saveAllAndFlush(Iterable entities) { + return null; + } + + @Override + public void deleteAllInBatch(Iterable entities) { + + } + + @Override + public void deleteAllByIdInBatch(Iterable users) { + + } + + @Override + public void deleteAllInBatch() { + + } + + @Override + public Integer getOne(User user) { + return null; + } + + @Override + public Integer getById(User user) { + return null; + } + + @Override + public Optional findOne(Example example) { + return Optional.empty(); + } + + @Override + public List findAll(Example example) { + return null; + } + + @Override + public List findAll(Example example, Sort sort) { + return null; + } + + @Override + public Page findAll(Example example, Pageable pageable) { + return null; + } + + @Override + public long count(Example example) { + return 0; + } + + @Override + public boolean exists(Example example) { + return false; + } + + @Override + public R findBy(Example example, Function, R> queryFunction) { + return null; + } +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/service/UserService.java b/spring1/src/main/java/com/unitbv/stereotype/service/UserService.java new file mode 100644 index 0000000..3b9ad34 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/service/UserService.java @@ -0,0 +1,4 @@ +package com.unitbv.stereotype.service; + +public interface UserService { +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/service/impl/UserServiceImpl.java b/spring1/src/main/java/com/unitbv/stereotype/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..afc9967 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/service/impl/UserServiceImpl.java @@ -0,0 +1,6 @@ +package com.unitbv.stereotype.service.impl; + +import com.unitbv.stereotype.service.UserService; + +public class UserServiceImpl implements UserService { +} diff --git a/spring1/src/main/java/com/unitbv/stereotype/util/UserUtil.java b/spring1/src/main/java/com/unitbv/stereotype/util/UserUtil.java new file mode 100644 index 0000000..1f440a4 --- /dev/null +++ b/spring1/src/main/java/com/unitbv/stereotype/util/UserUtil.java @@ -0,0 +1,4 @@ +package com.unitbv.stereotype.util; + +public class UserUtil { +} diff --git a/spring1/src/main/resources/application.properties b/spring1/src/main/resources/application.properties new file mode 100644 index 0000000..b2ff11b --- /dev/null +++ b/spring1/src/main/resources/application.properties @@ -0,0 +1 @@ +book.title="Dummy title" \ No newline at end of file diff --git a/spring1/src/test/java/com/unitbv/Spring1ApplicationTests.java b/spring1/src/test/java/com/unitbv/Spring1ApplicationTests.java new file mode 100644 index 0000000..8307489 --- /dev/null +++ b/spring1/src/test/java/com/unitbv/Spring1ApplicationTests.java @@ -0,0 +1,13 @@ +package com.unitbv; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Spring1ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/spring1/src/test/java/com/unitbv/autowiring/AutowiredCfgTest.java b/spring1/src/test/java/com/unitbv/autowiring/AutowiredCfgTest.java new file mode 100644 index 0000000..7e2b6d0 --- /dev/null +++ b/spring1/src/test/java/com/unitbv/autowiring/AutowiredCfgTest.java @@ -0,0 +1,39 @@ +package com.unitbv.autowiring; + +import com.unitbv.autowiring.service.FormatServiceConstructorInjection; +import com.unitbv.autowiring.service.FormatServiceFieldInjection; +import com.unitbv.autowiring.service.FormatServiceSetterInjection; +import com.unitbv.autowiring.util.FormatUtil; +import org.junit.jupiter.api.Test; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class AutowiredCfgTest { + //TODO use constructor injection, setter injection and field injection to add dependency in FormatServices classes + + @Test + void checkFormattedTestAutowireConstructorInjection() { + FormatUtil formatUtil = new FormatUtil(); + FormatServiceConstructorInjection formatServiceConstructorInjection = new FormatServiceConstructorInjection(formatUtil); + formatServiceConstructorInjection.checkFormatted(); + } + + @Test + void checkFormattedTestAutowireSetterInjection() { + FormatUtil formatUtil = new FormatUtil(); + FormatServiceSetterInjection formatServiceSetterInjection = new FormatServiceSetterInjection(); + formatServiceSetterInjection.setFormatUtil(formatUtil); + formatServiceSetterInjection.checkFormatted(); + } + + @Test + void checkFormattedTestAutowireFieldInjection() { + ConfigurableApplicationContext ctx = + new AnnotationConfigApplicationContext(AutowiredCfg.class); + FormatServiceFieldInjection formatServiceFieldInjection = ctx.getBean(FormatServiceFieldInjection.class); + assertNotNull(formatServiceFieldInjection); + formatServiceFieldInjection.checkFormatted(); + } +} diff --git a/spring1/src/test/java/com/unitbv/beans/HumanAppCfgTest.java b/spring1/src/test/java/com/unitbv/beans/HumanAppCfgTest.java new file mode 100644 index 0000000..45d1a77 --- /dev/null +++ b/spring1/src/test/java/com/unitbv/beans/HumanAppCfgTest.java @@ -0,0 +1,53 @@ +/* +Freeware License, some rights reserved + +Copyright (c) 2019 Iuliana Cosmina + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user's educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +package com.unitbv.beans; + +import org.junit.jupiter.api.Test; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class HumanAppCfgTest { + + //TODO make Book.class and Person.class to be recognized as beans by Spring and inject their values + // through constructors in order to make them compile + //title from Book class should be populated with a value that comes from application.properties file + @Test + void testHumanAndItem() { + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(HumanAppCfg.class); + + Human humanBean = ctx.getBean(Human.class); + + assertNotNull(humanBean); + assertNotNull(humanBean.getItem()); + assertNotNull(humanBean.getItem().getTitle()); + + ctx.close(); + } +} diff --git a/spring1/src/test/java/com/unitbv/beansnaming/SimpleDependantCfgTest.java b/spring1/src/test/java/com/unitbv/beansnaming/SimpleDependantCfgTest.java new file mode 100644 index 0000000..74fe6b5 --- /dev/null +++ b/spring1/src/test/java/com/unitbv/beansnaming/SimpleDependantCfgTest.java @@ -0,0 +1,65 @@ +package com.unitbv.beansnaming; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.jupiter.api.Assertions.*; + +public class SimpleDependantCfgTest { + + @Test + void testBeanNamingDefaultName() { + //TODO create definitions for SimpleBean and DependantBean and log some messages + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(SimpleDependantCfg.class); + + SimpleBean simpleBean = ctx.getBean("simpleBean", SimpleBean.class); + DependantBean dependantBean = ctx.getBean("dependantBean", DependantBean.class); + + assertNotNull(simpleBean); + assertNotNull(dependantBean); + + ctx.close(); + } + + @Test + void testBeanNamingSingleCustomName() { + //TODO define a name for your beans + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(SimpleDependantCfg.class); + + SimpleBean customSimpleBean = ctx.getBean("customSimpleBean", SimpleBean.class); + DependantBean customDependantBean = ctx.getBean("customDependantBean", DependantBean.class); + + assertNotNull(customSimpleBean); + assertNotNull(customDependantBean); + + // no bean named 'simpleBean' and 'dependantBean' + assertThrows(NoSuchBeanDefinitionException.class, () -> ctx.getBean("simpleBean", SimpleBean.class)); + assertThrows(NoSuchBeanDefinitionException.class, () -> ctx.getBean("dependantBean", DependantBean.class)); + + ctx.close(); + } + + @Test + void testBeanNamingMultipleCustomName() { + //TODO define multiple names for the same bean + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(SimpleDependantCfg.class); + + SimpleBean customSimpleBean1 = ctx.getBean("customSimpleBean1", SimpleBean.class); + SimpleBean customSimpleBean2 = ctx.getBean("customSimpleBean2", SimpleBean.class); + + DependantBean customDependantBean1 = ctx.getBean("customDependantBean1", DependantBean.class); + DependantBean customDependantBean2 = ctx.getBean("customDependantBean2", DependantBean.class); + + assertNotNull(customSimpleBean1); + assertNotNull(customSimpleBean2); + assertEquals(customDependantBean1, customDependantBean2); + + assertNotNull(customDependantBean1); + assertNotNull(customDependantBean2); + assertEquals(customDependantBean1, customDependantBean2); + + ctx.close(); + } +} diff --git a/spring1/src/test/java/com/unitbv/lifecycle/FunBeanCfgTest.java b/spring1/src/test/java/com/unitbv/lifecycle/FunBeanCfgTest.java new file mode 100644 index 0000000..b84d821 --- /dev/null +++ b/spring1/src/test/java/com/unitbv/lifecycle/FunBeanCfgTest.java @@ -0,0 +1,23 @@ +package com.unitbv.lifecycle; + +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class FunBeanCfgTest { + + @Test + void testBeanLifecycle() { + //TODO configure all lifecycle methods for FunBean and log messages just to be clear in each order are the methods called. + // hint 1: Use setter to inject a value for DepBean + // hint 2: @Bean has 2 values called initMethod and destroyMethod + // hint 3: you need to implement InitializingBean, DisposableBean + // hint 4: there should be 8 logged messages + var ctx = new AnnotationConfigApplicationContext(FunBeanCfg.class); + ctx.registerShutdownHook(); + + FunBean funBean = ctx.getBean(FunBean.class); + assertNotNull(funBean); + } +} diff --git a/spring1/src/test/java/com/unitbv/stereotype/StereotypeCfgTest.java b/spring1/src/test/java/com/unitbv/stereotype/StereotypeCfgTest.java new file mode 100644 index 0000000..df519f3 --- /dev/null +++ b/spring1/src/test/java/com/unitbv/stereotype/StereotypeCfgTest.java @@ -0,0 +1,35 @@ +package com.unitbv.stereotype; + +import com.unitbv.stereotype.controller.UserController; +import com.unitbv.stereotype.repository.UserRepository; +import com.unitbv.stereotype.repository.impl.UserRepositoryImpl; +import com.unitbv.stereotype.service.UserService; +import com.unitbv.stereotype.service.impl.UserServiceImpl; +import com.unitbv.stereotype.util.UserUtil; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class StereotypeCfgTest { + + //TODO add stereotype annotations in order to instantiate the beans. Use more specific annotations where you can + // Hint 1: there are 4 stereotype annotations + // Hint 2: pay attention to the configuration of StereotypeCfg.class + @Test + void testStereotypeAnnotations() { + var ctx = new AnnotationConfigApplicationContext(StereotypeCfg.class); + ctx.registerShutdownHook(); + + UserController userControllerBean = ctx.getBean(UserController.class); + UserService userServiceBean = ctx.getBean(UserServiceImpl.class); + UserRepository userRepositoryBean = ctx.getBean(UserRepositoryImpl.class); + UserUtil userUtilBean = ctx.getBean(UserUtil.class); + + assertNotNull(userControllerBean); + assertNotNull(userServiceBean); + assertNotNull(userRepositoryBean); + assertNotNull(userUtilBean); + + } +} From 24133ccd1beb80976225639d5534b7e51382baaa Mon Sep 17 00:00:00 2001 From: Stefan Petrescu Date: Wed, 2 Mar 2022 07:58:38 +0200 Subject: [PATCH 2/4] refactor --- .../example/discovery/DiscoveryServer.java | 29 ----- .../src/main/resources/discovery-server.yml | 12 +-- eureka/persons-server/pom.xml | 4 + .../java/com/eureka/persons/PersonRepo.java | 28 ----- .../com/eureka/persons/PersonsController.java | 102 ++---------------- .../com/eureka/persons/PersonsException.java | 28 ----- .../com/eureka/persons/PersonsServer.java | 31 ------ .../eureka/persons/base/AbstractEntity.java | 69 +----------- .../eureka/persons/ex/NotFoundException.java | 27 ----- .../com/eureka/persons/person/Person.java | 87 ++------------- .../eureka/persons/services/Initializer.java | 27 ----- .../persons/services/PersonService.java | 27 ----- .../persons/services/PersonServiceImpl.java | 28 ----- .../eureka/persons/util/DateProcessor.java | 27 ----- .../eureka/persons/util/NumberGenerator.java | 35 ------ .../src/main/resources/persons-server.yml | 12 +-- .../PersonsServerApplicationTests.java | 7 +- .../src/main/java/com/unitbv/beans/Book.java | 27 ----- .../java/com/unitbv/beans/HumanAppCfg.java | 27 ----- .../main/java/com/unitbv/beans/Person.java | 27 ----- .../com/unitbv/beansnaming/DependantBean.java | 27 ----- .../unitbv/beansnaming/DependantBeanImpl.java | 27 ----- .../unitbv/beansnaming/SimpleBeanImpl.java | 27 ----- .../beansnaming/SimpleDependantCfg.java | 27 ----- .../java/com/unitbv/lifecycle/DepBean.java | 27 ----- .../java/com/unitbv/lifecycle/FunBean.java | 27 ----- .../java/com/unitbv/lifecycle/FunBeanCfg.java | 27 ----- 27 files changed, 29 insertions(+), 821 deletions(-) diff --git a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java index 2f61fa9..15d783c 100644 --- a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java +++ b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java @@ -1,42 +1,13 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.example.discovery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import java.io.IOException; @SpringBootApplication -@EnableEurekaServer public class DiscoveryServer { private static Logger logger = LoggerFactory.getLogger(DiscoveryServer.class); diff --git a/eureka/discovery-server/src/main/resources/discovery-server.yml b/eureka/discovery-server/src/main/resources/discovery-server.yml index d8540ee..b55b43b 100644 --- a/eureka/discovery-server/src/main/resources/discovery-server.yml +++ b/eureka/discovery-server/src/main/resources/discovery-server.yml @@ -2,17 +2,7 @@ spring: application: name: discovery-service # Configure this Discovery Server -eureka: - instance: - hostname: localhost - client: - registerWithEureka: false # do not auto-register as client - fetchRegistry: false - -server: - port: 3000 # where this discovery server is accessible - waitTimeInMsWhenSyncEmpty: 0 - address: 0.0.0.0 +#TODO here you add configurations for server logging: pattern: diff --git a/eureka/persons-server/pom.xml b/eureka/persons-server/pom.xml index 245d2cd..b342d8e 100644 --- a/eureka/persons-server/pom.xml +++ b/eureka/persons-server/pom.xml @@ -66,6 +66,10 @@ validation-api 2.0.1.Final + + org.projectlombok + lombok + diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java index 767436d..5e7b169 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons; import com.eureka.persons.person.Person; @@ -46,7 +19,6 @@ public interface PersonRepo extends JpaRepository { @Query("select p from Person p where p.username like %?1%") List findByUsernameLike(String username); - @Query("select p from Person p where p.firstName=:fn") List findByFirstName(@Param("fn") String firstName); diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java index 7545a0b..ff4acb5 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java @@ -1,50 +1,14 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons; -import com.eureka.persons.ex.NotFoundException; import com.eureka.persons.person.Person; import com.eureka.persons.services.PersonService; -import com.eureka.persons.util.NumberGenerator; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; -import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.*; -import org.springframework.web.util.UriTemplate; -import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; import java.util.List; -import java.util.Optional; - -import static com.eureka.persons.base.AbstractEntity.COMPARATOR_BY_ID; @RestController @RequestMapping("/persons") @@ -58,54 +22,20 @@ public PersonsController(PersonService personService) { /** * Handles requests to list all persons. */ + //TODO find all persons using the functions already implemented and sort them by id @ResponseStatus(HttpStatus.OK) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public List list() { - List persons = personService.findAll(); - persons.sort(COMPARATOR_BY_ID); - return persons; + return new ArrayList<>(); } /** * Handles requests to create a person. */ + //TODO save a person to the db or throw PersonsException @ResponseStatus(HttpStatus.CREATED) @PostMapping - public void create(@RequestBody Person person, BindingResult result, @Value("#{request.requestURL}") - StringBuffer originalUrl, HttpServletResponse response) { - if (result.hasErrors()) { - var errString = createErrorString(result); - throw new PersonsException(HttpStatus.BAD_REQUEST, "Cannot save entry because: " + errString); - } - if (StringUtils.isEmpty(person.getPassword())) { - person.setPassword(NumberGenerator.getPassword()); - } - try { - var newPerson = personService.save(person); - response.setHeader("Location", getLocationForUser(originalUrl, newPerson.getId())); - } catch (Exception e) { - throw new PersonsException(HttpStatus.UNPROCESSABLE_ENTITY, e); - } - } - - /** - * Determines URL of user resource based on the full URL of the given request, - * appending the path info with the given childIdentifier using a UriTemplate. - */ - static String getLocationForUser(StringBuffer url, Object childIdentifier) { - var template = new UriTemplate(url.toString() + "/{id}"); - return template.expand(childIdentifier).toASCIIString(); - } - - private String createErrorString(BindingResult result) { - var sb = new StringBuilder(); - result.getAllErrors().forEach(error -> { - if (error instanceof FieldError) { - FieldError err = (FieldError) error; - sb.append("Field '").append(err.getField()).append("' value error: ").append(err.getDefaultMessage()).append("\n"); - } - }); - return sb.toString(); + public void create(@RequestBody Person person, BindingResult result) { } /** @@ -114,15 +44,11 @@ private String createErrorString(BindingResult result) { * @param id * @return */ + //TODO find a person by id or throw NotFoundException @ResponseStatus(HttpStatus.OK) @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public Person show(@PathVariable Long id) { - var personOpt = personService.findById(id); - if (personOpt.isPresent()) { - return personOpt.get(); - } else { - throw new NotFoundException(Person.class, id); - } + return new Person(); } /** @@ -132,19 +58,10 @@ public Person show(@PathVariable Long id) { * @param id * @return */ + //TODO update an existing person if found else throw NotFoundException @ResponseStatus(HttpStatus.NO_CONTENT) @PutMapping("/{id}") public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { - Optional personOpt = personService.findById(id); - if (personOpt.isPresent()) { - var person = personOpt.get(); - person.setUsername(updatedPerson.getUsername()); - person.setFirstName(updatedPerson.getFirstName()); - person.setLastName(updatedPerson.getLastName()); - personService.save(person); - } else { - throw new NotFoundException(Person.class, id); - } } /** @@ -152,10 +69,9 @@ public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { * * @param id */ + //TODO delete a person @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { - var personOpt = personService.findById(id); - personOpt.ifPresent(value -> personService.delete(value)); } } \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java index c5c6ccf..781c4f6 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsException.java @@ -1,35 +1,7 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons; import org.springframework.http.HttpStatus; - public class PersonsException extends RuntimeException{ private HttpStatus status; diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java index bde4308..f2098ae 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons; import org.slf4j.Logger; @@ -32,15 +5,11 @@ of this software and associated documentation files (the "Software"), import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import java.io.IOException; - @EntityScan(basePackages = "com.eureka.persons") @SpringBootApplication -@EnableEurekaClient public class PersonsServer { private static Logger logger = LoggerFactory.getLogger(PersonsServer.class); diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java index e017cfc..199103b 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java @@ -1,34 +1,9 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.base; import com.eureka.persons.util.DateProcessor; import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Getter; +import lombok.Setter; import org.springframework.format.annotation.DateTimeFormat; import javax.persistence.*; @@ -38,6 +13,8 @@ of this software and associated documentation files (the "Software"), import java.util.Objects; @MappedSuperclass +@Getter +@Setter public abstract class AbstractEntity implements Serializable { public static Comparator COMPARATOR_BY_ID = Comparator.comparing(AbstractEntity::getId); @@ -69,44 +46,6 @@ protected AbstractEntity() { modifiedAt = LocalDateTime.now(); } - /** - * Returns the entity identifier. This identifier is unique per entity. It is used by persistence frameworks used in a project, - * and although is public, it should not be used by application code. - * This identifier is mapped by ORM (Object Relational Mapper) to the database primary key of the Person record to which - * the entity instance is mapped. - * - * @return the unique entity identifier - */ - public Long getId() { - return id; - } - - /** - * Sets the entity identifier. This identifier is unique per entity. Is is used by persistence frameworks - * and although is public, it should never be set by application code. - * - * @param id the unique entity identifier - */ - public void setId(Long id) { - this.id = id; - } - - public LocalDateTime getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(LocalDateTime createdAt) { - this.createdAt = createdAt; - } - - public LocalDateTime getModifiedAt() { - return modifiedAt; - } - - public void setModifiedAt(LocalDateTime modifiedAt) { - this.modifiedAt = modifiedAt; - } - // IDE generated methods @Override public boolean equals(Object o) { diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java b/eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java index f131fd7..094ecec 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/ex/NotFoundException.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.ex; public class NotFoundException extends RuntimeException { diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java index b936b1c..9064df8 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java @@ -1,38 +1,13 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.person; - import com.eureka.persons.base.AbstractEntity; import com.eureka.persons.util.DateProcessor; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import org.springframework.format.annotation.DateTimeFormat; import javax.persistence.Column; @@ -43,8 +18,10 @@ of this software and associated documentation files (the "Software"), import java.time.LocalDateTime; import java.util.Objects; - @Entity +@Getter +@Setter +@NoArgsConstructor public class Person extends AbstractEntity { interface BasicValidation{} @@ -79,50 +56,6 @@ interface BasicValidation{} @Transient private String newPassword; - public Person() { - super(); - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - 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 getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public LocalDateTime getHiringDate() { - return hiringDate; - } - - public void setHiringDate(LocalDateTime hiringDate) { - this.hiringDate = hiringDate; - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -146,12 +79,4 @@ public String toString() { } - public String getNewPassword() { - return newPassword; - } - - public void setNewPassword(String newPassword) { - this.newPassword = newPassword; - } - } diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java b/eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java index ab311ab..22024a4 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/services/Initializer.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.services; import com.eureka.persons.person.Person; diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java index 3f30795..0f86a73 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonService.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.services; import com.eureka.persons.person.Person; diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java index b2b2e5e..f1c2fec 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/services/PersonServiceImpl.java @@ -1,33 +1,5 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.services; - import com.eureka.persons.PersonRepo; import com.eureka.persons.person.Person; import org.springframework.stereotype.Service; diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java b/eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java index 7becc70..0d2f624 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/util/DateProcessor.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.util; import java.time.LocalDateTime; diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java b/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java index e8caafa..92b1c40 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/util/NumberGenerator.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.eureka.persons.util; import java.util.Random; @@ -34,14 +7,6 @@ public final class NumberGenerator { private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final String DIGITS = "0123456789"; - public static String getPassword(){ - final var sb = new StringBuilder(); - for (int i = 0; i < 8; ++i) { - sb.append(randomCharacter()); - } - return sb.toString(); - } - private static Character randomCharacter() { final var all = UPPER.concat(UPPER.toLowerCase()).concat(DIGITS); return all.charAt(RAND.nextInt(all.length() - 1)); diff --git a/eureka/persons-server/src/main/resources/persons-server.yml b/eureka/persons-server/src/main/resources/persons-server.yml index 0d60f33..b2b466d 100644 --- a/eureka/persons-server/src/main/resources/persons-server.yml +++ b/eureka/persons-server/src/main/resources/persons-server.yml @@ -24,17 +24,7 @@ server: address: 0.0.0.0 # Discovery Server Access -eureka: - client: - registerWithEureka: true - fetchRegistry: false - serviceUrl: - defaultZone: http://localhost:3000/eureka/ - - - instance: - leaseRenewalIntervalInSeconds: 5 - preferIpAddress: false +#TODO here you add configurations for eureka client info: app: diff --git a/eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java b/eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java index 4153297..2fd298e 100644 --- a/eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java +++ b/eureka/persons-server/src/test/java/com/example/personsserver/PersonsServerApplicationTests.java @@ -6,9 +6,10 @@ @SpringBootTest class PersonsServerApplicationTests { - //TODO configure discovery-server to be the Eureka's server and persons-server to be the client server - // hint1: If discovery-server was configured correctly it can be accessed on localhost:3000 - // hint2: If persons-server was configured correctly it should appear on localhost:3000 under DS Replicas section + //TODO configure discovery-server to be the Eureka's server (on port 3000) and persons-server to be the client server (on port 4001) + // hint1: you need to add some configurations in resources -> .yml files + // After you start your server and your client you need to create the endpoints from PersonsController, you have more details there + // Use postman to test the endpoints. Create a new collection and add all 5 endpoints inside of it. @Test void contextLoads() { } diff --git a/spring1/src/main/java/com/unitbv/beans/Book.java b/spring1/src/main/java/com/unitbv/beans/Book.java index 539f3af..3429ac6 100644 --- a/spring1/src/main/java/com/unitbv/beans/Book.java +++ b/spring1/src/main/java/com/unitbv/beans/Book.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.beans; public class Book implements Item { diff --git a/spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java b/spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java index a004d5f..a38cf2b 100644 --- a/spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java +++ b/spring1/src/main/java/com/unitbv/beans/HumanAppCfg.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.beans; import org.springframework.context.annotation.ComponentScan; diff --git a/spring1/src/main/java/com/unitbv/beans/Person.java b/spring1/src/main/java/com/unitbv/beans/Person.java index 617d8c3..3a851d0 100644 --- a/spring1/src/main/java/com/unitbv/beans/Person.java +++ b/spring1/src/main/java/com/unitbv/beans/Person.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.beans; public class Person implements Human { diff --git a/spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java b/spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java index 3189d49..b47f156 100644 --- a/spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java +++ b/spring1/src/main/java/com/unitbv/beansnaming/DependantBean.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.beansnaming; public interface DependantBean { diff --git a/spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java b/spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java index c263e7b..ff79055 100644 --- a/spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java +++ b/spring1/src/main/java/com/unitbv/beansnaming/DependantBeanImpl.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.beansnaming; diff --git a/spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java b/spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java index 24d9e22..c83ca34 100644 --- a/spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java +++ b/spring1/src/main/java/com/unitbv/beansnaming/SimpleBeanImpl.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.beansnaming; import org.slf4j.Logger; diff --git a/spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java b/spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java index aa336d4..0d57764 100644 --- a/spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java +++ b/spring1/src/main/java/com/unitbv/beansnaming/SimpleDependantCfg.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.beansnaming; import org.slf4j.Logger; diff --git a/spring1/src/main/java/com/unitbv/lifecycle/DepBean.java b/spring1/src/main/java/com/unitbv/lifecycle/DepBean.java index 016da09..808134f 100644 --- a/spring1/src/main/java/com/unitbv/lifecycle/DepBean.java +++ b/spring1/src/main/java/com/unitbv/lifecycle/DepBean.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.lifecycle; import org.springframework.stereotype.Component; diff --git a/spring1/src/main/java/com/unitbv/lifecycle/FunBean.java b/spring1/src/main/java/com/unitbv/lifecycle/FunBean.java index 9fe10a2..3cb72a8 100644 --- a/spring1/src/main/java/com/unitbv/lifecycle/FunBean.java +++ b/spring1/src/main/java/com/unitbv/lifecycle/FunBean.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.lifecycle; import org.slf4j.Logger; diff --git a/spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java b/spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java index f07c7c4..55796b2 100644 --- a/spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java +++ b/spring1/src/main/java/com/unitbv/lifecycle/FunBeanCfg.java @@ -1,30 +1,3 @@ -/* -Freeware License, some rights reserved - -Copyright (c) 2019 Iuliana Cosmina - -Permission is hereby granted, free of charge, to anyone obtaining a copy -of this software and associated documentation files (the "Software"), -to work with the Software within the limits of freeware distribution and fair use. -This includes the rights to use, copy, and modify the Software for personal use. -Users are also allowed and encouraged to submit corrections and modifications -to the Software for the benefit of other users. - -It is not allowed to reuse, modify, or redistribute the Software for -commercial use in any way, or for a user's educational materials such as books -or blog articles without prior permission from the copyright holder. - -The above copyright notice and this permission notice need to be included -in all copies or substantial portions of the software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ package com.unitbv.lifecycle; import org.springframework.context.annotation.ComponentScan; From c5fa76e44f0d48e56b923e2100d6ae6781e303b5 Mon Sep 17 00:00:00 2001 From: Ovidiu Popa Date: Thu, 31 Mar 2022 10:58:22 +0300 Subject: [PATCH 3/4] lab6 --- eureka/discovery-server/pom.xml | 4 -- eureka/pom.xml | 5 ++ lab-6-api-gateway/.gitignore | 33 +++++++++ .../api-gateway-project/.gitignore | 33 +++++++++ lab-6-api-gateway/api-gateway-project/pom.xml | 68 +++++++++++++++++++ .../ApiGatewayProjectApplication.java | 13 ++++ .../src/main/resources/application.yml | 5 ++ .../ApiGatewayProjectApplicationTests.java | 13 ++++ lab-6-api-gateway/pom.xml | 47 +++++++++++++ lab-6-api-gateway/service1/.gitignore | 33 +++++++++ lab-6-api-gateway/service1/pom.xml | 61 +++++++++++++++++ .../example/service1/Service1Application.java | 20 ++++++ .../src/main/resources/application.yml | 2 + .../service1/Service1ApplicationTests.java | 13 ++++ lab-6-api-gateway/service2/.gitignore | 33 +++++++++ lab-6-api-gateway/service2/pom.xml | 61 +++++++++++++++++ .../example/service2/Service2Application.java | 19 ++++++ .../src/main/resources/application.yml | 3 + .../service2/Service2ApplicationTests.java | 13 ++++ .../Lab6ApiGatewayApplication.java | 13 ++++ .../src/main/resources/application.properties | 1 + .../Lab6ApiGatewayApplicationTests.java | 13 ++++ pom.xml | 2 + 23 files changed, 504 insertions(+), 4 deletions(-) create mode 100644 lab-6-api-gateway/.gitignore create mode 100644 lab-6-api-gateway/api-gateway-project/.gitignore create mode 100644 lab-6-api-gateway/api-gateway-project/pom.xml create mode 100644 lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java create mode 100644 lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml create mode 100644 lab-6-api-gateway/api-gateway-project/src/test/java/com/example/apigatewayproject/ApiGatewayProjectApplicationTests.java create mode 100644 lab-6-api-gateway/pom.xml create mode 100644 lab-6-api-gateway/service1/.gitignore create mode 100644 lab-6-api-gateway/service1/pom.xml create mode 100644 lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java create mode 100644 lab-6-api-gateway/service1/src/main/resources/application.yml create mode 100644 lab-6-api-gateway/service1/src/test/java/com/example/service1/Service1ApplicationTests.java create mode 100644 lab-6-api-gateway/service2/.gitignore create mode 100644 lab-6-api-gateway/service2/pom.xml create mode 100644 lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java create mode 100644 lab-6-api-gateway/service2/src/main/resources/application.yml create mode 100644 lab-6-api-gateway/service2/src/test/java/com/example/service2/Service2ApplicationTests.java create mode 100644 lab-6-api-gateway/src/main/java/com/example/lab6apigateway/Lab6ApiGatewayApplication.java create mode 100644 lab-6-api-gateway/src/main/resources/application.properties create mode 100644 lab-6-api-gateway/src/test/java/com/example/lab6apigateway/Lab6ApiGatewayApplicationTests.java diff --git a/eureka/discovery-server/pom.xml b/eureka/discovery-server/pom.xml index d9e9e18..fd67994 100644 --- a/eureka/discovery-server/pom.xml +++ b/eureka/discovery-server/pom.xml @@ -18,10 +18,6 @@ 2021.0.1 - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-client - org.springframework.cloud spring-cloud-starter-netflix-eureka-server diff --git a/eureka/pom.xml b/eureka/pom.xml index 3a7d06f..1049986 100644 --- a/eureka/pom.xml +++ b/eureka/pom.xml @@ -13,10 +13,15 @@ 0.0.1-SNAPSHOT eureka eureka + pom 11 2021.0.1 + + discovery-server + persons-server + org.springframework.boot diff --git a/lab-6-api-gateway/.gitignore b/lab-6-api-gateway/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/lab-6-api-gateway/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lab-6-api-gateway/api-gateway-project/.gitignore b/lab-6-api-gateway/api-gateway-project/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lab-6-api-gateway/api-gateway-project/pom.xml b/lab-6-api-gateway/api-gateway-project/pom.xml new file mode 100644 index 0000000..871ce8d --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.5 + + + com.example + api-gateway-project + 0.0.1-SNAPSHOT + api-gateway-project + api-gateway-project + + 11 + 2021.0.1 + + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java b/lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java new file mode 100644 index 0000000..e0f6114 --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java @@ -0,0 +1,13 @@ +package com.example.apigatewayproject; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ApiGatewayProjectApplication { + + public static void main(String[] args) { + SpringApplication.run(ApiGatewayProjectApplication.class, args); + } + +} diff --git a/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml b/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml new file mode 100644 index 0000000..3847331 --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml @@ -0,0 +1,5 @@ +server: + port: 8080 +#TODO use eureka to discover the URL for the service1 and service2 +#TODO configure spring cloud gateway to route the request to downstream services (service1 and service2) based on the paths(/greeting, /product) +#and method types (GET,POST) \ No newline at end of file diff --git a/lab-6-api-gateway/api-gateway-project/src/test/java/com/example/apigatewayproject/ApiGatewayProjectApplicationTests.java b/lab-6-api-gateway/api-gateway-project/src/test/java/com/example/apigatewayproject/ApiGatewayProjectApplicationTests.java new file mode 100644 index 0000000..6c58423 --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/src/test/java/com/example/apigatewayproject/ApiGatewayProjectApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.apigatewayproject; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ApiGatewayProjectApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/lab-6-api-gateway/pom.xml b/lab-6-api-gateway/pom.xml new file mode 100644 index 0000000..aa491ab --- /dev/null +++ b/lab-6-api-gateway/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.5 + + + com.example + lab-6-api-gateway + 0.0.1-SNAPSHOT + lab-6-api-gateway + lab-6-api-gateway + + 11 + + pom + + api-gateway-project + service1 + service2 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lab-6-api-gateway/service1/.gitignore b/lab-6-api-gateway/service1/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/lab-6-api-gateway/service1/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lab-6-api-gateway/service1/pom.xml b/lab-6-api-gateway/service1/pom.xml new file mode 100644 index 0000000..478fe3f --- /dev/null +++ b/lab-6-api-gateway/service1/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.5 + + + com.example + service1 + 0.0.1-SNAPSHOT + service1 + service1 + + 11 + 2021.0.1 + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java new file mode 100644 index 0000000..2e2f439 --- /dev/null +++ b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java @@ -0,0 +1,20 @@ +package com.example.service1; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Service1Application { + + public static void main(String[] args) { + SpringApplication.run(Service1Application.class, args); + } + + // TODO + // 1. define a GET endpoint /greeting which should accept a query parameter "name" + // 2. return should be a string returning a greeting: Hello Brasov + // 3. print request headers + // 4. register the service in eureka + + +} diff --git a/lab-6-api-gateway/service1/src/main/resources/application.yml b/lab-6-api-gateway/service1/src/main/resources/application.yml new file mode 100644 index 0000000..54b155f --- /dev/null +++ b/lab-6-api-gateway/service1/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 8081 \ No newline at end of file diff --git a/lab-6-api-gateway/service1/src/test/java/com/example/service1/Service1ApplicationTests.java b/lab-6-api-gateway/service1/src/test/java/com/example/service1/Service1ApplicationTests.java new file mode 100644 index 0000000..539fcde --- /dev/null +++ b/lab-6-api-gateway/service1/src/test/java/com/example/service1/Service1ApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.service1; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Service1ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/lab-6-api-gateway/service2/.gitignore b/lab-6-api-gateway/service2/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/lab-6-api-gateway/service2/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lab-6-api-gateway/service2/pom.xml b/lab-6-api-gateway/service2/pom.xml new file mode 100644 index 0000000..be06918 --- /dev/null +++ b/lab-6-api-gateway/service2/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.5 + + + com.example + service2 + 0.0.1-SNAPSHOT + service2 + service2 + + 11 + 2021.0.1 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java new file mode 100644 index 0000000..3c2fbd2 --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java @@ -0,0 +1,19 @@ +package com.example.service2; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Service2Application { + + public static void main(String[] args) { + SpringApplication.run(Service2Application.class, args); + } + // TODO + // 1. define a POST endpoint /product which should accept a request body containing two properties -product name and quantity + //2. save the request body in memory + // 3. return 200 if OK + // 4. print request headers + // 5. register the service in eureka + // 6. define a GET endpoint /product to return the saved data using the POST endpoint - return type is List +} diff --git a/lab-6-api-gateway/service2/src/main/resources/application.yml b/lab-6-api-gateway/service2/src/main/resources/application.yml new file mode 100644 index 0000000..4772153 --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/resources/application.yml @@ -0,0 +1,3 @@ + +server: + port: 8082 \ No newline at end of file diff --git a/lab-6-api-gateway/service2/src/test/java/com/example/service2/Service2ApplicationTests.java b/lab-6-api-gateway/service2/src/test/java/com/example/service2/Service2ApplicationTests.java new file mode 100644 index 0000000..3da138b --- /dev/null +++ b/lab-6-api-gateway/service2/src/test/java/com/example/service2/Service2ApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.service2; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Service2ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/lab-6-api-gateway/src/main/java/com/example/lab6apigateway/Lab6ApiGatewayApplication.java b/lab-6-api-gateway/src/main/java/com/example/lab6apigateway/Lab6ApiGatewayApplication.java new file mode 100644 index 0000000..eb83cff --- /dev/null +++ b/lab-6-api-gateway/src/main/java/com/example/lab6apigateway/Lab6ApiGatewayApplication.java @@ -0,0 +1,13 @@ +package com.example.lab6apigateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Lab6ApiGatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(Lab6ApiGatewayApplication.class, args); + } + +} diff --git a/lab-6-api-gateway/src/main/resources/application.properties b/lab-6-api-gateway/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/lab-6-api-gateway/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/lab-6-api-gateway/src/test/java/com/example/lab6apigateway/Lab6ApiGatewayApplicationTests.java b/lab-6-api-gateway/src/test/java/com/example/lab6apigateway/Lab6ApiGatewayApplicationTests.java new file mode 100644 index 0000000..af4ce01 --- /dev/null +++ b/lab-6-api-gateway/src/test/java/com/example/lab6apigateway/Lab6ApiGatewayApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.lab6apigateway; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Lab6ApiGatewayApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/pom.xml b/pom.xml index 487bfbd..1ccc714 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,8 @@ java8 rest-service + eureka + lab-6-api-gateway From 604aaea3213e4194acbde530340254dbbbd7777f Mon Sep 17 00:00:00 2001 From: Ovidiu Popa Date: Thu, 31 Mar 2022 13:43:17 +0300 Subject: [PATCH 4/4] lab6 --- .../api-gateway-project/src/main/resources/application.yml | 3 ++- .../main/java/com/example/service1/Service1Application.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml b/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml index 3847331..aaad35d 100644 --- a/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml +++ b/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml @@ -1,5 +1,6 @@ server: port: 8080 #TODO use eureka to discover the URL for the service1 and service2 -#TODO configure spring cloud gateway to route the request to downstream services (service1 and service2) based on the paths(/greeting, /product) +#TODO configure spring cloud gateway to route the request to downstream services (service1 and service2) based on the paths(/api/greeting, /product) +#TODO for greeting endpoint add a route to accept requests to /greeting but before calling service1 it must append api before the greeting path (HINT: rewrite path filter) #and method types (GET,POST) \ No newline at end of file diff --git a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java index 2e2f439..27128b5 100644 --- a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java +++ b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java @@ -11,7 +11,7 @@ public static void main(String[] args) { } // TODO - // 1. define a GET endpoint /greeting which should accept a query parameter "name" + // 1. define a GET endpoint /api/greeting which should accept a query parameter "name" // 2. return should be a string returning a greeting: Hello Brasov // 3. print request headers // 4. register the service in eureka