Skip to content

Commit

Permalink
Merge pull request #11 from paulojribp/master
Browse files Browse the repository at this point in the history
Cadastrar novos investidores no Clube de Investimentos #7
  • Loading branch information
paulojribp authored Feb 29, 2020
2 parents ea188ba + e4cc73f commit 31d352e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/javadevzone/cotas/CotasApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class CotasApplication {
public class CotasApplication implements WebMvcConfigurer {

public static void main(String[] args) {
SpringApplication.run(CotasApplication.class, args);
}


@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}

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

}
6 changes: 6 additions & 0 deletions src/main/java/com/javadevzone/cotas/entity/QuotaHolder.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.javadevzone.cotas.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDate;

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class QuotaHolder {

@Id
Expand Down
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);
}
}
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> {

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

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.test.web.servlet.ResultMatcher.matchAll;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down

0 comments on commit 31d352e

Please sign in to comment.