Commit 1e8237b6 authored by 0Tyler's avatar 0Tyler

deviceContractTest

parent 48377c5d
......@@ -16,6 +16,17 @@
<option name="useAutoImport" value="true" />
<option name="useQualifiedModuleNames" value="true" />
</GradleProjectSettings>
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$/contract" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$/contract" />
</set>
</option>
<option name="useAutoImport" value="true" />
<option name="useQualifiedModuleNames" value="true" />
</GradleProjectSettings>
</option>
</component>
</project>
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
buildscript {
ext{
ext {
springBootVersion = '2.1.4.RELEASE'
mapstructVersion = '1.3.0.Final'
jwtVersion = '3.5.0'
jjwtVersion = '0.9.1'
}
repositories {
mavenCentral()
......@@ -31,9 +35,12 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
package edu.prlab.tyler.iotgateway.cloud.config;
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.Manufacturer;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model;
......@@ -8,6 +9,7 @@ import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.p3p.*;
import edu.prlab.tyler.iotgateway.cloud.services.DeviceService;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService;
import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
......@@ -15,25 +17,31 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import static java.util.Arrays.asList;
@Component
public class DefaultData implements ApplicationRunner {
private DeviceService deviceService;
private PrivacyPolicyReportService privacyPolicyReportService;
// private SensitiveUserService sensitiveUserService;
@Autowired
public DefaultData(DeviceService deviceService,
PrivacyPolicyReportService privacyPolicyReportService) {
PrivacyPolicyReportService privacyPolicyReportService,
SensitiveUserService sensitiveUserService) {
this.deviceService = deviceService;
this.privacyPolicyReportService = privacyPolicyReportService;
// this.sensitiveUserService = sensitiveUserService;
}
@Override
public void run(ApplicationArguments args) {
// SensitiveUser user = SensitiveUser.builder()
// .username("test")
// .password("test")
// .build();
// sensitiveUserService.add(user);
Device oxygenDevice = Device.builder()
.udn("a1252c49-4188-4e6d-a32e-66604c664fb8")
......@@ -112,4 +120,7 @@ public class DefaultData implements ApplicationRunner {
privacyPolicyReportService.add(oxygenPrivacyPolicyReport);
}
}
\ No newline at end of file
package edu.prlab.tyler.iotgateway.cloud.controllers;
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.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.cloud.services.DeviceService;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
......@@ -37,9 +41,16 @@ public class DeviceController {
.orElseGet(()->ResponseEntity.noContent().build());
}
// @GetMapping
// public ResponseEntity<Iterable<Device>> readDevices() {
// return Optional.of(deviceService.readll())
// .map(ResponseEntity::ok)
// .orElseGet(()->ResponseEntity.noContent().build());
// }
@GetMapping
public ResponseEntity<Iterable<Device>> readDevices() {
return Optional.of(deviceService.readll())
public ResponseEntity<ArrayList<Device>> readDevices(SensitiveUser user) {
return Optional.of(Lists.newArrayList(deviceService.readll()))
.map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build());
}
......
package edu.prlab.tyler.iotgateway.cloud.controllers;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService;
import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -11,12 +13,17 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
private PrivacyChoiceService privacyChoiceService;
private SensitiveUserService sensitiveUserService;
@Autowired
public GatewayController(PrivacyChoiceService privacyChoiceService) {
public GatewayController(PrivacyChoiceService privacyChoiceService,
SensitiveUserService sensitiveUserService) {
this.privacyChoiceService = privacyChoiceService;
this.sensitiveUserService = sensitiveUserService;
}
@PostMapping("/choice")
public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
return privacyChoiceService.add(privacyChoice)
......@@ -24,4 +31,11 @@ public class GatewayController {
.orElseGet(() -> ResponseEntity.noContent().build());
}
@PostMapping("user")
public ResponseEntity<SensitiveUser> setUser(@RequestBody SensitiveUser sensitiveUser) {
return sensitiveUserService.add(sensitiveUser)
.map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build());
}
}
package edu.prlab.tyler.iotgateway.cloud.pojo.auth;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.math.BigInteger;
@Data
......@@ -19,14 +14,16 @@ import java.math.BigInteger;
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class User {
public class SensitiveUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private int id;
private String account;
private long id;
private String username;
private String password;
private BigInteger publicKey;
private BigInteger privateKey;
private String address;
// private BigInteger publicKey;
// private BigInteger privateKey;
// private String address;
}
package edu.prlab.tyler.iotgateway.cloud.pojo.privacy;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.xml.internal.ws.api.pipe.ContentType;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import lombok.AllArgsConstructor;
import lombok.Builder;
......@@ -26,7 +25,7 @@ public class PrivacyContent {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User user;
private SensitiveUser user;
@OneToOne
@JoinColumn(name = "device_id")
......
package edu.prlab.tyler.iotgateway.cloud.repositories;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SensitiveUserRepository extends JpaRepository<SensitiveUser, Long> {
}
......@@ -2,6 +2,7 @@ package edu.prlab.tyler.iotgateway.cloud.services;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
import java.util.Optional;
public abstract class CrudService<T , ID> {
......@@ -12,6 +13,7 @@ public abstract class CrudService<T , ID> {
}
public Optional<T> add(T pojo) {
System.out.println(pojo.toString());
return Optional.of(repository.save(pojo));
}
......
package edu.prlab.tyler.iotgateway.cloud.services;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.repositories.SensitiveUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class SensitiveUserService {
private SensitiveUserRepository repository;
@Autowired
public SensitiveUserService(SensitiveUserRepository repository) {
this.repository = repository;
}
public Optional<SensitiveUser> add(SensitiveUser pojo) {
System.out.println(pojo.toString());
return Optional.of(repository.saveAndFlush(pojo));
}
}
package edu.prlab.tyler.iotgateway.cloud;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
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.Icon;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model;
......@@ -22,10 +24,9 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import static java.util.Arrays.asList;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -44,6 +45,7 @@ public class CloudHttpApiTest {
.webAppContextSetup(context)
.build();
this.mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
@Test
......@@ -73,7 +75,6 @@ public class CloudHttpApiTest {
.andDo(print())
.andExpect(status().isOk())
.andReturn();
device = mapper.readValue(result.getResponse().getContentAsString(), Device.class);
Assert.assertNotNull(device);
......@@ -83,7 +84,6 @@ public class CloudHttpApiTest {
.andDo(print())
.andExpect(status().isOk())
.andReturn();
device = mapper.readValue(result.getResponse().getContentAsString(), Device.class);
Assert.assertNotNull(device);
......@@ -93,33 +93,31 @@ public class CloudHttpApiTest {
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Assert.assertNotNull(result);
ArrayList<Device> devices = mapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<ArrayList<Device>>() {});
Assert.assertNotNull(devices);
result = mvc.perform(MockMvcRequestBuilders
.get("/device/privacy/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.get("/device/privacy/" + device.getUdn())
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
PrivacyPolicyReport report = mapper.readValue(result.getResponse().getContentAsString(), PrivacyPolicyReport.class);
Assert.assertNotNull(report);
PrivacyChoice privacyChoice = PrivacyChoice.builder()
.privacyContent(PrivacyContent.builder()
.user(User.builder()
.account("test")
.user(SensitiveUser.builder()
.username("test")
.password("test")
.publicKey(BigInteger.ONE)
.privateKey(BigInteger.ONE)
.address("test Address").build())
.build())
.device(device)
.policy(report.getPolicies().get(0))
.build())
.isAccepted(true)
.build();
mapper.writeValueAsString(privacyChoice);
result = mvc.perform(MockMvcRequestBuilders
.post("/choice")
.contentType(MediaType.APPLICATION_JSON_UTF8)
......
HELP.md
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}
ext {
web3jQuorumVersion = '4.0.6'
}
apply plugin: 'io.spring.dependency-management'
group = 'edu.prlab.tyler.iotgateway'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation "org.web3j:quorum:${web3jQuorumVersion}"
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testCompile("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}
#Mon Apr 29 13:41:54 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'contract'
package edu.prlab.tyler.iotgateway.contract;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ContractApplication {
public static void main(String[] args) {
SpringApplication.run(ContractApplication.class, args);
}
}
package edu.prlab.tyler.iotgateway.contract;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runners.MethodSorters;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.quorum.Quorum;
import org.web3j.tx.ClientTransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.tx.gas.StaticGasProvider;
import java.math.BigInteger;
......@@ -16,23 +21,22 @@ import java.math.BigInteger;
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class ContractTester {
private static final String rpcUrl = "http://localhost:22001";
private static final String rpcUrl = "http://localhost:22000";
private static final String privateKey = "7777777777777777777777777777777777777777777777777777777777777777";
// private static final String privateKey = "7777777777777777777777777777777777777777777777777777777777777777";
protected static final ContractGasProvider DEFAULT_GAS_PROVIDER = new StaticGasProvider(
private static final ContractGasProvider DEFAULT_GAS_PROVIDER = new StaticGasProvider(
BigInteger.ZERO,
BigInteger.valueOf(1000000000L)
DefaultGasProvider.GAS_LIMIT
);
@BeforeEach
public void setUp() {
@BeforeClass
public static void setUp() {
}
@Test
public void deployContract() throws Exception {
System.out.println("test start");
public void test1deployDeviceContract() throws Exception {
// Credentials credentials = Credentials.create(privateKey);
Quorum quorum = Quorum.build(new HttpService(rpcUrl));
Web3ClientVersion web3ClientVersion = quorum.web3ClientVersion().sendAsync().get();
......@@ -42,15 +46,34 @@ public class ContractTester {
String userAddress = quorum.ethAccounts().send().getAccounts().get(0);
System.out.println("User : " + userAddress);
// ClientTransactionManager manager = new ClientTransactionManager(quorum, userAddress);
// Credentials credentials = Credentials.create(privateKey);
ClientTransactionManager manager = new ClientTransactionManager(quorum, userAddress);
DeviceContract deviceContract = DeviceContract.deploy(quorum, manager, DEFAULT_GAS_PROVIDER).send();
String deviceContractAddress = deviceContract.getContractAddress();
System.out.println("智慧合約地址 : " + deviceContractAddress);
Assert.assertNotNull(deviceContract);
TransactionReceipt deviceReceipt = deviceContract.setdeviceinfo("testdevice").send();
System.out.println(deviceReceipt.toString());
TransactionReceipt privacyPolicyReceipt = deviceContract.setpp("testpp").send();
System.out.println(privacyPolicyReceipt.toString());
String deviceInfo = deviceContract.deviceInfo().send();
System.out.println(deviceInfo);
String privacyPolicy = deviceContract.privacypolicy().send();
System.out.println(privacyPolicy);
// DeviceContract deviceContract = DeviceContract.deploy(quorum, manager,DEFAULT_GAS_PROVIDER).send();
//
// System.out.println("智慧合約地址:" + deviceContract.getContractAddress());
//
// Assert.assertNotNull(deviceContract);
System.out.println("test end");
}
@Test
public void test2uploadDeviceInfoAndPolicy() throws Exception {}
@Test
public void test3deployGatewayContract() throws Exception {}
@Test
public void test4bind() throws Exception {}
}
......@@ -33,6 +33,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation project(":cloud")
implementation project(":contract")
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
......
......@@ -7,3 +7,6 @@ rootProject.name = 'gateway'
include ":cloud"
project(":cloud").projectDir = file("../cloud")
include ":contract"
project(":contract").projectDir = file("../contract")
\ No newline at end of file
package edu.prlab.tyler.iotgateway.gateway.controllers;
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.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService;
......@@ -15,6 +16,12 @@ public class GatewayController {
this.privacyService = privacyService;
}
// TODO
// @GetMapping("")
// public ResponseEntity<Iterable<Device>> readDevicesOfUser() {
//
// }
@GetMapping("/privacy/{UDN}")
public ResponseEntity<PrivacyPolicyReport> readPrivacyPolicyReportByDevice(@PathVariable(value = "UDN") String UDN) {
return privacyService.getRelatedPrivacyPolicies(UDN)
......@@ -28,5 +35,4 @@ public class GatewayController {
.map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build());
}
}
package edu.prlab.tyler.iotgateway.gateway.services;
import org.springframework.stereotype.Service;
@Service
public class DeviceService {
}
......@@ -14,13 +14,17 @@ public class PrivacyService {
private RestTemplate template;
@Value("${iotgateway.cloud.address}")
private String cloudAddress;
public PrivacyService() {
this.template = new RestTemplate();
}
@Value("${iotgateway.cloud.address}")
private void setCloudAddress(String cloudAddress) {
this.cloudAddress = cloudAddress;
}
public Optional<PrivacyPolicyReport> getRelatedPrivacyPolicies(String Udn) {
return Optional.ofNullable(template.getForObject(cloudAddress + "/device/privacy/" + Udn, PrivacyPolicyReport.class));
}
......
package edu.prlab.tyler.iotgateway.gateway;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
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.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
......@@ -20,8 +20,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.math.BigInteger;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -45,6 +43,10 @@ public class GatewayHttpApiTest {
@Test
public void getPrivacyPolicyAndSetChoice() throws Exception{
//取得裝置清單
//拿取隱私政策
MvcResult result = mvc.perform(MockMvcRequestBuilders
.get("/privacy/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.accept(MediaType.APPLICATION_JSON_UTF8))
......@@ -52,25 +54,21 @@ public class GatewayHttpApiTest {
.andExpect(status().isOk())
.andReturn();
PrivacyPolicyReport report = mapper.readValue(result.getResponse().getContentAsString(), PrivacyPolicyReport.class);
Assert.assertNotNull(report);
//表達隱私偏好
Device device = report.getDevice();
PrivacyChoice privacyChoice = PrivacyChoice.builder()
.privacyContent(PrivacyContent.builder()
.user(User.builder()
.account("test")
.user(SensitiveUser.builder()
.username("test")
.password("test")
.publicKey(BigInteger.ONE)
.privateKey(BigInteger.ONE)
.address("test Address").build())
.build())
.device(device)
.policy(report.getPolicies().get(0))
.build())
.isAccepted(true)
.build();
mapper.writeValueAsString(privacyChoice);
result = mvc.perform(MockMvcRequestBuilders
.post("/choice")
......@@ -80,8 +78,6 @@ public class GatewayHttpApiTest {
.andDo(print())
.andExpect(status().isOk())
.andReturn();
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