Skip to content

Commit

Permalink
New spring boot and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gtiwari333 committed Sep 21, 2024
1 parent 4b9ff00 commit f2fd824
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 138 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ services:
mysql:
image: 'mysql'
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=noteappdb
- "MYSQL_ROOT_PASSWORD=password"
- "MYSQL_DATABASE=noteappdb"
ports:
- 3306:3306
command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp
Expand Down
56 changes: 28 additions & 28 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<version>3.4.0-M3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -16,25 +16,25 @@
<properties>
<java.version>21</java.version>

<springdoc-openapi-ui.version>2.3.0</springdoc-openapi-ui.version>
<mapstruct.version>1.6.0.Beta1</mapstruct.version>
<springdoc-openapi-ui.version>2.6.0</springdoc-openapi-ui.version>
<mapstruct.version>1.6.2</mapstruct.version>

<selenide.version>7.1.0</selenide.version>
<selenide.version>7.5.1</selenide.version>

<wro4j.version>1.9.0</wro4j.version>
<spock.version>2.4-M1-groovy-4.0</spock.version>
<spock.version>2.4-M4-groovy-4.0</spock.version>
<!-- client deps -->
<webjar-jquery.version>3.5.1</webjar-jquery.version>
<webjar-bootstrap.version>4.5.0</webjar-bootstrap.version>
<webjar-popperjs.version>2.0.2</webjar-popperjs.version>


<!-- code quality -->
<archunit-junit5.version>1.2.1</archunit-junit5.version>
<archunit-junit5.version>1.3.0</archunit-junit5.version>
<sonar-maven-plugin.version>3.10.0.2594</sonar-maven-plugin.version>
<jacoco-maven-plugin.version>0.8.10</jacoco-maven-plugin.version>
<maven.checkstyle.plugin.version>3.3.0</maven.checkstyle.plugin.version>
<puppycrawl.checkstyle.version>10.13.0</puppycrawl.checkstyle.version>
<puppycrawl.checkstyle.version>10.18.1</puppycrawl.checkstyle.version>
<puppycrawl.checkstyle.config.file>src/main/resources/checkstyle.xml</puppycrawl.checkstyle.config.file>


Expand Down Expand Up @@ -231,7 +231,7 @@
<dependency> <!-- enables mocking of classes without default constructor (together with ByteBuddy or CGLIB) -->
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.3</version>
<version>3.4</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -617,24 +617,24 @@
</profile>

</profiles>
<!-- <repositories>-->
<!-- <repository>-->
<!-- <id>spring-milestones</id>-->
<!-- <name>Spring Milestones</name>-->
<!-- <url>https://repo.spring.io/milestone</url>-->
<!-- <snapshots>-->
<!-- <enabled>false</enabled>-->
<!-- </snapshots>-->
<!-- </repository>-->
<!-- </repositories>-->
<!-- <pluginRepositories>-->
<!-- <pluginRepository>-->
<!-- <id>spring-milestones</id>-->
<!-- <name>Spring Milestones</name>-->
<!-- <url>https://repo.spring.io/milestone</url>-->
<!-- <snapshots>-->
<!-- <enabled>false</enabled>-->
<!-- </snapshots>-->
<!-- </pluginRepository>-->
<!-- </pluginRepositories>-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
90 changes: 6 additions & 84 deletions src/main/java/gt/app/domain/AppUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
Expand All @@ -16,6 +19,9 @@
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "APP_USER")
@Getter
@Setter
@NoArgsConstructor
public class AppUser extends BaseEntity implements UserDetails {
@Basic(fetch = FetchType.LAZY)
@Lob
Expand Down Expand Up @@ -98,90 +104,6 @@ public boolean isEnabled() {
return active;
}

public byte[] getAvatar() {
return avatar;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public String getUniqueId() {
return uniqueId;
}

public String getActivationKey() {
return activationKey;
}

public String getResetKey() {
return resetKey;
}

public void setAvatar(byte[] avatar) {
this.avatar = avatar;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setEmail(String email) {
this.email = email;
}

public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

public void setPassword(String password) {
this.password = password;
}

public void setAuthorities(Set<Authority> authorities) {
this.authorities = authorities;
}

public void setActive(Boolean active) {
this.active = active;
}

public void setAccountNonExpired(Boolean accountNonExpired) {
this.accountNonExpired = accountNonExpired;
}

public void setAccountNonLocked(Boolean accountNonLocked) {
this.accountNonLocked = accountNonLocked;
}

public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
this.credentialsNonExpired = credentialsNonExpired;
}

public void setActivationKey(String activationKey) {
this.activationKey = activationKey;
}

public void setResetKey(String resetKey) {
this.resetKey = resetKey;
}

public AppUser() {

}

public AppUser(String uniqueId, String firstName, String lastName, String email) {
this.uniqueId = uniqueId;
this.firstName = firstName;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/gt/app/web/mvc/NoteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,8 @@ public class NoteController {

final NoteService noteService;

@GetMapping("/add")
public String startAddNote(Model model) {
model.addAttribute("msg", "Add a new note");
model.addAttribute("note", new Note());
return "note/add-note";
}

@PostMapping("/add")
public String finishAddNote(NoteCreateDto noteDto, RedirectAttributes redirectAttrs) {

//TODO:validate and return to GET:/add on errors

Note note = noteService.createNote(noteDto);
Expand All @@ -43,7 +35,6 @@ public String finishAddNote(NoteCreateDto noteDto, RedirectAttributes redirectAt
@GetMapping("/delete/{id}")
@PreAuthorize("@permEvaluator.hasAccess(#id, 'Note' )")
public String deleteNote(@PathVariable Long id, RedirectAttributes redirectAttrs) {

noteService.delete(id);

redirectAttrs.addFlashAttribute("success", "Note with id " + id + " is deleted");
Expand All @@ -63,11 +54,8 @@ public String startEditNote(Model model, @PathVariable Long id) {
@PreAuthorize("@permEvaluator.hasAccess(#noteDto.id, 'Note' )")
public String finishEditNote(Model model, NoteEditDto noteDto, RedirectAttributes redirectAttrs) {
model.addAttribute("msg", "Add a new note");


//TODO:validate and return to GET:/edit/{id} on errors


noteService.update(noteDto);

redirectAttrs.addFlashAttribute("success", "Note with id " + noteDto.id() + " is updated");
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/gt/app/web/mvc/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public String register(Model model) {
@PostMapping(value = "/signup")
public String register(@Valid @ModelAttribute UserSignUpDTO user, BindingResult bindingResult,
RedirectAttributes redirectAttrs) {

//do custom validation along with the BeanValidation
userSignupValidator.validate(user, bindingResult);

Expand All @@ -59,7 +58,6 @@ public String updateProfile(Model model, @AuthenticationPrincipal AppUserDetails
@PostMapping(value = "/profile")
public String updateProfile(@Valid @ModelAttribute UserProfileUpdateDTO user, BindingResult bindingResult,
@AuthenticationPrincipal AppUserDetails loggedInUserDtl, RedirectAttributes redirectAttrs) {

if (bindingResult.hasErrors()) {
return "user/profile";
}
Expand All @@ -80,7 +78,6 @@ public String updatePassword(Model model) {
@PostMapping(value = "/password")
public String updatePassword(@Valid @ModelAttribute("user") PasswordUpdateDTO reqDto, BindingResult bindingResult,
@AuthenticationPrincipal AppUserDetails loggedInUserDtl, RedirectAttributes redirectAttrs) {

//do custom validation along with the BeanValidation
passwordUpdateValidator.validate(reqDto, bindingResult, loggedInUserDtl);

Expand Down
7 changes: 3 additions & 4 deletions src/test/groovy/gt/app/SpringContextSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import spock.lang.Specification
@SpringBootTest
@ActiveProfiles(Constants.SPRING_PROFILE_TEST)
class SpringContextSpec extends Specification {
//this fails due to incompatibility between spring 6 and spock, comment until a fix is released
// @Autowired
// private HelloResource webController
@Autowired
private HelloResource webController

def "when context is loaded then all expected beans are created"() {
expect: "the WebController is created"
1 == 1
// webController
webController
}
}
13 changes: 12 additions & 1 deletion src/test/groovy/gt/app/modules/user/AppUserServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gt.app.modules.user
import gt.app.config.Constants
import gt.app.domain.AppUser
import gt.app.modules.email.EmailService
import gt.app.modules.email.dto.EmailDto
import gt.app.modules.user.dto.UserSignUpDTO
import org.springframework.security.crypto.password.NoOpPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
Expand Down Expand Up @@ -39,7 +40,17 @@ class AppUserServiceSpec extends Specification {
user.email == toCreate.email
1 * authorityService.findByNameIn(Constants.ROLE_USER)
1 * userRepository.save(_)
1 * emailService.sendEmail(_)
1 * emailService.sendEmail(_) >> {
//argument capture and assertions
EmailDto dto ->
assert dto.to()[0] == toCreate.email
assert dto.cc().size() == 0
assert dto.bcc().size() == 0
assert dto.from() == "system@noteapp"
assert dto.subject() == "NoteApp Account Created!"
assert dto.content() == "Thanks for signing up."
}

0 * _
}
}
19 changes: 19 additions & 0 deletions src/test/java/gt/app/ApplicationStartupTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gt.app;

import org.junit.jupiter.api.Test;

import java.net.UnknownHostException;

class ApplicationStartupTest {

@Test
void applicationCanBeStartedWithDefaultConfigByRunningMainMethod() throws UnknownHostException {
/*
this ensures that the 'Application' can be "Simply" run from IDE without doing any config change(the yml files or vm arg).
*/
Application.main(new String[]{});
}

}


4 changes: 0 additions & 4 deletions src/test/java/gt/app/modules/file/FileDownloadUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.util.MimeTypeUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -25,7 +23,6 @@ void downloadFile() throws IOException {


assertThat(resp.getHeader("Content-Disposition")).isEqualTo("attachment; filename=original.txt");
//assertThat(resp.getContentLength()).isEqualTo(toDownload.length());
assertThat(resp.getContentType()).isEqualTo(MimeTypeUtils.APPLICATION_OCTET_STREAM.getType());

assertThat(resp.getContentAsString()).contains("Some Content");
Expand All @@ -45,7 +42,6 @@ void downloadFileWithContentType() throws IOException {


assertThat(resp.getHeader("Content-Disposition")).isEqualTo("attachment; filename=original.txt");
//assertThat(resp.getContentLength()).isEqualTo(toDownload.length());
assertThat(resp.getContentType()).isEqualTo("mimetype");

assertThat(resp.getContentAsString()).contains("Some Content");
Expand Down

0 comments on commit f2fd824

Please sign in to comment.