Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teme 5 + 6 (Eureka si API Gateway) #101

Open
wants to merge 2 commits into
base: lab6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
# Compiled class file
*.class
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

# IntelliJ IDEA
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### VS Code ###
.vscode/
2 changes: 1 addition & 1 deletion eureka/discovery-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
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;

@EnableEurekaServer
@SpringBootApplication
public class DiscoveryServer {

Expand All @@ -22,4 +24,4 @@ public static void main(String... args) throws IOException {
System.in.read();
ctx.close();
}
}
}
12 changes: 9 additions & 3 deletions eureka/discovery-server/src/main/resources/discovery-server.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
spring:
application:
name: discovery-service
# Configure this Discovery Server
#TODO here you add configurations for server

eureka:
client:
register-with-eureka: false
fetch-registry: false

server:
port: 3000

logging:
pattern:
console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
level:
root: INFO
org.springframework: DEBUG
com.apress.cems: DEBUG
com.apress.cems: DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class DiscoveryServerApplicationTests {

Expand All @@ -11,3 +12,5 @@ void contextLoads() {
}

}


2 changes: 1 addition & 1 deletion eureka/persons-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ public interface PersonRepo extends JpaRepository<Person, Long> {

@Query("select p from Person p where p.hiringDate=:hd")
List<Person> findByHiringDate(@Param("hd") LocalDateTime date);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.eureka.persons;

import com.eureka.persons.person.Person;
import com.eureka.persons.services.PersonService;
import java.util.Comparator;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.eureka.persons.ex.NotFoundException;
import com.eureka.persons.person.Person;
import com.eureka.persons.services.PersonService;

@RestController
@RequestMapping("/persons")
Expand All @@ -26,7 +34,16 @@ public PersonsController(PersonService personService) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Person> list() {
return new ArrayList<>();
List<Person> people = personService.findAll();

people.sort(new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getId().compareTo(p2.getId());
}
});

return people;
}

/**
Expand All @@ -36,6 +53,11 @@ public List<Person> list() {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public void create(@RequestBody Person person, BindingResult result) {
if(result.hasErrors()){
throw new PersonsException(HttpStatus.BAD_REQUEST, "There was an error.");
} else {
personService.save(person);
}
}

/**
Expand All @@ -48,7 +70,7 @@ public void create(@RequestBody Person person, BindingResult result) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public Person show(@PathVariable Long id) {
return new Person();
return personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id));
}

/**
Expand All @@ -62,6 +84,16 @@ public Person show(@PathVariable Long id) {
@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}")
public void update(@RequestBody Person updatedPerson, @PathVariable Long id) {
Person person = personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id));

person.setUsername(updatedPerson.getUsername());
person.setFirstName(updatedPerson.getFirstName());
person.setLastName(updatedPerson.getLastName());
person.setPassword(updatedPerson.getPassword());
person.setHiringDate(updatedPerson.getHiringDate());
person.setNewPassword(updatedPerson.getNewPassword());

personService.save(person);
}

/**
Expand All @@ -73,5 +105,6 @@ public void update(@RequestBody Person updatedPerson, @PathVariable Long id) {
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
personService.delete(personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
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 java.io.IOException;

@EnableEurekaClient
@EntityScan(basePackages = "com.eureka.persons")
@SpringBootApplication
public class PersonsServer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import org.springframework.format.annotation.DateTimeFormat;
import com.eureka.persons.util.DateProcessor;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;

@MappedSuperclass
@Getter
Expand All @@ -24,6 +28,14 @@ public abstract class AbstractEntity implements Serializable {
@Column(updatable = false)
protected Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Version
protected int version;

Expand Down Expand Up @@ -51,7 +63,6 @@ protected AbstractEntity() {
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;
Expand All @@ -67,4 +78,4 @@ 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Transient;
Expand Down Expand Up @@ -76,7 +75,53 @@ public int hashCode() {
public String toString() {
return String.format("Person[username='%s', firstName='%s', lastName='%s', hiringDate='%s']\n",
username, firstName, lastName, hiringDate.toString());
}

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;
}

public String getNewPassword() {
return newPassword;
}

public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
}
10 changes: 8 additions & 2 deletions eureka/persons-server/src/main/resources/persons-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ server:
# Discovery Server Access
#TODO here you add configurations for eureka client

eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/
fetchRegistry: true

info:
app:
name: persons-server
Expand All @@ -36,6 +42,6 @@ logging:
pattern:
console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
level:
root: INFO
root: DEBUG
org.springframework: DEBUG
com.apress.cems: DEBUG
com.apress.cems: DEBUG
6 changes: 4 additions & 2 deletions java8/src/test/java/com/unitbv/Java8Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ public void getGetMapOfUsers(){
public void testGetPredicateForFilteringName(){
String name = "John";
Predicate<User> predicate = dataSource.getPredicateForFilteringByName(name);
List<User> expected = Stream.of(new User(1, "John", "Wick", 35, "actor"))
.collect(Collectors.toList());
List<User> expected = Stream.of(
new User(1, "John", "Wick", 35, "actor"),
new User(7, "Mark", "John", 17, "student")
).collect(Collectors.toList());
List<User> actual = dataSource.filterUsers(predicate);
Assertions.assertEquals(expected, actual);
}
Expand Down
Loading