diff --git a/pom.xml b/pom.xml index 1095d17..d083bff 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.wiemanboy WiemanApi - 0.1.1 + 1.0.0 WiemanApi WiemanApi diff --git a/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java b/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java index 7e12acc..847c621 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java +++ b/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java @@ -1,11 +1,17 @@ package com.wiemanboy.wiemanapi.application; import com.wiemanboy.wiemanapi.data.ProfileRepository; -import com.wiemanboy.wiemanapi.domain.Profile; +import com.wiemanboy.wiemanapi.domain.*; import com.wiemanboy.wiemanapi.domain.exceptions.ProfileNotFoundException; +import com.wiemanboy.wiemanapi.presentation.dto.response.DescriptionDto; +import com.wiemanboy.wiemanapi.presentation.dto.response.SkillSectionDto; +import com.wiemanboy.wiemanapi.presentation.dto.response.SocialDto; import jakarta.transaction.Transactional; import org.springframework.stereotype.Service; +import java.net.URI; +import java.util.List; + @Service @Transactional public class ProfileService { @@ -24,12 +30,42 @@ public Profile getProfileByName(String name) { return profileRepository.findByFullNameOrUsername(name).orElseThrow(() -> new ProfileNotFoundException(null, name)); } - public Profile createProfile(String firstName, String lastName, String username) { - Profile profile = new Profile(firstName, lastName, username); + public Profile createProfile(String firstName, String lastName, String username, List descriptionDtos, List socialDtos, List skillSectionDtos) { + Profile profile = new Profile( + firstName, + lastName, + username, + descriptionDtos.stream().map( + descriptionDto -> Description.builder() + .title(descriptionDto.title()) + .locale(descriptionDto.locale()) + .content(descriptionDto.content()) + .build() + ).toList(), + socialDtos.stream().map( + socialDto -> Social.builder() + .platform(socialDto.platform()) + .url(URI.create(socialDto.url())) + .build() + ).toList(), + skillSectionDtos.stream().map( + skillSectionDto -> SkillSection.builder() + .title(skillSectionDto.title()) + .skills(skillSectionDto.skills().stream() + .map( + skillDto -> Skill.builder() + .name(skillDto.name()) + .level(skillDto.level()) + .build() + ).toList() + ).build( + ) + ).toList() + ); return profileRepository.save(profile); } - public Profile patchProfile(String id, String firstName, String lastName, String username) { + public Profile updateProfile(String id, String firstName, String lastName, String username, List descriptionDtos, List socialDtos, List skillSectionDtos) { Profile profile = profileRepository.findById(id).orElseThrow(() -> new ProfileNotFoundException(id, null)); if (firstName != null) { @@ -41,6 +77,44 @@ public Profile patchProfile(String id, String firstName, String lastName, String if (username != null) { profile.setUsername(username); } + if (descriptionDtos != null) { + profile.setDescriptions( + descriptionDtos.stream().map( + descriptionDto -> Description.builder() + .title(descriptionDto.title()) + .locale(descriptionDto.locale()) + .content(descriptionDto.content()) + .build() + ).toList() + ); + } + if (socialDtos != null) { + profile.setSocials( + socialDtos.stream().map( + socialDto -> Social.builder() + .platform(socialDto.platform()) + .url(URI.create(socialDto.url())) + .build() + ).toList() + ); + } + if (skillSectionDtos != null) { + profile.setSkillSections( + skillSectionDtos.stream().map( + skillSectionDto -> SkillSection.builder() + .title(skillSectionDto.title()) + .skills(skillSectionDto.skills().stream() + .map( + skillDto -> Skill.builder() + .name(skillDto.name()) + .level(skillDto.level()) + .build() + ).toList() + ).build( + ) + ).toList() + ); + } return profileRepository.save(profile); } diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/Description.java b/src/main/java/com/wiemanboy/wiemanapi/domain/Description.java index d1027d6..a462f4f 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/domain/Description.java +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/Description.java @@ -1,10 +1,12 @@ package com.wiemanboy.wiemanapi.domain; +import lombok.Builder; import lombok.Getter; import lombok.Setter; @Setter @Getter +@Builder public class Description { private String locale; private String title; diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/Profile.java b/src/main/java/com/wiemanboy/wiemanapi/domain/Profile.java index f72619e..3859d5b 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/domain/Profile.java +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/Profile.java @@ -1,5 +1,6 @@ package com.wiemanboy.wiemanapi.domain; +import lombok.Builder; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -11,28 +12,34 @@ @Getter @EqualsAndHashCode @Document +@Setter +@Builder public class Profile { private String id; - private List descriptions = new ArrayList<>(); - private List socials = new ArrayList<>(); - private List skillSections = new ArrayList<>(); - @Setter private String firstName; - @Setter private String lastName; - @Setter private String username; + @Builder.Default + private List descriptions = new ArrayList<>(); + @Builder.Default + private List socials = new ArrayList<>(); + @Builder.Default + private List skillSections = new ArrayList<>(); + + protected Profile() { + } - public Profile(String firstName, String lastName, String username) { + protected Profile(String id, String firstName, String lastName, String username, List descriptions, List socials, List skillSections) { + this.id = id; this.firstName = firstName; this.lastName = lastName; this.username = username; + this.descriptions = descriptions; + this.socials = socials; + this.skillSections = skillSections; } - protected Profile() { - } - - protected Profile(List descriptions, List socials, List skillSections, String firstName, String lastName, String username) { + public Profile(String firstName, String lastName, String username, List descriptions, List socials, List skillSections) { this.descriptions = descriptions; this.socials = socials; this.skillSections = skillSections; @@ -41,10 +48,6 @@ protected Profile(List descriptions, List socials, List description.getLocale().equals(locale)) @@ -52,29 +55,9 @@ public Description getDescription(String locale) { .orElse(null); } - public void removeDescription(Description description) { - this.descriptions.remove(description); - } - - public void addSocial(Social social) { - socials.add(social); - } - - public void removeSocial(Social social) { - socials.remove(social); - } - - public void addSkillSection(SkillSection skillSection) { - skillSections.add(skillSection); - } - public List getSkillSections(String locale) { return skillSections.stream() .filter(skillSection -> skillSection.getLocale().equals(locale)) .toList(); } - - public void removeSkillSection(SkillSection skillSection) { - skillSections.remove(skillSection); - } } diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java b/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java index c8944ba..eb66c73 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java @@ -1,10 +1,12 @@ package com.wiemanboy.wiemanapi.domain; +import lombok.Builder; import lombok.Getter; import lombok.Setter; @Getter @Setter +@Builder public class Skill { private String name; private String level; diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/SkillSection.java b/src/main/java/com/wiemanboy/wiemanapi/domain/SkillSection.java index 5ab0061..9aaafcb 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/domain/SkillSection.java +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/SkillSection.java @@ -1,29 +1,24 @@ package com.wiemanboy.wiemanapi.domain; +import lombok.Builder; import lombok.Getter; import lombok.Setter; import java.util.ArrayList; import java.util.List; +@Setter @Getter +@Builder public class SkillSection { - @Setter private String locale; - @Setter private String title; + @Builder.Default List skills = new ArrayList<>(); - public SkillSection(String locale, String title) { + public SkillSection(String locale, String title, List skills) { this.locale = locale; this.title = title; - } - - public void addSkill(Skill skill) { - this.skills.add(skill); - } - - public void removeSkill(Skill skill) { - this.skills.remove(skill); + this.skills = skills; } } diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/Social.java b/src/main/java/com/wiemanboy/wiemanapi/domain/Social.java index 1c61c75..bc30662 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/domain/Social.java +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/Social.java @@ -1,5 +1,6 @@ package com.wiemanboy.wiemanapi.domain; +import lombok.Builder; import lombok.Getter; import lombok.Setter; @@ -7,6 +8,7 @@ @Getter @Setter +@Builder public class Social { private String username; private String platform; diff --git a/src/main/java/com/wiemanboy/wiemanapi/presentation/ProfileController.java b/src/main/java/com/wiemanboy/wiemanapi/presentation/ProfileController.java index b120fdb..c55116c 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/presentation/ProfileController.java +++ b/src/main/java/com/wiemanboy/wiemanapi/presentation/ProfileController.java @@ -1,12 +1,11 @@ package com.wiemanboy.wiemanapi.presentation; import com.wiemanboy.wiemanapi.application.ProfileService; +import com.wiemanboy.wiemanapi.presentation.dto.request.CreateProfileDto; import com.wiemanboy.wiemanapi.presentation.dto.response.ProfileDto; import com.wiemanboy.wiemanapi.presentation.dto.response.ProfileLocaleDto; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/profiles") @@ -27,4 +26,30 @@ public ProfileDto getProfile(@PathVariable String id) { public ProfileLocaleDto getProfileByName(@PathVariable String name, @PathVariable String locale) { return ProfileLocaleDto.from(profileService.getProfileByName(name), locale); } + + @PostMapping("/") + @ResponseStatus(HttpStatus.CREATED) + public ProfileDto createProfile(@RequestBody CreateProfileDto createProfileDto) { + return ProfileDto.from(profileService.createProfile( + createProfileDto.firstName(), + createProfileDto.lastName(), + createProfileDto.username(), + createProfileDto.descriptions(), + createProfileDto.socials(), + createProfileDto.skillSections() + )); + } + + @PutMapping("/{id}") + public ProfileDto updateProfile(@PathVariable String id, @RequestBody CreateProfileDto createProfileDto) { + return ProfileDto.from(profileService.updateProfile( + id, + createProfileDto.firstName(), + createProfileDto.lastName(), + createProfileDto.username(), + createProfileDto.descriptions(), + createProfileDto.socials(), + createProfileDto.skillSections() + )); + } } diff --git a/src/main/java/com/wiemanboy/wiemanapi/presentation/dto/request/CreateProfileDto.java b/src/main/java/com/wiemanboy/wiemanapi/presentation/dto/request/CreateProfileDto.java new file mode 100644 index 0000000..b309f44 --- /dev/null +++ b/src/main/java/com/wiemanboy/wiemanapi/presentation/dto/request/CreateProfileDto.java @@ -0,0 +1,18 @@ +package com.wiemanboy.wiemanapi.presentation.dto.request; + +import com.wiemanboy.wiemanapi.presentation.dto.response.DescriptionDto; +import com.wiemanboy.wiemanapi.presentation.dto.response.SkillSectionDto; +import com.wiemanboy.wiemanapi.presentation.dto.response.SocialDto; + +import java.util.List; + +public record CreateProfileDto( + String id, + String firstName, + String lastName, + String username, + List descriptions, + List skillSections, + List socials +) { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 21c1515..e4acad4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,8 @@ debug=true spring.application.name=WiemanApi management.endpoints.web.exposure.include=health management.endpoints.web.base-path=/services/profiles/actuator/ -springdoc.swagger-ui.path=/services/profiles/docs/ +springdoc.api-docs.path=/services/profiles/api-docs +springdoc.swagger-ui.path=/services/profiles/docs springdoc.show-actuator=true spring.data.mongodb.host=${MONGO_HOST:localhost} spring.data.mongodb.port=${MONGO_PORT:27017} diff --git a/src/test/java/com/wiemanboy/wiemanapi/application/ProfileServiceTest.java b/src/test/java/com/wiemanboy/wiemanapi/application/ProfileServiceTest.java index 0250131..3ed60fa 100644 --- a/src/test/java/com/wiemanboy/wiemanapi/application/ProfileServiceTest.java +++ b/src/test/java/com/wiemanboy/wiemanapi/application/ProfileServiceTest.java @@ -1,11 +1,22 @@ package com.wiemanboy.wiemanapi.application; +import com.wiemanboy.wiemanapi.builders.DescriptionBuilder; +import com.wiemanboy.wiemanapi.builders.ProfileBuilder; +import com.wiemanboy.wiemanapi.builders.SkillSectionBuilder; +import com.wiemanboy.wiemanapi.builders.SocialBuilder; import com.wiemanboy.wiemanapi.data.ProfileRepository; +import com.wiemanboy.wiemanapi.domain.Description; import com.wiemanboy.wiemanapi.domain.Profile; +import com.wiemanboy.wiemanapi.domain.SkillSection; +import com.wiemanboy.wiemanapi.domain.Social; +import com.wiemanboy.wiemanapi.presentation.dto.response.DescriptionDto; +import com.wiemanboy.wiemanapi.presentation.dto.response.SkillSectionDto; +import com.wiemanboy.wiemanapi.presentation.dto.response.SocialDto; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import java.util.List; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -25,7 +36,7 @@ void setUp() { @Test void testGetProfile() { - Profile profile = new Profile("John", "Doe", "johndoe"); + Profile profile = new ProfileBuilder().build(); when(profileRepository.findById("")).thenReturn(Optional.of(profile)); Profile result = profileService.getProfile(""); @@ -35,7 +46,7 @@ void testGetProfile() { @Test void testGetProfileByName() { String name = "johndoe"; - Profile profile = new Profile("John", "Doe", "johndoe"); + Profile profile = new ProfileBuilder().setUsername(name).build(); when(profileRepository.findByFullNameOrUsername(name)).thenReturn(Optional.of(profile)); Profile result = profileService.getProfileByName(name); @@ -44,10 +55,20 @@ void testGetProfileByName() { @Test void testCreateProfile() { - Profile profile = new Profile("John", "Doe", "johndoe"); + Description description = new DescriptionBuilder().build(); + SkillSection skillSection = new SkillSectionBuilder().build(); + Social social = new SocialBuilder().build(); + Profile profile = Profile.builder() + .firstName("John") + .lastName("Doe") + .username("johndoe") + .descriptions(List.of(description)) + .socials(List.of(social)) + .skillSections(List.of(skillSection)) + .build(); when(profileRepository.save(any(Profile.class))).thenReturn(profile); - Profile result = profileService.createProfile("John", "Doe", "johndoe"); + Profile result = profileService.createProfile(profile.getFirstName(), profile.getLastName(), profile.getUsername(), List.of(DescriptionDto.from(description)), List.of(SocialDto.from(social)), List.of(SkillSectionDto.from(skillSection))); assertEquals(profile, result); } @@ -67,14 +88,14 @@ void testGetProfileByNameNotFound() { } @Test - void testPatchProfile() { - Profile profile = new Profile("John", "Doe", "johndoe"); + void testUpdateProfile() { + Profile profile = new ProfileBuilder().build(); when(profileRepository.findById("")).thenReturn(Optional.of(profile)); when(profileRepository.save(any(Profile.class))).thenReturn(profile); - Profile result = profileService.patchProfile("", "Jane", null, null); + Profile result = profileService.updateProfile("", "Jane", null, null, null, null, null); assertEquals("Jane", result.getFirstName()); - assertEquals("Doe", result.getLastName()); - assertEquals("johndoe", result.getUsername()); + assertEquals(profile.getLastName(), result.getLastName()); + assertEquals(profile.getUsername(), result.getUsername()); } } \ No newline at end of file diff --git a/src/test/java/com/wiemanboy/wiemanapi/builders/ProfileBuilder.java b/src/test/java/com/wiemanboy/wiemanapi/builders/ProfileBuilder.java index 4a356fc..6e8a86f 100644 --- a/src/test/java/com/wiemanboy/wiemanapi/builders/ProfileBuilder.java +++ b/src/test/java/com/wiemanboy/wiemanapi/builders/ProfileBuilder.java @@ -4,6 +4,8 @@ import lombok.Setter; import lombok.experimental.Accessors; +import java.util.List; + @Setter @Accessors(chain = true) public class ProfileBuilder { @@ -12,6 +14,6 @@ public class ProfileBuilder { private String username = "johndoe"; public Profile build() { - return new Profile(firstName, lastName, username); + return new Profile(firstName, lastName, username, List.of(), List.of(), List.of()); } } diff --git a/src/test/java/com/wiemanboy/wiemanapi/builders/SkillSectionBuilder.java b/src/test/java/com/wiemanboy/wiemanapi/builders/SkillSectionBuilder.java index f4565d2..a4354d4 100644 --- a/src/test/java/com/wiemanboy/wiemanapi/builders/SkillSectionBuilder.java +++ b/src/test/java/com/wiemanboy/wiemanapi/builders/SkillSectionBuilder.java @@ -4,6 +4,8 @@ import lombok.Setter; import lombok.experimental.Accessors; +import java.util.List; + @Setter @Accessors(chain = true) public class SkillSectionBuilder { @@ -11,6 +13,6 @@ public class SkillSectionBuilder { private String title = "Title"; public SkillSection build() { - return new SkillSection(locale, title); + return new SkillSection(locale, title, List.of()); } } diff --git a/src/test/java/com/wiemanboy/wiemanapi/config/TestSecurityConfig.java b/src/test/java/com/wiemanboy/wiemanapi/config/TestSecurityConfig.java index f043188..ae03b9f 100644 --- a/src/test/java/com/wiemanboy/wiemanapi/config/TestSecurityConfig.java +++ b/src/test/java/com/wiemanboy/wiemanapi/config/TestSecurityConfig.java @@ -3,27 +3,17 @@ import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; -import org.springframework.security.oauth2.jwt.JwtDecoder; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.web.SecurityFilterChain; -import static org.mockito.Mockito.mock; - @TestConfiguration public class TestSecurityConfig { - - @Bean - public JwtDecoder jwtDecoder() { - return mock(JwtDecoder.class); // Mock JwtDecoder - } - @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { - return http.authorizeHttpRequests(authorize -> authorize.anyRequest().permitAll()).build(); - } - - @Bean - public ClientRegistrationRepository clientRegistrationRepository() { - return mock(ClientRegistrationRepository.class); // Mock ClientRegistrationRepository + return http.authorizeHttpRequests( + authorize -> authorize.anyRequest().permitAll() + ) + .csrf(AbstractHttpConfigurer::disable) + .build(); } } diff --git a/src/test/java/com/wiemanboy/wiemanapi/domain/ProfileTest.java b/src/test/java/com/wiemanboy/wiemanapi/domain/ProfileTest.java index dc36988..1e69011 100644 --- a/src/test/java/com/wiemanboy/wiemanapi/domain/ProfileTest.java +++ b/src/test/java/com/wiemanboy/wiemanapi/domain/ProfileTest.java @@ -3,81 +3,33 @@ import com.wiemanboy.wiemanapi.builders.DescriptionBuilder; import com.wiemanboy.wiemanapi.builders.ProfileBuilder; import com.wiemanboy.wiemanapi.builders.SkillSectionBuilder; -import com.wiemanboy.wiemanapi.builders.SocialBuilder; import org.junit.jupiter.api.Test; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; class ProfileTest { - @Test - void testAddDescription() { - Profile profile = (new ProfileBuilder()).build(); - Description description = (new DescriptionBuilder()).build(); - profile.addDescription(description); - assertEquals(1, profile.getDescriptions().size()); - } - @Test void testGetDescription() { - Profile profile = (new ProfileBuilder()).build(); - Description description = (new DescriptionBuilder()).setLocale("en").build(); - profile.addDescription(description); - profile.addDescription((new DescriptionBuilder()).setLocale("nl").build()); - + Profile profile = new ProfileBuilder().build(); + Description description = new DescriptionBuilder().setLocale("en").build(); + profile.setDescriptions(List.of( + description, + new DescriptionBuilder().setLocale("nl").build() + )); assertEquals(description, profile.getDescription("en")); } - @Test - void testRemoveDescription() { - Profile profile = (new ProfileBuilder()).build(); - Description description = (new DescriptionBuilder()).build(); - profile.addDescription(description); - profile.removeDescription(description); - assertEquals(0, profile.getDescriptions().size()); - } - - @Test - void testAddSocial() { - Profile profile = (new ProfileBuilder()).build(); - Social social = (new SocialBuilder()).build(); - profile.addSocial(social); - assertEquals(1, profile.getSocials().size()); - } - - @Test - void testRemoveSocial() { - Profile profile = (new ProfileBuilder()).build(); - Social social = (new SocialBuilder()).build(); - profile.addSocial(social); - profile.removeSocial(social); - assertEquals(0, profile.getSocials().size()); - } - - @Test - void testAddSkillSection() { - Profile profile = (new ProfileBuilder()).build(); - SkillSection skillSection = (new SkillSectionBuilder()).build(); - profile.addSkillSection(skillSection); - assertEquals(1, profile.getSkillSections().size()); - } - @Test void testGetSkillSections() { Profile profile = (new ProfileBuilder()).build(); SkillSection skillSection = (new SkillSectionBuilder()).setLocale("en").build(); - profile.addSkillSection(skillSection); - profile.addSkillSection((new SkillSectionBuilder()).setLocale("nl").build()); - + profile.setSkillSections(List.of( + skillSection, + new SkillSectionBuilder().setLocale("nl").build() + )); assertEquals(skillSection, profile.getSkillSections("en").getFirst()); } - - @Test - void testRemoveSkillSection() { - Profile profile = (new ProfileBuilder()).build(); - SkillSection skillSection = (new SkillSectionBuilder()).build(); - profile.addSkillSection(skillSection); - profile.removeSkillSection(skillSection); - assertEquals(0, profile.getSkillSections().size()); - } } \ No newline at end of file diff --git a/src/test/java/com/wiemanboy/wiemanapi/domain/SkillSectionTest.java b/src/test/java/com/wiemanboy/wiemanapi/domain/SkillSectionTest.java deleted file mode 100644 index 2a6aff0..0000000 --- a/src/test/java/com/wiemanboy/wiemanapi/domain/SkillSectionTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.wiemanboy.wiemanapi.domain; - -import com.wiemanboy.wiemanapi.builders.SkillBuilder; -import com.wiemanboy.wiemanapi.builders.SkillSectionBuilder; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class SkillSectionTest { - - @Test - void testAddSkill() { - SkillSection skillSection = (new SkillSectionBuilder()).build(); - Skill skill = (new SkillBuilder()).build(); - skillSection.addSkill(skill); - assertEquals(1, skillSection.getSkills().size()); - } - - @Test - void testRemoveSkill() { - SkillSection skillSection = (new SkillSectionBuilder()).build(); - Skill skill = (new SkillBuilder()).build(); - skillSection.addSkill(skill); - skillSection.removeSkill(skill); - assertEquals(0, skillSection.getSkills().size()); - } - -} \ No newline at end of file diff --git a/src/test/java/com/wiemanboy/wiemanapi/presentation/ProfileControllerTest.java b/src/test/java/com/wiemanboy/wiemanapi/presentation/ProfileControllerTest.java index 610a279..6eccb4c 100644 --- a/src/test/java/com/wiemanboy/wiemanapi/presentation/ProfileControllerTest.java +++ b/src/test/java/com/wiemanboy/wiemanapi/presentation/ProfileControllerTest.java @@ -11,11 +11,12 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import java.util.Optional; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest(classes = {TestSecurityConfig.class, WiemanApiApplication.class}) @@ -64,4 +65,41 @@ void testGetProfileByNameNotFound() throws Exception { mockMvc.perform(get("/api/profiles/{name}/{locale}", "johndoe", "en")) .andExpect(status().isNotFound()); } + + @Test + void testCreateProfile() throws Exception { + Mockito.when(profileRepository.save(Mockito.any())).thenReturn((new ProfileBuilder()).build()); + + mockMvc.perform(post("/api/profiles/") + .contentType(MediaType.APPLICATION_JSON) + .content(""" + { + "firstName": "John", + "lastName": "Doe", + "username": "johndoe", + "descriptions": [], + "socials": [], + "skillSections": [] + }""")) + .andExpect(status().isCreated()); + } + + @Test + void testUpdateProfile() throws Exception { + Mockito.when(profileRepository.findById("id")).thenReturn(Optional.of((new ProfileBuilder()).build())); + Mockito.when(profileRepository.save(Mockito.any())).thenReturn((new ProfileBuilder()).build()); + + mockMvc.perform(put("/api/profiles/" + "id") + .contentType(MediaType.APPLICATION_JSON) + .content(""" + { + "firstName": "John", + "lastName": "Doe", + "username": "johndoe", + "descriptions": [], + "socials": [], + "skillSections": [] + }""")) + .andExpect(status().isOk()); + } } \ No newline at end of file