Commit 20f39116 authored by 0Tyler's avatar 0Tyler

User

parent 39dc692a
This diff is collapsed.
...@@ -38,11 +38,4 @@ public class GatewayController { ...@@ -38,11 +38,4 @@ public class GatewayController {
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build()); .orElseGet(() -> ResponseEntity.noContent().build());
} }
// @PostMapping("/user")
// public ResponseEntity<SensitiveUser> setUser(@RequestBody SensitiveUser sensitiveUser) {
// return sensitiveUserService.add(sensitiveUser)
// .map(ResponseEntity::ok)
// .orElseGet(()->ResponseEntity.noContent().build());
// }
} }
...@@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class GatewayApplication { public class GatewayApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args); SpringApplication.run(GatewayApplication.class, args);
} }
} }
package edu.prlab.tyler.iotgateway.gateway.config;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User;
import edu.prlab.tyler.iotgateway.gateway.services.UserService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class DefaultData implements ApplicationRunner {
private UserService userService;
public DefaultData(UserService userService) {
this.userService = userService;
}
@Override
public void run(ApplicationArguments args) throws Exception {
User defaultUser = userService.registerNewUser(User.builder().username("testUser0").password("testUser0").build()).get();
System.out.println(defaultUser.toString());
}
}
...@@ -47,9 +47,9 @@ public class GatewayController { ...@@ -47,9 +47,9 @@ public class GatewayController {
} }
// //
@GetMapping("/device/{UDN}") @GetMapping("/device/{udn}")
public ResponseEntity<PrivacyPolicyReport> readPrivacyPolicyReportByDevice(@PathVariable(value = "UDN") String UDN) { public ResponseEntity<PrivacyPolicyReport> readPrivacyPolicyReportByDevice(@PathVariable(value = "udn") String udn) {
return privacyService.getRelatedPrivacyPolicies(UDN) return privacyService.getRelatedPrivacyPolicies(udn)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build()); .orElseGet(() -> ResponseEntity.noContent().build());
} }
......
package edu.prlab.tyler.iotgateway.gateway.controllers;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User;
import edu.prlab.tyler.iotgateway.gateway.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@PostMapping("/user")
public ResponseEntity<User> register(@RequestBody User user) {
return userService.registerNewUser(user)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build());
}
@PostMapping("/login")
public ResponseEntity<User> login(@RequestBody User user) {
return userService.login(user)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build());
}
}
package edu.prlab.tyler.iotgateway.gateway.pojo; package edu.prlab.tyler.iotgateway.gateway.pojo;
import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
@Data @Data
@Builder @Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity @Entity
public class DeviceIndex { public class DeviceIndex {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String udn; private String udn;
} }
package edu.prlab.tyler.iotgateway.gateway.pojo; package edu.prlab.tyler.iotgateway.gateway.pojo;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity; import javax.persistence.*;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Data @Data
@Builder @Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity @Entity
public class PrivacyChoiceIndex { public class PrivacyChoiceIndex {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id; private long id;
private LocalDateTime localDateTime; private LocalDateTime localDateTime;
private PrivacyChoice privacyChoice;
} }
package edu.prlab.tyler.iotgateway.gateway.pojo.auth; package edu.prlab.tyler.iotgateway.gateway.pojo.auth;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class User { public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private long id;
private String username; private String username;
private String password; private String password;
// private BigInteger publicKey; // private BigInteger publicKey;
// private BigInteger privateKey; // private BigInteger privateKey;
......
package edu.prlab.tyler.iotgateway.gateway.repositories;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
User findUserByUsername(String username);
boolean existsByUsername(String username);
}
...@@ -6,8 +6,6 @@ import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex; ...@@ -6,8 +6,6 @@ import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex;
import edu.prlab.tyler.iotgateway.gateway.repositories.PrivacyChoiceIndexRepository; import edu.prlab.tyler.iotgateway.gateway.repositories.PrivacyChoiceIndexRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -39,11 +37,12 @@ public class PrivacyService { ...@@ -39,11 +37,12 @@ public class PrivacyService {
public Optional<PrivacyChoice> setPrivacyPolicyChoice(PrivacyChoice privacyChoice) { public Optional<PrivacyChoice> setPrivacyPolicyChoice(PrivacyChoice privacyChoice) {
return Optional.of(privacyChoice) return Optional.of(privacyChoice)
.map(choice -> template.postForObject(cloudAddress + "/choice", choice, PrivacyChoice.class))
.map(choice -> privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder() .map(choice -> privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder()
.id(choice.getId())
.localDateTime(LocalDateTime.now()) .localDateTime(LocalDateTime.now())
.privacyChoice(choice)
.build())) .build()))
.map((choice) -> template.postForObject(cloudAddress + "/choice", choice.getPrivacyChoice(), PrivacyChoice.class)); .map(choice -> privacyChoice);
} }
// public Optional<Iterable<PrivacyChoice>> getPrivacyPolicyChoices() { // public Optional<Iterable<PrivacyChoice>> getPrivacyPolicyChoices() {
......
package edu.prlab.tyler.iotgateway.gateway.services;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User;
import edu.prlab.tyler.iotgateway.gateway.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class UserService {
private UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
//TODO
//需要增加加密機制
public Optional<User> registerNewUser(User user) {
return Optional.ofNullable(user)
.map(User::getUsername)
.filter(username->!userRepository.existsByUsername(username))
.map(username -> userRepository.save(user));
}
//TODO
//需要增加解密確認
public Optional<User> login(User user) {
return Optional.ofNullable(user.getUsername())
.map(username -> userRepository.findUserByUsername(username))
.filter(sensitiveUser->sensitiveUser.getPassword().equals(user.getPassword()));
}
}
package edu.prlab.tyler.iotgateway.gateway; package edu.prlab.tyler.iotgateway.gateway;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser; import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -42,14 +44,45 @@ public class GatewayHttpApiTest { ...@@ -42,14 +44,45 @@ public class GatewayHttpApiTest {
} }
@Test @Test
public void getPrivacyPolicyAndSetChoice() throws Exception{ public void getPrivacyPolicyAndSetChoice() throws Exception {
//取得裝置清單 //登入預設帳號
MvcResult result = mvc.perform(MockMvcRequestBuilders
.post("/login")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(User.builder().username("testUser0").password("testUser0").build())))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Assert.assertNotNull(result);
//取得裝置清單
result = mvc.perform(MockMvcRequestBuilders
.get("/device")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Iterable<Device> devices = mapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<Iterable<Device>>() {
});
Assert.assertNotNull(devices);
//綁定裝置
result = mvc.perform(MockMvcRequestBuilders
.post("/device/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(User.builder().username("testUser0").password("testUser0").build())))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Device device= mapper.readValue(result.getResponse().getContentAsString(), Device.class);
Assert.assertNotNull(device);
//拿取隱私政策 //拿取隱私政策
MvcResult result = mvc.perform(MockMvcRequestBuilders result = mvc.perform(MockMvcRequestBuilders
.get("/device/" + "a1252c49-4188-4e6d-a32e-66604c664fb8") .get("/device/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.accept(MediaType.APPLICATION_JSON_UTF8)) .accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print()) .andDo(print())
...@@ -59,7 +92,7 @@ public class GatewayHttpApiTest { ...@@ -59,7 +92,7 @@ public class GatewayHttpApiTest {
Assert.assertNotNull(report); Assert.assertNotNull(report);
//表達隱私偏好 //表達隱私偏好
Device device = report.getDevice(); device = report.getDevice();
PrivacyChoice privacyChoice = PrivacyChoice.builder() PrivacyChoice privacyChoice = PrivacyChoice.builder()
.privacyContent(PrivacyContent.builder() .privacyContent(PrivacyContent.builder()
.user(SensitiveUser.builder() .user(SensitiveUser.builder()
...@@ -83,12 +116,12 @@ public class GatewayHttpApiTest { ...@@ -83,12 +116,12 @@ public class GatewayHttpApiTest {
Assert.assertNotNull(result); Assert.assertNotNull(result);
//取得所有隱私偏好 //取得所有隱私偏好
// result = mvc.perform(MockMvcRequestBuilders result = mvc.perform(MockMvcRequestBuilders
// .get("/choice") .get("/choice")
// .accept(MediaType.APPLICATION_JSON_UTF8)) .accept(MediaType.APPLICATION_JSON_UTF8))
// .andDo(print()) .andDo(print())
// .andExpect(status().isOk()) .andExpect(status().isOk())
// .andReturn(); .andReturn();
// Assert.assertNotNull(result); Assert.assertNotNull(result);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment