-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from paulojribp/master
Cadastrar novos investidores no Clube de Investimentos #7
- Loading branch information
Showing
7 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/javadevzone/cotas/controllers/QuotaHolderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.javadevzone.cotas.controllers; | ||
|
||
import com.javadevzone.cotas.entity.QuotaHolder; | ||
import com.javadevzone.cotas.exceptions.RequiredAttributeException; | ||
import com.javadevzone.cotas.repository.QuotaHolderRepository; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.net.URI; | ||
import java.util.Objects; | ||
|
||
import static org.springframework.http.HttpStatus.CREATED; | ||
|
||
@RestController | ||
@RequestMapping("/quotaHolder") | ||
@AllArgsConstructor | ||
public class QuotaHolderController { | ||
|
||
private final QuotaHolderRepository quotaHolderRepository; | ||
|
||
@PostMapping | ||
@ResponseStatus(CREATED) | ||
public QuotaHolder create(@RequestBody QuotaHolder quotaHolder) { | ||
if (Objects.isNull(quotaHolder.getName())) | ||
throw new RequiredAttributeException("O nome do Investidor é obrigatório"); | ||
|
||
return quotaHolderRepository.save(quotaHolder); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/javadevzone/cotas/exceptions/RequiredAttributeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.javadevzone.cotas.exceptions; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public class RequiredAttributeException extends RuntimeException { | ||
|
||
public RequiredAttributeException(String mensagem) { | ||
super(mensagem); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/javadevzone/cotas/repository/QuotaHolderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.javadevzone.cotas.repository; | ||
|
||
import com.javadevzone.cotas.entity.QuotaHolder; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface QuotaHolderRepository extends CrudRepository<QuotaHolder, Long> { | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/com/javadevzone/cotas/controllers/QuotaHolderControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.javadevzone.cotas.controllers; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.javadevzone.cotas.CotasApplication; | ||
import com.javadevzone.cotas.entity.QuotaHolder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON; | ||
import static org.springframework.test.web.servlet.ResultMatcher.matchAll; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(classes = CotasApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) | ||
@AutoConfigureMockMvc | ||
public class QuotaHolderControllerTest { | ||
|
||
@Autowired | ||
private ObjectMapper mapper; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
public void should_create_a_new_quota_holder() throws Exception { | ||
QuotaHolder mrs_lovely = QuotaHolder.builder().name("Mrs Lovely").build(); | ||
|
||
MvcResult mvcResult = mockMvc.perform(post("/quotaHolder") | ||
.contentType(APPLICATION_JSON) | ||
.accept(APPLICATION_JSON) | ||
.content(mapper.writeValueAsString(mrs_lovely))) | ||
.andExpect(matchAll(status().isCreated())) | ||
.andReturn(); | ||
|
||
QuotaHolder investidor = mapper.readValue(mvcResult.getResponse().getContentAsString(), QuotaHolder.class); | ||
|
||
assertThat(investidor.getId()) | ||
.isNotNull(); | ||
|
||
} | ||
|
||
@Test | ||
public void should_throw_exception_when_creating_new_quota_holder_without_name() throws Exception { | ||
mockMvc.perform(post("/quotaHolder") | ||
.contentType(APPLICATION_JSON) | ||
.accept(APPLICATION_JSON) | ||
.content(mapper.writeValueAsString(new QuotaHolder()))) | ||
.andExpect(matchAll(status().is4xxClientError())) | ||
.andReturn(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters