├── project ├── .gitattributes ├── src │ ├── main │ │ ├── java │ │ │ └── br │ │ │ │ └── com │ │ │ │ └── nubank │ │ │ │ ├── dto │ │ │ │ ├── Clientes │ │ │ │ │ ├── ClientesRecordDTO.java │ │ │ │ │ ├── ClientesDTO.java │ │ │ │ │ └── ClientesResponseDTO.java │ │ │ │ └── Contatos │ │ │ │ │ ├── ContatosChildDTO.java │ │ │ │ │ ├── ContatosDTO.java │ │ │ │ │ ├── ContatosChildResponseDTO.java │ │ │ │ │ └── ContatosResponseDTO.java │ │ │ │ ├── repository │ │ │ │ ├── ClientesRepository.java │ │ │ │ └── ContatosRepository.java │ │ │ │ ├── Startup.java │ │ │ │ ├── mapper │ │ │ │ ├── ClientesMapper.java │ │ │ │ └── ContatosMapper.java │ │ │ │ ├── service │ │ │ │ ├── ContatosService.java │ │ │ │ └── ClientesService.java │ │ │ │ ├── model │ │ │ │ ├── Contatos.java │ │ │ │ └── Clientes.java │ │ │ │ └── controller │ │ │ │ ├── ContatosController.java │ │ │ │ └── ClientesController.java │ │ └── resources │ │ │ └── application.properties │ └── test │ │ ├── java │ │ └── br │ │ │ └── com │ │ │ └── nubank │ │ │ ├── StartupTests.java │ │ │ ├── service │ │ │ ├── ContatosServiceTest.java │ │ │ └── ClientesServiceTest.java │ │ │ └── controller │ │ │ ├── ContatosControllerTest.java │ │ │ └── ClientesControllerTest.java │ │ └── resources │ │ └── application.properties ├── .gitignore ├── .mvn │ └── wrapper │ │ └── maven-wrapper.properties ├── pom.xml ├── mvnw.cmd └── mvnw ├── assets └── imgs │ └── foto-de-capa.png ├── .gitignore ├── docker └── docker-compose.yml └── README.md /project/.gitattributes: -------------------------------------------------------------------------------- 1 | /mvnw text eol=lf 2 | *.cmd text eol=crlf 3 | -------------------------------------------------------------------------------- /assets/imgs/foto-de-capa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Maykon-JDS/desafio-backend-nubank/HEAD/assets/imgs/foto-de-capa.png -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/dto/Clientes/ClientesRecordDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.dto.Clientes; 2 | 3 | import br.com.nubank.dto.Contatos.ContatosChildDTO; 4 | 5 | import java.util.List; 6 | 7 | public record ClientesRecordDTO(String nome, List contatos) {} 8 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/repository/ClientesRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.repository; 2 | 3 | import br.com.nubank.model.Clientes; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface ClientesRepository extends JpaRepository { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /project/src/test/java/br/com/nubank/StartupTests.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class StartupTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/repository/ContatosRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.repository; 2 | 3 | import br.com.nubank.model.Contatos; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface ContatosRepository extends JpaRepository { 7 | 8 | 9 | } 10 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/dto/Contatos/ContatosChildDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.dto.Contatos; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | @Getter 11 | @Setter 12 | public class ContatosChildDTO { 13 | 14 | private String tipo; 15 | 16 | private String conteudo; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | replay_pid* 25 | 26 | # docker volumes 27 | docker/postgres 28 | docker/mysql -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/Startup.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.data.jpa.repository.config.EnableJpaAuditing; 6 | 7 | @EnableJpaAuditing 8 | @SpringBootApplication 9 | public class Startup { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Startup.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /project/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=Desafio Nubank 2 | spring.datasource.url=jdbc:postgresql://localhost:5432/nubank 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.datasource.driver-class-name=org.postgresql.Driver 7 | 8 | spring.jpa.hibernate.ddl-auto=update 9 | spring.jpa.show-sql=true 10 | spring.jpa.properties.hibernate.format_sql=true 11 | spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect 12 | 13 | server.port=8001 -------------------------------------------------------------------------------- /project/src/test/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=Desafio Nubank 2 | spring.datasource.url=jdbc:postgresql://localhost:5432/nubank 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.datasource.driver-class-name=org.postgresql.Driver 7 | 8 | spring.jpa.hibernate.ddl-auto=update 9 | spring.jpa.show-sql=true 10 | spring.jpa.properties.hibernate.format_sql=true 11 | spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect 12 | 13 | server.port=8001 -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/dto/Clientes/ClientesDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.dto.Clientes; 2 | 3 | import br.com.nubank.dto.Contatos.ContatosChildDTO; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | import java.util.List; 10 | 11 | @AllArgsConstructor 12 | @NoArgsConstructor 13 | @Getter 14 | @Setter 15 | public class ClientesDTO { 16 | 17 | private String nome; 18 | private List contatos; 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /project/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | .mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/dto/Clientes/ClientesResponseDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.dto.Clientes; 2 | 3 | import br.com.nubank.dto.Contatos.ContatosChildResponseDTO; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | import java.util.List; 10 | 11 | @AllArgsConstructor 12 | @NoArgsConstructor 13 | @Getter 14 | @Setter 15 | public class ClientesResponseDTO { 16 | 17 | private Long id; 18 | private String nome; 19 | private List contatos; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/dto/Contatos/ContatosDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.dto.Contatos; 2 | 3 | import jakarta.validation.constraints.NotBlank; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | // TODO: IMPLEMENTAR COM RECORD 10 | @AllArgsConstructor 11 | @NoArgsConstructor 12 | @Getter 13 | @Setter 14 | public class ContatosDTO { 15 | 16 | @NotBlank(message = "Id do Cliente é Obrigatório!") 17 | private Long clienteId; 18 | 19 | private String tipo; 20 | 21 | private String conteudo; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/dto/Contatos/ContatosChildResponseDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.dto.Contatos; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | @NoArgsConstructor 11 | @AllArgsConstructor 12 | @Getter 13 | @Setter 14 | public class ContatosChildResponseDTO { 15 | 16 | private Long id; 17 | 18 | private String tipo; 19 | 20 | private String conteudo; 21 | 22 | private Boolean situacao; 23 | 24 | private LocalDateTime createdAt; 25 | 26 | private LocalDateTime updatedAt; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | name: desafio-nubank 2 | services: 3 | postgres: 4 | image: postgres 5 | restart: always 6 | # set shared memory limit when using docker compose 7 | shm_size: 128mb 8 | # or set shared memory limit when deploy via swarm stack 9 | volumes: 10 | - ./postgres:/var/lib/postgresql/data 11 | environment: 12 | POSTGRES_USER: root 13 | POSTGRES_PASSWORD: root 14 | ports: 15 | - "5432:5432" 16 | networks: 17 | - nubank 18 | adminer: 19 | image: adminer 20 | restart: always 21 | ports: 22 | - 8080:8080 23 | networks: 24 | - nubank 25 | networks: 26 | nubank: 27 | driver: bridge -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/dto/Contatos/ContatosResponseDTO.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.dto.Contatos; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | @NoArgsConstructor 11 | @AllArgsConstructor 12 | @Getter 13 | @Setter 14 | public class ContatosResponseDTO { 15 | 16 | private Long id; 17 | 18 | private String tipo; 19 | 20 | private String conteudo; 21 | 22 | private Boolean situacao; 23 | 24 | private LocalDateTime createdAt; 25 | 26 | private LocalDateTime updatedAt; 27 | 28 | private Long clienteId; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/mapper/ClientesMapper.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.mapper; 2 | 3 | import br.com.nubank.dto.Clientes.ClientesResponseDTO; 4 | import br.com.nubank.dto.Contatos.ContatosChildResponseDTO; 5 | import br.com.nubank.model.Clientes; 6 | 7 | import java.util.List; 8 | 9 | public class ClientesMapper { 10 | 11 | public static ClientesResponseDTO toResponseDTO (Clientes cliente) { 12 | ClientesResponseDTO clienteDTO = new ClientesResponseDTO(); 13 | clienteDTO.setId(cliente.getId()); 14 | clienteDTO.setNome(cliente.getNome()); 15 | 16 | if (cliente.getContatos() != null && !cliente.getContatos().isEmpty()) { 17 | List contatosDTO = cliente.getContatos().stream().map(ContatosMapper::toChildResponseDTO).toList(); 18 | clienteDTO.setContatos(contatosDTO); 19 | } 20 | 21 | return clienteDTO; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /project/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | wrapperVersion=3.3.2 18 | distributionType=only-script 19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.10/apache-maven-3.9.10-bin.zip 20 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/service/ContatosService.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.service; 2 | 3 | 4 | import br.com.nubank.dto.Contatos.ContatosDTO; 5 | import br.com.nubank.dto.Contatos.ContatosResponseDTO; 6 | import br.com.nubank.mapper.ContatosMapper; 7 | import br.com.nubank.model.Clientes; 8 | import br.com.nubank.model.Contatos; 9 | import br.com.nubank.repository.ContatosRepository; 10 | import lombok.RequiredArgsConstructor; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.util.List; 14 | 15 | @Service 16 | @RequiredArgsConstructor 17 | public class ContatosService { 18 | 19 | private final ClientesService clientesService; 20 | private final ContatosRepository contatosRepository; 21 | 22 | public Contatos salvarContato(ContatosDTO dto) { 23 | 24 | Clientes cliente = clientesService.findById(dto.getClienteId()); 25 | 26 | if (cliente == null) { 27 | throw new RuntimeException("Cliente não encontrado"); 28 | } 29 | 30 | Contatos contato = new Contatos(); 31 | contato.setClientes(cliente); 32 | contato.setTipo(dto.getTipo()); 33 | contato.setConteudo(dto.getConteudo()); 34 | 35 | return contatosRepository.save(contato); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/model/Contatos.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonBackReference; 4 | import jakarta.persistence.*; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Getter; 7 | import lombok.NoArgsConstructor; 8 | import lombok.Setter; 9 | import org.springframework.data.annotation.CreatedDate; 10 | import org.springframework.data.annotation.LastModifiedDate; 11 | import org.springframework.data.jpa.domain.support.AuditingEntityListener; 12 | 13 | import java.sql.Timestamp; 14 | import java.time.LocalDateTime; 15 | 16 | @Entity 17 | @Getter 18 | @Setter 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | @EntityListeners(AuditingEntityListener.class) 22 | public class Contatos { 23 | 24 | @Id 25 | @GeneratedValue(strategy = GenerationType.IDENTITY) 26 | private Long id; 27 | 28 | @Column(nullable = false) 29 | private String tipo; 30 | 31 | @Column(nullable = false) 32 | private String conteudo; 33 | 34 | @Column(nullable = false) 35 | private Boolean situacao = Boolean.TRUE; 36 | 37 | @CreatedDate 38 | @Column(name = "created_at", updatable = false) 39 | private LocalDateTime createdAt; 40 | 41 | @LastModifiedDate 42 | @Column(name = "updated_at") 43 | private LocalDateTime updatedAt; 44 | 45 | @ManyToOne 46 | @JoinColumn(name = "cliente_id") 47 | @JsonBackReference 48 | private Clientes clientes; 49 | } 50 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/model/Clientes.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonBackReference; 4 | import com.fasterxml.jackson.annotation.JsonManagedReference; 5 | import jakarta.persistence.*; 6 | import lombok.AllArgsConstructor; 7 | import lombok.Getter; 8 | import lombok.NoArgsConstructor; 9 | import lombok.Setter; 10 | import org.springframework.data.annotation.CreatedDate; 11 | import org.springframework.data.annotation.LastModifiedDate; 12 | import org.springframework.data.jpa.domain.support.AuditingEntityListener; 13 | 14 | import java.sql.Timestamp; 15 | import java.time.LocalDateTime; 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | @Entity 20 | @EntityListeners(AuditingEntityListener.class) 21 | @Getter 22 | @Setter 23 | @AllArgsConstructor 24 | @NoArgsConstructor 25 | public class Clientes { 26 | @Id 27 | @GeneratedValue(strategy = GenerationType.IDENTITY) 28 | private Long id; 29 | 30 | @Column(nullable = false) 31 | private String nome; 32 | 33 | @Column(nullable = false) 34 | private Boolean situacao = Boolean.TRUE; 35 | 36 | @CreatedDate 37 | @Column(name = "created_at", updatable = false) 38 | private LocalDateTime createdAt; 39 | 40 | @LastModifiedDate 41 | @Column(name = "updated_at") 42 | private LocalDateTime updatedAt; 43 | 44 | @OneToMany(mappedBy = "clientes", cascade = CascadeType.ALL, orphanRemoval = true) 45 | @JsonManagedReference 46 | private List contatos = new ArrayList<>(); 47 | } 48 | -------------------------------------------------------------------------------- /project/src/test/java/br/com/nubank/service/ContatosServiceTest.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.service; 2 | 3 | import br.com.nubank.dto.Contatos.ContatosDTO; 4 | import br.com.nubank.model.Clientes; 5 | import br.com.nubank.model.Contatos; 6 | import br.com.nubank.repository.ClientesRepository; 7 | import br.com.nubank.repository.ContatosRepository; 8 | import org.junit.jupiter.api.Test; 9 | import org.junit.jupiter.api.extension.ExtendWith; 10 | import org.mockito.InjectMocks; 11 | import org.mockito.Mock; 12 | import org.mockito.junit.jupiter.MockitoExtension; 13 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals; 15 | import static org.junit.jupiter.api.Assertions.assertNotNull; 16 | import static org.mockito.ArgumentMatchers.any; 17 | import static org.mockito.Mockito.when; 18 | 19 | @ExtendWith(MockitoExtension.class) 20 | public class ContatosServiceTest { 21 | 22 | @Mock 23 | ContatosRepository contatosRepository; 24 | 25 | @Mock 26 | ClientesService clientesService; 27 | 28 | @InjectMocks 29 | ContatosService contatosService; 30 | 31 | @Test 32 | void salvarContato() { 33 | 34 | Clientes cliente = new Clientes(); 35 | cliente.setId(1L); 36 | 37 | when(clientesService.findById(1L)).thenReturn(cliente); 38 | when(contatosRepository.save(any(Contatos.class))).thenAnswer(invocation -> { 39 | Contatos contato = invocation.getArgument(0); 40 | return contato; 41 | }); 42 | 43 | ContatosDTO contatoDTO = new ContatosDTO(); 44 | contatoDTO.setClienteId(1L); 45 | contatoDTO.setTipo("EMAIL"); 46 | contatoDTO.setConteudo("maria_correia8@hotmail.com"); 47 | 48 | Contatos result = contatosService.salvarContato(contatoDTO); 49 | 50 | assertNotNull(result); 51 | assertEquals(cliente, result.getClientes()); 52 | 53 | assertEquals("EMAIL", result.getTipo()); 54 | assertEquals("maria_correia8@hotmail.com", result.getConteudo()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/controller/ContatosController.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.controller; 2 | 3 | import br.com.nubank.dto.Contatos.ContatosDTO; 4 | import br.com.nubank.dto.Contatos.ContatosResponseDTO; 5 | import br.com.nubank.mapper.ContatosMapper; 6 | import br.com.nubank.model.Contatos; 7 | import br.com.nubank.service.ClientesService; 8 | import br.com.nubank.service.ContatosService; 9 | import io.swagger.v3.oas.annotations.Operation; 10 | import io.swagger.v3.oas.annotations.media.Content; 11 | import io.swagger.v3.oas.annotations.responses.ApiResponse; 12 | import io.swagger.v3.oas.annotations.responses.ApiResponses; 13 | import lombok.RequiredArgsConstructor; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.bind.annotation.*; 17 | 18 | @RestController 19 | @RequestMapping("/contatos") 20 | @RequiredArgsConstructor 21 | public class ContatosController { 22 | 23 | private final ClientesService clientesService; 24 | private final ContatosService contatosService; 25 | 26 | @PostMapping 27 | @Operation(description = "Endpoint responsável por criar um contato") 28 | @ApiResponses(value = { 29 | @ApiResponse(responseCode = "201", description = "Contato criado com sucesso"), 30 | @ApiResponse(responseCode = "422", description = "Campos não atendem os requisitos para criação", content = @Content), 31 | @ApiResponse(responseCode = "400", description = "Erro de requisição", content = @Content), 32 | }) 33 | public ResponseEntity criar(@RequestBody ContatosDTO contatoDTO) { 34 | 35 | if (clientesService.findById(contatoDTO.getClienteId()) == null) { 36 | return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); 37 | } 38 | 39 | Contatos contatoSalvo = contatosService.salvarContato(contatoDTO); 40 | return ResponseEntity.status(HttpStatus.CREATED).body(ContatosMapper.toResponseDTO(contatoSalvo)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/mapper/ContatosMapper.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.mapper; 2 | 3 | import br.com.nubank.dto.Contatos.ContatosChildDTO; 4 | import br.com.nubank.dto.Contatos.ContatosChildResponseDTO; 5 | import br.com.nubank.dto.Contatos.ContatosDTO; 6 | import br.com.nubank.dto.Contatos.ContatosResponseDTO; 7 | import br.com.nubank.model.Clientes; 8 | import br.com.nubank.model.Contatos; 9 | 10 | public class ContatosMapper { 11 | 12 | public static ContatosDTO toDTO (Contatos contato) { 13 | ContatosDTO dto = new ContatosDTO(); 14 | 15 | dto.setTipo(contato.getTipo()); 16 | dto.setConteudo(contato.getConteudo()); 17 | dto.setClienteId(contato.getClientes().getId()); 18 | 19 | return dto; 20 | } 21 | 22 | public static ContatosResponseDTO toResponseDTO (Contatos contato) { 23 | ContatosResponseDTO dto = new ContatosResponseDTO(); 24 | 25 | dto.setId(contato.getId()); 26 | dto.setTipo(contato.getTipo()); 27 | dto.setSituacao(contato.getSituacao()); 28 | dto.setConteudo(contato.getConteudo()); 29 | dto.setCreatedAt(contato.getCreatedAt()); 30 | dto.setUpdatedAt(contato.getUpdatedAt()); 31 | dto.setClienteId(contato.getClientes().getId()); 32 | 33 | return dto; 34 | } 35 | 36 | public static ContatosChildResponseDTO toChildResponseDTO (Contatos contato) { 37 | ContatosChildResponseDTO dto = new ContatosChildResponseDTO(); 38 | 39 | dto.setId(contato.getId()); 40 | dto.setTipo(contato.getTipo()); 41 | dto.setSituacao(contato.getSituacao()); 42 | dto.setConteudo(contato.getConteudo()); 43 | dto.setCreatedAt(contato.getCreatedAt()); 44 | dto.setUpdatedAt(contato.getUpdatedAt()); 45 | 46 | return dto; 47 | } 48 | 49 | public static Contatos toEntity(ContatosChildDTO dto, Clientes cliente) { 50 | 51 | Contatos contato = new Contatos(); 52 | contato.setClientes(cliente); 53 | contato.setTipo(dto.getTipo()); 54 | contato.setConteudo(dto.getConteudo()); 55 | 56 | return contato; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/service/ClientesService.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.service; 2 | 3 | import br.com.nubank.dto.Clientes.ClientesDTO; 4 | import br.com.nubank.dto.Clientes.ClientesRecordDTO; 5 | import br.com.nubank.dto.Clientes.ClientesResponseDTO; 6 | import br.com.nubank.dto.Contatos.ContatosResponseDTO; 7 | import br.com.nubank.mapper.ClientesMapper; 8 | import br.com.nubank.mapper.ContatosMapper; 9 | import br.com.nubank.model.Clientes; 10 | import br.com.nubank.model.Contatos; 11 | import br.com.nubank.repository.ClientesRepository; 12 | import org.springframework.context.annotation.Lazy; 13 | import org.springframework.stereotype.Service; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | import java.util.Optional; 18 | 19 | @Service 20 | public class ClientesService { 21 | 22 | @Lazy 23 | private final ContatosService contatosService; 24 | 25 | private final ClientesRepository clientesRepository; 26 | 27 | public ClientesService (@Lazy ContatosService contatosService, ClientesRepository clientesRepository){ 28 | this.contatosService = contatosService; 29 | this.clientesRepository = clientesRepository; 30 | } 31 | 32 | public Clientes salvarCliente(ClientesRecordDTO clientesRecordDTO) { 33 | 34 | Clientes cliente = new Clientes(); 35 | cliente.setNome(clientesRecordDTO.nome()); 36 | 37 | if (clientesRecordDTO.contatos() != null && !clientesRecordDTO.contatos().isEmpty()) { 38 | List contatos = clientesRecordDTO.contatos().stream().map(contatosDTO -> ContatosMapper.toEntity(contatosDTO, cliente)).toList(); 39 | cliente.setContatos(contatos); 40 | } 41 | 42 | return clientesRepository.save(cliente); 43 | } 44 | 45 | public List listarContatos(Long clienteId) { 46 | Clientes cliente = this.findById(clienteId); 47 | 48 | if (cliente == null) { 49 | throw new RuntimeException("Cliente não encontrado"); 50 | } 51 | 52 | if (cliente.getContatos() == null || cliente.getContatos().isEmpty()){ 53 | return new ArrayList(); 54 | } 55 | 56 | return cliente.getContatos().stream().map(ContatosMapper::toResponseDTO).toList(); 57 | } 58 | 59 | public List listarTodos() { 60 | return clientesRepository.findAll().stream().map(ClientesMapper::toResponseDTO).toList(); 61 | } 62 | 63 | public Clientes findById(Long clienteId) { 64 | Optional cliente = clientesRepository.findById(clienteId); 65 | return cliente.orElse(null); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /project/src/main/java/br/com/nubank/controller/ClientesController.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.controller; 2 | 3 | import br.com.nubank.dto.Clientes.ClientesDTO; 4 | import br.com.nubank.dto.Clientes.ClientesRecordDTO; 5 | import br.com.nubank.dto.Clientes.ClientesResponseDTO; 6 | import br.com.nubank.dto.Contatos.ContatosResponseDTO; 7 | import br.com.nubank.mapper.ClientesMapper; 8 | import br.com.nubank.model.Clientes; 9 | import br.com.nubank.service.ClientesService; 10 | import io.swagger.v3.oas.annotations.media.Content; 11 | import io.swagger.v3.oas.annotations.responses.ApiResponse; 12 | import io.swagger.v3.oas.annotations.responses.ApiResponses; 13 | import lombok.RequiredArgsConstructor; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.bind.annotation.*; 17 | import io.swagger.v3.oas.annotations.Operation; 18 | 19 | 20 | import java.util.List; 21 | 22 | @RestController 23 | @RequestMapping("/clientes") 24 | @RequiredArgsConstructor 25 | public class ClientesController { 26 | 27 | private final ClientesService clientesService; 28 | 29 | @PostMapping 30 | @Operation(description = "Endpoint responsável por criar um cliente") 31 | @ApiResponses(value = { 32 | @ApiResponse(responseCode = "201", description = "Cliente criado com sucesso"), 33 | @ApiResponse(responseCode = "422", description = "Campos não atendem os requisitos para criação", content = @Content), 34 | @ApiResponse(responseCode = "400", description = "Erro de requisição", content = @Content), 35 | }) 36 | public ResponseEntity criar(@RequestBody ClientesRecordDTO clientesRecordDTO) { 37 | Clientes clienteSalvo = clientesService.salvarCliente(clientesRecordDTO); 38 | return ResponseEntity.status(HttpStatus.CREATED).body(ClientesMapper.toResponseDTO(clienteSalvo)); 39 | } 40 | 41 | @GetMapping 42 | @Operation(description = "Endpoint responsável por listar todos os clientes") 43 | @ApiResponses(value = { 44 | @ApiResponse(responseCode = "200", description = "Busca efetuada com sucesso"), 45 | }) 46 | public ResponseEntity> listarTotos() { 47 | return ResponseEntity.ok().body(clientesService.listarTodos()); 48 | } 49 | 50 | @GetMapping("/{clienteId}/contatos") 51 | @Operation(description = "Endpoint responsável por listar todos os contatos de um cliente") 52 | @ApiResponses(value = { 53 | @ApiResponse(responseCode = "200", description = "Busca efetuada com sucesso"), 54 | }) 55 | public ResponseEntity> listarContatos(@PathVariable Long clienteId) { 56 | return ResponseEntity.ok(clientesService.listarContatos(clienteId)); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /project/src/test/java/br/com/nubank/controller/ContatosControllerTest.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.controller; 2 | 3 | import br.com.nubank.dto.Contatos.ContatosDTO; 4 | import br.com.nubank.model.Clientes; 5 | import br.com.nubank.model.Contatos; 6 | import br.com.nubank.service.ClientesService; 7 | import br.com.nubank.service.ContatosService; 8 | import org.junit.jupiter.api.BeforeEach; 9 | import org.junit.jupiter.api.Test; 10 | import org.junit.jupiter.api.extension.ExtendWith; 11 | import org.mockito.InjectMocks; 12 | import org.mockito.Mock; 13 | import org.mockito.junit.jupiter.MockitoExtension; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.test.web.servlet.MockMvc; 16 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 17 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 18 | 19 | import static org.mockito.ArgumentMatchers.any; 20 | import static org.mockito.Mockito.when; 21 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 22 | 23 | @ExtendWith(MockitoExtension.class) 24 | public class ContatosControllerTest { 25 | 26 | @Mock 27 | ContatosService contatosService; 28 | 29 | @Mock 30 | ClientesService clientesService; 31 | 32 | @InjectMocks 33 | ContatosController contatosController; 34 | 35 | MockMvc mockMvc; 36 | 37 | @BeforeEach 38 | void setUp(){ 39 | mockMvc = MockMvcBuilders.standaloneSetup(contatosController).build(); 40 | } 41 | 42 | @Test 43 | void salvarContato() throws Exception { 44 | Clientes cliente = new Clientes(); 45 | cliente.setId(1L); 46 | cliente.setNome("TEST NUBANK 01"); 47 | 48 | Contatos contato = new Contatos(); 49 | contato.setId(1L); 50 | contato.setTipo("EMAIL"); 51 | contato.setConteudo("maria_correia8@hotmail.com"); 52 | contato.setSituacao(true); 53 | contato.setClientes(cliente); 54 | 55 | when(clientesService.findById(1L)).thenReturn(cliente); 56 | when(contatosService.salvarContato(any(ContatosDTO.class))).thenReturn(contato); 57 | 58 | mockMvc.perform(MockMvcRequestBuilders.post("/contatos") 59 | .content("{\"clienteId\":\"1\",\"tipo\":\"EMAIL\",\"conteudo\":\"maria_correia8@hotmail.com\"}") 60 | .contentType(MediaType.APPLICATION_JSON) 61 | .accept(MediaType.APPLICATION_JSON)) 62 | .andExpect(content().contentType(MediaType.APPLICATION_JSON)) 63 | .andExpect(status().isCreated()) 64 | .andExpect(jsonPath("$.id").value(contato.getId())) 65 | .andExpect(jsonPath("$.tipo").value(contato.getTipo())) 66 | .andExpect(jsonPath("$.conteudo").value(contato.getConteudo())) 67 | .andExpect(jsonPath("$.situacao").value(contato.getSituacao())) 68 | .andExpect(jsonPath("$.clienteId").value(cliente.getId())); 69 | 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Desafio Back-End Nubank 2 | 3 | ![Foto de Capa](assets/imgs/foto-de-capa.png) 4 | 5 | [![licence mit](https://img.shields.io/badge/licence-MIT-blue.svg)](./LICENSE) 6 | [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org) 7 | 8 | ## Sumário 9 | - [Desafio Back-End Nubank](#desafio-back-end-nubank) 10 | - [Sumário](#sumário) 11 | - [Introdução](#introdução) 12 | - [Requisitos Técnicos](#requisitos-técnicos) 13 | - [Requisitos de Código](#requisitos-de-código) 14 | - [Diferenciais (Não obrigatórios)](#diferenciais-não-obrigatórios) 15 | - [Tecnologias Usadas](#tecnologias-usadas) 16 | - [Licença](#licença) 17 | 18 | ## Introdução 19 | Construa uma API REST para gerenciamento de clientes e seus contatos. Cada cliente pode ter um ou mais contatos associados. 20 | 21 | ### Requisitos Técnicos 22 | 23 | A aplicação deve conter: 24 | 25 | - Cadastro de Cliente: `POST /clientes` 26 | - Cadastro de Contato associado a um cliente existente: `POST /contatos` 27 | - Listagem de todos os clientes com seus contatos: `GET /clientes` 28 | - Listagem de contatos de um cliente específico: `GET /clientes/{id}/contatos` 29 | - Uso do **Spring Boot** + **Spring Data JPA** 30 | - Banco de Dados **PostgreSQL** 31 | - Entidades **Cliente** e **Contato** com relacionamento `@OneToMany` / `@ManyToOne` 32 | 33 | ### Requisitos de Código 34 | 35 | Esperamos que o código siga boas práticas de desenvolvimento, incluindo: 36 | 37 | - Separação de responsabilidades (`controller`, `service`, `repository`) 38 | - Uso de **DTOs** para entrada e saída de dados 39 | - Tratamento adequado de erros 40 | - Uso de **Lombok** 41 | 42 | ### Diferenciais (Não obrigatórios) 43 | 44 | - Uso de **Docker** para subir o PostgreSQL 45 | - **Testes automatizados** 46 | - Documentação com **Swagger** 47 | 48 | ## Tecnologias Usadas 49 | ![Java](https://img.shields.io/badge/Java-ED8B00?style=for-the-badge&logo=java&logoColor=white)  50 | ![Spring Boot](https://img.shields.io/badge/Spring_Boot-6DB33F?style=for-the-badge&logo=spring-boot&logoColor=white)  51 | ![Spring Data JPA](https://img.shields.io/badge/Spring%20Data%20JPA-007396?style=for-the-badge&logo=hibernate&logoColor=white)  52 | ![PostgreSQL](https://img.shields.io/badge/PostgreSQL-336791?style=for-the-badge&logo=postgresql&logoColor=white)  53 | ![Lombok](https://img.shields.io/badge/Lombok-FF0000?style=for-the-badge&logo=lombok&logoColor=white)  54 | ![Docker](https://img.shields.io/badge/Docker-2496ED?style=for-the-badge&logo=docker&logoColor=white)  55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | ## Licença 67 | Este projeto está sob a [Licença MIT](./LICENSE.md). Consulte o arquivo [LICENSE.md](LICENSE.md) para obter mais detalhes. 68 | -------------------------------------------------------------------------------- /project/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.5.3 9 | 10 | 11 | br.com.nubank 12 | nubank 13 | 0.0.1 14 | Desafio Nubank 15 | Desafio Nubank 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 21 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-data-jpa 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-web 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-validation 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-devtools 48 | runtime 49 | true 50 | 51 | 52 | org.postgresql 53 | postgresql 54 | runtime 55 | 56 | 57 | org.projectlombok 58 | lombok 59 | true 60 | 61 | 62 | org.springdoc 63 | springdoc-openapi-starter-webmvc-ui 64 | 2.8.9 65 | 66 | 67 | org.springframework.boot 68 | spring-boot-starter-test 69 | test 70 | 71 | 72 | junit 73 | junit 74 | test 75 | 76 | 77 | 78 | 79 | 80 | 81 | org.apache.maven.plugins 82 | maven-compiler-plugin 83 | 84 | 85 | 86 | org.projectlombok 87 | lombok 88 | 89 | 90 | 91 | 92 | 93 | org.springframework.boot 94 | spring-boot-maven-plugin 95 | 96 | 97 | 98 | org.projectlombok 99 | lombok 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /project/src/test/java/br/com/nubank/service/ClientesServiceTest.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.service; 2 | 3 | import br.com.nubank.dto.Clientes.ClientesRecordDTO; 4 | import br.com.nubank.dto.Clientes.ClientesResponseDTO; 5 | import br.com.nubank.dto.Contatos.ContatosChildDTO; 6 | import br.com.nubank.dto.Contatos.ContatosResponseDTO; 7 | import br.com.nubank.model.Clientes; 8 | import br.com.nubank.model.Contatos; 9 | import br.com.nubank.repository.ClientesRepository; 10 | import org.junit.jupiter.api.BeforeEach; 11 | import org.junit.jupiter.api.Test; 12 | import org.junit.jupiter.api.extension.ExtendWith; 13 | import org.mockito.InjectMocks; 14 | import org.mockito.Mock; 15 | import org.mockito.junit.jupiter.MockitoExtension; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | import java.util.Optional; 20 | 21 | import static org.junit.jupiter.api.Assertions.*; 22 | import static org.mockito.ArgumentMatchers.any; 23 | import static org.mockito.Mockito.when; 24 | 25 | @ExtendWith(MockitoExtension.class) 26 | public class ClientesServiceTest { 27 | 28 | @Mock 29 | ClientesRepository clientesRepository; 30 | 31 | @InjectMocks 32 | ClientesService clientesService; 33 | 34 | ClientesRecordDTO clientesRecordDTO; 35 | List contatosChildDTOS; 36 | 37 | @BeforeEach 38 | void setUp(){ 39 | contatosChildDTOS = new ArrayList<>(); 40 | 41 | contatosChildDTOS.add(new ContatosChildDTO("TELEFONE", "6621648176")); 42 | contatosChildDTOS.add(new ContatosChildDTO("TELEFONE", "9632197043")); 43 | contatosChildDTOS.add(new ContatosChildDTO("EMAIL", "maria_correia8@hotmail.com")); 44 | contatosChildDTOS.add(new ContatosChildDTO("EMAIL", "alissonsodre58@geradornv.com")); 45 | 46 | clientesRecordDTO = new ClientesRecordDTO("TEST NUBANK 01", contatosChildDTOS); 47 | } 48 | 49 | @Test 50 | void salvarCliente(){ 51 | when(clientesRepository.save(any(Clientes.class))).thenAnswer(invocation -> { 52 | Clientes cliente = invocation.getArgument(0); 53 | return cliente; 54 | }); 55 | 56 | 57 | Clientes cliente = clientesService.salvarCliente(clientesRecordDTO); 58 | 59 | assertEquals("TEST NUBANK 01", cliente.getNome()); 60 | assertTrue(cliente.getSituacao()); 61 | 62 | assertEquals("TELEFONE", cliente.getContatos().getFirst().getTipo()); 63 | assertEquals("6621648176", cliente.getContatos().getFirst().getConteudo()); 64 | 65 | assertEquals("TELEFONE", cliente.getContatos().get(1).getTipo()); 66 | assertEquals("9632197043", cliente.getContatos().get(1).getConteudo()); 67 | 68 | assertEquals("EMAIL", cliente.getContatos().get(2).getTipo()); 69 | assertEquals("maria_correia8@hotmail.com", cliente.getContatos().get(2).getConteudo()); 70 | 71 | assertEquals("EMAIL", cliente.getContatos().get(3).getTipo()); 72 | assertEquals("alissonsodre58@geradornv.com", cliente.getContatos().get(3).getConteudo()); 73 | } 74 | 75 | @Test 76 | void findById(){ 77 | Clientes cliente = new Clientes(); 78 | cliente.setId(1L); 79 | cliente.setNome("TEST NUBANK 02"); 80 | 81 | when(clientesRepository.findById(1L)).thenReturn(Optional.of(cliente)); 82 | 83 | Clientes result = clientesService.findById(1L); 84 | 85 | assertNotNull(result); 86 | assertEquals(1L, result.getId()); 87 | assertEquals("TEST NUBANK 02", result.getNome()); 88 | } 89 | 90 | @Test 91 | void listarContatos(){ 92 | Clientes cliente = new Clientes(); 93 | cliente.setId(1L); 94 | cliente.setNome("TEST NUBANK 02"); 95 | 96 | List contatos = new ArrayList<>(); 97 | 98 | Contatos contato1 = new Contatos(); 99 | contato1.setTipo("TELEFONE"); 100 | contato1.setConteudo("6621648176"); 101 | contato1.setClientes(cliente); 102 | 103 | Contatos contato2 = new Contatos(); 104 | contato2.setTipo("EMAIL"); 105 | contato2.setConteudo("maria_correia8@hotmail.com"); 106 | contato2.setClientes(cliente); 107 | 108 | contatos.add(contato1); 109 | contatos.add(contato2); 110 | 111 | cliente.setContatos(contatos); 112 | 113 | when(clientesRepository.findById(1L)).thenReturn(Optional.of(cliente)); 114 | 115 | List result = clientesService.listarContatos(1L); 116 | 117 | assertNotNull(result); 118 | assertEquals("TELEFONE",result.getFirst().getTipo()); 119 | assertEquals("6621648176",result.getFirst().getConteudo()); 120 | 121 | assertEquals("EMAIL",result.getLast().getTipo()); 122 | assertEquals("maria_correia8@hotmail.com",result.getLast().getConteudo()); 123 | } 124 | 125 | @Test 126 | void listarTodos() { 127 | Clientes cliente1 = new Clientes(); 128 | cliente1.setId(1L); 129 | cliente1.setNome("TEST NUBANK 03 - CLIENTE 1"); 130 | 131 | List contatosCliente1 = new ArrayList<>(); 132 | 133 | Contatos contato1 = new Contatos(); 134 | contato1.setTipo("TELEFONE"); 135 | contato1.setConteudo("6621648176"); 136 | contato1.setClientes(cliente1); 137 | 138 | contatosCliente1.add(contato1); 139 | 140 | cliente1.setContatos(contatosCliente1); 141 | 142 | List contatosCliente2 = new ArrayList<>(); 143 | 144 | Clientes cliente2 = new Clientes(); 145 | cliente2.setId(2L); 146 | cliente2.setNome("TEST NUBANK 03 - CLIENTE 2"); 147 | 148 | Contatos contato2 = new Contatos(); 149 | contato2.setTipo("EMAIL"); 150 | contato2.setConteudo("maria_correia8@hotmail.com"); 151 | contato2.setClientes(cliente2); 152 | 153 | contatosCliente2.add(contato2); 154 | 155 | cliente2.setContatos(contatosCliente2); 156 | 157 | List clientes = new ArrayList<>(); 158 | 159 | clientes.add(cliente1); 160 | clientes.add(cliente2); 161 | 162 | when(clientesRepository.findAll()).thenReturn(clientes); 163 | 164 | List result = clientesService.listarTodos(); 165 | 166 | assertNotNull(result); 167 | assertEquals("TEST NUBANK 03 - CLIENTE 1",result.getFirst().getNome()); 168 | 169 | assertEquals("TELEFONE",result.getFirst().getContatos().getFirst().getTipo()); 170 | assertEquals("6621648176",result.getFirst().getContatos().getFirst().getConteudo()); 171 | 172 | assertEquals("TEST NUBANK 03 - CLIENTE 2",result.getLast().getNome()); 173 | 174 | assertEquals("EMAIL",result.getLast().getContatos().getFirst().getTipo()); 175 | assertEquals("maria_correia8@hotmail.com",result.getLast().getContatos().getFirst().getConteudo()); 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /project/mvnw.cmd: -------------------------------------------------------------------------------- 1 | <# : batch portion 2 | @REM ---------------------------------------------------------------------------- 3 | @REM Licensed to the Apache Software Foundation (ASF) under one 4 | @REM or more contributor license agreements. See the NOTICE file 5 | @REM distributed with this work for additional information 6 | @REM regarding copyright ownership. The ASF licenses this file 7 | @REM to you under the Apache License, Version 2.0 (the 8 | @REM "License"); you may not use this file except in compliance 9 | @REM with the License. You may obtain a copy of the License at 10 | @REM 11 | @REM http://www.apache.org/licenses/LICENSE-2.0 12 | @REM 13 | @REM Unless required by applicable law or agreed to in writing, 14 | @REM software distributed under the License is distributed on an 15 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | @REM KIND, either express or implied. See the License for the 17 | @REM specific language governing permissions and limitations 18 | @REM under the License. 19 | @REM ---------------------------------------------------------------------------- 20 | 21 | @REM ---------------------------------------------------------------------------- 22 | @REM Apache Maven Wrapper startup batch script, version 3.3.2 23 | @REM 24 | @REM Optional ENV vars 25 | @REM MVNW_REPOURL - repo url base for downloading maven distribution 26 | @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 27 | @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output 28 | @REM ---------------------------------------------------------------------------- 29 | 30 | @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) 31 | @SET __MVNW_CMD__= 32 | @SET __MVNW_ERROR__= 33 | @SET __MVNW_PSMODULEP_SAVE=%PSModulePath% 34 | @SET PSModulePath= 35 | @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( 36 | IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) 37 | ) 38 | @SET PSModulePath=%__MVNW_PSMODULEP_SAVE% 39 | @SET __MVNW_PSMODULEP_SAVE= 40 | @SET __MVNW_ARG0_NAME__= 41 | @SET MVNW_USERNAME= 42 | @SET MVNW_PASSWORD= 43 | @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) 44 | @echo Cannot start maven from wrapper >&2 && exit /b 1 45 | @GOTO :EOF 46 | : end batch / begin powershell #> 47 | 48 | $ErrorActionPreference = "Stop" 49 | if ($env:MVNW_VERBOSE -eq "true") { 50 | $VerbosePreference = "Continue" 51 | } 52 | 53 | # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties 54 | $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl 55 | if (!$distributionUrl) { 56 | Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" 57 | } 58 | 59 | switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { 60 | "maven-mvnd-*" { 61 | $USE_MVND = $true 62 | $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" 63 | $MVN_CMD = "mvnd.cmd" 64 | break 65 | } 66 | default { 67 | $USE_MVND = $false 68 | $MVN_CMD = $script -replace '^mvnw','mvn' 69 | break 70 | } 71 | } 72 | 73 | # apply MVNW_REPOURL and calculate MAVEN_HOME 74 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 75 | if ($env:MVNW_REPOURL) { 76 | $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } 77 | $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" 78 | } 79 | $distributionUrlName = $distributionUrl -replace '^.*/','' 80 | $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' 81 | $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" 82 | if ($env:MAVEN_USER_HOME) { 83 | $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" 84 | } 85 | $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' 86 | $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" 87 | 88 | if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { 89 | Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" 90 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 91 | exit $? 92 | } 93 | 94 | if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { 95 | Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" 96 | } 97 | 98 | # prepare tmp dir 99 | $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile 100 | $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" 101 | $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null 102 | trap { 103 | if ($TMP_DOWNLOAD_DIR.Exists) { 104 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 105 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 106 | } 107 | } 108 | 109 | New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null 110 | 111 | # Download and Install Apache Maven 112 | Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 113 | Write-Verbose "Downloading from: $distributionUrl" 114 | Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 115 | 116 | $webclient = New-Object System.Net.WebClient 117 | if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { 118 | $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) 119 | } 120 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 121 | $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null 122 | 123 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 124 | $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum 125 | if ($distributionSha256Sum) { 126 | if ($USE_MVND) { 127 | Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." 128 | } 129 | Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash 130 | if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { 131 | Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." 132 | } 133 | } 134 | 135 | # unzip and move 136 | Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null 137 | Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null 138 | try { 139 | Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null 140 | } catch { 141 | if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { 142 | Write-Error "fail to move MAVEN_HOME" 143 | } 144 | } finally { 145 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 146 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 147 | } 148 | 149 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 150 | -------------------------------------------------------------------------------- /project/src/test/java/br/com/nubank/controller/ClientesControllerTest.java: -------------------------------------------------------------------------------- 1 | package br.com.nubank.controller; 2 | 3 | import br.com.nubank.dto.Clientes.ClientesRecordDTO; 4 | import br.com.nubank.dto.Clientes.ClientesResponseDTO; 5 | import br.com.nubank.dto.Contatos.ContatosChildResponseDTO; 6 | import br.com.nubank.dto.Contatos.ContatosResponseDTO; 7 | import br.com.nubank.model.Clientes; 8 | import br.com.nubank.model.Contatos; 9 | import br.com.nubank.service.ClientesService; 10 | import org.junit.jupiter.api.BeforeEach; 11 | import org.junit.jupiter.api.Test; 12 | import org.junit.jupiter.api.extension.ExtendWith; 13 | import org.mockito.InjectMocks; 14 | import org.mockito.Mock; 15 | import org.mockito.junit.jupiter.MockitoExtension; 16 | import org.springframework.http.MediaType; 17 | import org.springframework.test.web.servlet.MockMvc; 18 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 19 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import static org.mockito.ArgumentMatchers.any; 25 | import static org.mockito.Mockito.when; 26 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 27 | 28 | @ExtendWith(MockitoExtension.class) 29 | public class ClientesControllerTest { 30 | 31 | @Mock 32 | ClientesService clientesService; 33 | 34 | @InjectMocks 35 | ClientesController clientesController; 36 | 37 | MockMvc mockMvc; 38 | 39 | @BeforeEach 40 | void setUp(){ 41 | mockMvc = MockMvcBuilders.standaloneSetup(clientesController).build(); 42 | } 43 | 44 | @Test 45 | void criar() throws Exception { 46 | 47 | Clientes cliente = new Clientes(); 48 | 49 | cliente.setId(1L); 50 | cliente.setNome("TEST NUBANK 01"); 51 | 52 | List contatos = new ArrayList<>(); 53 | 54 | Contatos contato = new Contatos(); 55 | contato.setId(1L); 56 | contato.setTipo("TELEFONE"); 57 | contato.setConteudo("6621648176"); 58 | contato.setClientes(cliente); 59 | 60 | contatos.add(contato); 61 | 62 | cliente.setContatos(contatos); 63 | 64 | when(clientesService.salvarCliente(any(ClientesRecordDTO.class))).thenReturn(cliente); 65 | 66 | mockMvc.perform(MockMvcRequestBuilders.post("/clientes") 67 | .content("{\"nome\":\"TEST NUBANK 01\",\"contatos\":[{\"tipo\":\"TELEFONE\",\"conteudo\":\"6621648176\"}]}") 68 | .contentType(MediaType.APPLICATION_JSON) 69 | .accept(MediaType.APPLICATION_JSON)) 70 | .andExpect(content().contentType(MediaType.APPLICATION_JSON)) 71 | .andExpect(status().isCreated()) 72 | .andExpect(jsonPath("$.nome").value("TEST NUBANK 01")) 73 | .andExpect(jsonPath("$.contatos").isNotEmpty()) 74 | .andExpect(jsonPath("$.contatos[0].tipo").value("TELEFONE")) 75 | .andExpect(jsonPath("$.contatos[0].conteudo").value("6621648176")); 76 | } 77 | 78 | @Test 79 | void listarTotos() throws Exception { 80 | List contatos1 = new ArrayList<>(); 81 | 82 | ContatosChildResponseDTO contato1 = new ContatosChildResponseDTO(); 83 | contato1.setId(1L); 84 | contato1.setTipo("TELEFONE"); 85 | contato1.setConteudo("6621648176"); 86 | contato1.setSituacao(true); 87 | 88 | contatos1.add(contato1); 89 | 90 | ClientesResponseDTO cliente1 = new ClientesResponseDTO(); 91 | 92 | cliente1.setId(1L); 93 | cliente1.setNome("TEST NUBANK 02 - Cliente 1"); 94 | cliente1.setContatos(contatos1); 95 | 96 | List contatos2 = new ArrayList<>(); 97 | 98 | ContatosChildResponseDTO contato2 = new ContatosChildResponseDTO(); 99 | contato2.setId(2L); 100 | contato2.setTipo("EMAIL"); 101 | contato2.setConteudo("maria_correia8@hotmail.com"); 102 | contato2.setSituacao(true); 103 | 104 | contatos2.add(contato2); 105 | 106 | ClientesResponseDTO cliente2 = new ClientesResponseDTO(); 107 | 108 | cliente2.setId(2L); 109 | cliente2.setNome("TEST NUBANK 02 - Cliente 2"); 110 | cliente2.setContatos(contatos2); 111 | 112 | List clientes = new ArrayList<>(); 113 | 114 | clientes.add(cliente1); 115 | clientes.add(cliente2); 116 | 117 | when(clientesService.listarTodos()).thenReturn(clientes); 118 | 119 | mockMvc.perform(MockMvcRequestBuilders.get("/clientes") 120 | .accept(MediaType.APPLICATION_JSON)) 121 | .andExpect(content().contentType(MediaType.APPLICATION_JSON)) 122 | .andExpect(status().isOk()) 123 | .andExpect(jsonPath("$[0].nome").value("TEST NUBANK 02 - Cliente 1")) 124 | .andExpect(jsonPath("$[0].contatos").isNotEmpty()) 125 | .andExpect(jsonPath("$[0].contatos[0].id").value(1)) 126 | .andExpect(jsonPath("$[0].contatos[0].tipo").value("TELEFONE")) 127 | .andExpect(jsonPath("$[0].contatos[0].conteudo").value("6621648176")) 128 | .andExpect(jsonPath("$[0].contatos[0].situacao").value(true)) 129 | .andExpect(jsonPath("$[1].nome").value("TEST NUBANK 02 - Cliente 2")) 130 | .andExpect(jsonPath("$[1].contatos").isNotEmpty()) 131 | .andExpect(jsonPath("$[1].contatos[0].id").value(2)) 132 | .andExpect(jsonPath("$[1].contatos[0].tipo").value("EMAIL")) 133 | .andExpect(jsonPath("$[1].contatos[0].conteudo").value("maria_correia8@hotmail.com")) 134 | .andExpect(jsonPath("$[1].contatos[0].situacao").value(true)); 135 | } 136 | 137 | @Test 138 | void listarContatos() throws Exception { 139 | List contatos = new ArrayList<>(); 140 | 141 | ContatosResponseDTO contato1 = new ContatosResponseDTO(); 142 | contato1.setId(1L); 143 | contato1.setTipo("TELEFONE"); 144 | contato1.setConteudo("6621648176"); 145 | contato1.setSituacao(true); 146 | contato1.setClienteId(1L); 147 | 148 | ContatosResponseDTO contato2 = new ContatosResponseDTO(); 149 | contato2.setId(2L); 150 | contato2.setTipo("EMAIL"); 151 | contato2.setConteudo("maria_correia8@hotmail.com"); 152 | contato2.setSituacao(true); 153 | contato2.setClienteId(1L); 154 | 155 | contatos.add(contato1); 156 | contatos.add(contato2); 157 | 158 | when(clientesService.listarContatos(1L)).thenReturn(contatos); 159 | 160 | mockMvc.perform(MockMvcRequestBuilders.get("/clientes/{clienteId}/contatos", 1L) 161 | .accept(MediaType.APPLICATION_JSON)) 162 | .andExpect(content().contentType(MediaType.APPLICATION_JSON)) 163 | .andExpect(status().isOk()) 164 | .andExpect(jsonPath("$[0].id").value(1)) 165 | .andExpect(jsonPath("$[0].tipo").value("TELEFONE")) 166 | .andExpect(jsonPath("$[0].conteudo").value("6621648176")) 167 | .andExpect(jsonPath("$[0].situacao").value(true)) 168 | .andExpect(jsonPath("$[0].clienteId").value(1)) 169 | .andExpect(jsonPath("$[1].id").value(2)) 170 | .andExpect(jsonPath("$[1].tipo").value("EMAIL")) 171 | .andExpect(jsonPath("$[1].conteudo").value("maria_correia8@hotmail.com")) 172 | .andExpect(jsonPath("$[1].situacao").value(true)) 173 | .andExpect(jsonPath("$[1].clienteId").value(1)); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /project/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.3.2 23 | # 24 | # Optional ENV vars 25 | # ----------------- 26 | # JAVA_HOME - location of a JDK home dir, required when download maven via java source 27 | # MVNW_REPOURL - repo url base for downloading maven distribution 28 | # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 29 | # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output 30 | # ---------------------------------------------------------------------------- 31 | 32 | set -euf 33 | [ "${MVNW_VERBOSE-}" != debug ] || set -x 34 | 35 | # OS specific support. 36 | native_path() { printf %s\\n "$1"; } 37 | case "$(uname)" in 38 | CYGWIN* | MINGW*) 39 | [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" 40 | native_path() { cygpath --path --windows "$1"; } 41 | ;; 42 | esac 43 | 44 | # set JAVACMD and JAVACCMD 45 | set_java_home() { 46 | # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched 47 | if [ -n "${JAVA_HOME-}" ]; then 48 | if [ -x "$JAVA_HOME/jre/sh/java" ]; then 49 | # IBM's JDK on AIX uses strange locations for the executables 50 | JAVACMD="$JAVA_HOME/jre/sh/java" 51 | JAVACCMD="$JAVA_HOME/jre/sh/javac" 52 | else 53 | JAVACMD="$JAVA_HOME/bin/java" 54 | JAVACCMD="$JAVA_HOME/bin/javac" 55 | 56 | if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then 57 | echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 58 | echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 59 | return 1 60 | fi 61 | fi 62 | else 63 | JAVACMD="$( 64 | 'set' +e 65 | 'unset' -f command 2>/dev/null 66 | 'command' -v java 67 | )" || : 68 | JAVACCMD="$( 69 | 'set' +e 70 | 'unset' -f command 2>/dev/null 71 | 'command' -v javac 72 | )" || : 73 | 74 | if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then 75 | echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 76 | return 1 77 | fi 78 | fi 79 | } 80 | 81 | # hash string like Java String::hashCode 82 | hash_string() { 83 | str="${1:-}" h=0 84 | while [ -n "$str" ]; do 85 | char="${str%"${str#?}"}" 86 | h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) 87 | str="${str#?}" 88 | done 89 | printf %x\\n $h 90 | } 91 | 92 | verbose() { :; } 93 | [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } 94 | 95 | die() { 96 | printf %s\\n "$1" >&2 97 | exit 1 98 | } 99 | 100 | trim() { 101 | # MWRAPPER-139: 102 | # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. 103 | # Needed for removing poorly interpreted newline sequences when running in more 104 | # exotic environments such as mingw bash on Windows. 105 | printf "%s" "${1}" | tr -d '[:space:]' 106 | } 107 | 108 | # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties 109 | while IFS="=" read -r key value; do 110 | case "${key-}" in 111 | distributionUrl) distributionUrl=$(trim "${value-}") ;; 112 | distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; 113 | esac 114 | done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" 115 | [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" 116 | 117 | case "${distributionUrl##*/}" in 118 | maven-mvnd-*bin.*) 119 | MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ 120 | case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in 121 | *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; 122 | :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; 123 | :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; 124 | :Linux*x86_64*) distributionPlatform=linux-amd64 ;; 125 | *) 126 | echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 127 | distributionPlatform=linux-amd64 128 | ;; 129 | esac 130 | distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" 131 | ;; 132 | maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; 133 | *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; 134 | esac 135 | 136 | # apply MVNW_REPOURL and calculate MAVEN_HOME 137 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 138 | [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" 139 | distributionUrlName="${distributionUrl##*/}" 140 | distributionUrlNameMain="${distributionUrlName%.*}" 141 | distributionUrlNameMain="${distributionUrlNameMain%-bin}" 142 | MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" 143 | MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" 144 | 145 | exec_maven() { 146 | unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : 147 | exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" 148 | } 149 | 150 | if [ -d "$MAVEN_HOME" ]; then 151 | verbose "found existing MAVEN_HOME at $MAVEN_HOME" 152 | exec_maven "$@" 153 | fi 154 | 155 | case "${distributionUrl-}" in 156 | *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; 157 | *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; 158 | esac 159 | 160 | # prepare tmp dir 161 | if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then 162 | clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } 163 | trap clean HUP INT TERM EXIT 164 | else 165 | die "cannot create temp dir" 166 | fi 167 | 168 | mkdir -p -- "${MAVEN_HOME%/*}" 169 | 170 | # Download and Install Apache Maven 171 | verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 172 | verbose "Downloading from: $distributionUrl" 173 | verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 174 | 175 | # select .zip or .tar.gz 176 | if ! command -v unzip >/dev/null; then 177 | distributionUrl="${distributionUrl%.zip}.tar.gz" 178 | distributionUrlName="${distributionUrl##*/}" 179 | fi 180 | 181 | # verbose opt 182 | __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' 183 | [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v 184 | 185 | # normalize http auth 186 | case "${MVNW_PASSWORD:+has-password}" in 187 | '') MVNW_USERNAME='' MVNW_PASSWORD='' ;; 188 | has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; 189 | esac 190 | 191 | if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then 192 | verbose "Found wget ... using wget" 193 | wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" 194 | elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then 195 | verbose "Found curl ... using curl" 196 | curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" 197 | elif set_java_home; then 198 | verbose "Falling back to use Java to download" 199 | javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" 200 | targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" 201 | cat >"$javaSource" <<-END 202 | public class Downloader extends java.net.Authenticator 203 | { 204 | protected java.net.PasswordAuthentication getPasswordAuthentication() 205 | { 206 | return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); 207 | } 208 | public static void main( String[] args ) throws Exception 209 | { 210 | setDefault( new Downloader() ); 211 | java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); 212 | } 213 | } 214 | END 215 | # For Cygwin/MinGW, switch paths to Windows format before running javac and java 216 | verbose " - Compiling Downloader.java ..." 217 | "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" 218 | verbose " - Running Downloader.java ..." 219 | "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" 220 | fi 221 | 222 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 223 | if [ -n "${distributionSha256Sum-}" ]; then 224 | distributionSha256Result=false 225 | if [ "$MVN_CMD" = mvnd.sh ]; then 226 | echo "Checksum validation is not supported for maven-mvnd." >&2 227 | echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 228 | exit 1 229 | elif command -v sha256sum >/dev/null; then 230 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then 231 | distributionSha256Result=true 232 | fi 233 | elif command -v shasum >/dev/null; then 234 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then 235 | distributionSha256Result=true 236 | fi 237 | else 238 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 239 | echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 240 | exit 1 241 | fi 242 | if [ $distributionSha256Result = false ]; then 243 | echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 244 | echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 245 | exit 1 246 | fi 247 | fi 248 | 249 | # unzip and move 250 | if command -v unzip >/dev/null; then 251 | unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" 252 | else 253 | tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" 254 | fi 255 | printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" 256 | mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" 257 | 258 | clean || : 259 | exec_maven "$@" 260 | --------------------------------------------------------------------------------