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

deviceContractTest

parent 48377c5d
...@@ -16,6 +16,17 @@ ...@@ -16,6 +16,17 @@
<option name="useAutoImport" value="true" /> <option name="useAutoImport" value="true" />
<option name="useQualifiedModuleNames" value="true" /> <option name="useQualifiedModuleNames" value="true" />
</GradleProjectSettings> </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> </option>
</component> </component>
</project> </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 { buildscript {
ext{ ext {
springBootVersion = '2.1.4.RELEASE' springBootVersion = '2.1.4.RELEASE'
mapstructVersion = '1.3.0.Final'
jwtVersion = '3.5.0'
jjwtVersion = '0.9.1'
} }
repositories { repositories {
mavenCentral() mavenCentral()
...@@ -31,9 +35,12 @@ repositories { ...@@ -31,9 +35,12 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
runtimeOnly 'com.h2database:h2' runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }
package edu.prlab.tyler.iotgateway.cloud.config; 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.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Manufacturer; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Manufacturer;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model;
...@@ -8,6 +9,7 @@ import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; ...@@ -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.pojo.privacy.p3p.*;
import edu.prlab.tyler.iotgateway.cloud.services.DeviceService; import edu.prlab.tyler.iotgateway.cloud.services.DeviceService;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
...@@ -15,25 +17,31 @@ import org.springframework.stereotype.Component; ...@@ -15,25 +17,31 @@ import org.springframework.stereotype.Component;
import java.util.Arrays; import java.util.Arrays;
import static java.util.Arrays.asList;
@Component @Component
public class DefaultData implements ApplicationRunner { public class DefaultData implements ApplicationRunner {
private DeviceService deviceService; private DeviceService deviceService;
private PrivacyPolicyReportService privacyPolicyReportService; private PrivacyPolicyReportService privacyPolicyReportService;
// private SensitiveUserService sensitiveUserService;
@Autowired @Autowired
public DefaultData(DeviceService deviceService, public DefaultData(DeviceService deviceService,
PrivacyPolicyReportService privacyPolicyReportService) { PrivacyPolicyReportService privacyPolicyReportService,
SensitiveUserService sensitiveUserService) {
this.deviceService = deviceService; this.deviceService = deviceService;
this.privacyPolicyReportService = privacyPolicyReportService; this.privacyPolicyReportService = privacyPolicyReportService;
// this.sensitiveUserService = sensitiveUserService;
} }
@Override @Override
public void run(ApplicationArguments args) { public void run(ApplicationArguments args) {
// SensitiveUser user = SensitiveUser.builder()
// .username("test")
// .password("test")
// .build();
// sensitiveUserService.add(user);
Device oxygenDevice = Device.builder() Device oxygenDevice = Device.builder()
.udn("a1252c49-4188-4e6d-a32e-66604c664fb8") .udn("a1252c49-4188-4e6d-a32e-66604c664fb8")
...@@ -112,4 +120,7 @@ public class DefaultData implements ApplicationRunner { ...@@ -112,4 +120,7 @@ public class DefaultData implements ApplicationRunner {
privacyPolicyReportService.add(oxygenPrivacyPolicyReport); privacyPolicyReportService.add(oxygenPrivacyPolicyReport);
} }
} }
\ No newline at end of file
package edu.prlab.tyler.iotgateway.cloud.controllers; 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.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.cloud.services.DeviceService; import edu.prlab.tyler.iotgateway.cloud.services.DeviceService;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService; import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@RestController @RestController
...@@ -37,9 +41,16 @@ public class DeviceController { ...@@ -37,9 +41,16 @@ public class DeviceController {
.orElseGet(()->ResponseEntity.noContent().build()); .orElseGet(()->ResponseEntity.noContent().build());
} }
// @GetMapping
// public ResponseEntity<Iterable<Device>> readDevices() {
// return Optional.of(deviceService.readll())
// .map(ResponseEntity::ok)
// .orElseGet(()->ResponseEntity.noContent().build());
// }
@GetMapping @GetMapping
public ResponseEntity<Iterable<Device>> readDevices() { public ResponseEntity<ArrayList<Device>> readDevices(SensitiveUser user) {
return Optional.of(deviceService.readll()) return Optional.of(Lists.newArrayList(deviceService.readll()))
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build()); .orElseGet(()->ResponseEntity.noContent().build());
} }
......
package edu.prlab.tyler.iotgateway.cloud.controllers; 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.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService; 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.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -11,12 +13,17 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -11,12 +13,17 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class GatewayController { public class GatewayController {
private PrivacyChoiceService privacyChoiceService; private PrivacyChoiceService privacyChoiceService;
private SensitiveUserService sensitiveUserService;
@Autowired @Autowired
public GatewayController(PrivacyChoiceService privacyChoiceService) { public GatewayController(PrivacyChoiceService privacyChoiceService,
SensitiveUserService sensitiveUserService) {
this.privacyChoiceService = privacyChoiceService; this.privacyChoiceService = privacyChoiceService;
this.sensitiveUserService = sensitiveUserService;
} }
@PostMapping("/choice") @PostMapping("/choice")
public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) { public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
return privacyChoiceService.add(privacyChoice) return privacyChoiceService.add(privacyChoice)
...@@ -24,4 +31,11 @@ public class GatewayController { ...@@ -24,4 +31,11 @@ public class GatewayController {
.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());
}
} }
package edu.prlab.tyler.iotgateway.cloud.pojo.auth; package edu.prlab.tyler.iotgateway.cloud.pojo.auth;
import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import java.math.BigInteger;
@Data @Data
...@@ -19,14 +14,16 @@ import java.math.BigInteger; ...@@ -19,14 +14,16 @@ import java.math.BigInteger;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Entity @Entity
public class User { public class SensitiveUser {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore private long id;
private int id;
private String account; private String username;
private String password; private String password;
private BigInteger publicKey; // private BigInteger publicKey;
private BigInteger privateKey; // private BigInteger privateKey;
private String address; // private String address;
} }
package edu.prlab.tyler.iotgateway.cloud.pojo.privacy; package edu.prlab.tyler.iotgateway.cloud.pojo.privacy;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.xml.internal.ws.api.pipe.ContentType; import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
...@@ -26,7 +25,7 @@ public class PrivacyContent { ...@@ -26,7 +25,7 @@ public class PrivacyContent {
@OneToOne(cascade = CascadeType.ALL) @OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
private User user; private SensitiveUser user;
@OneToOne @OneToOne
@JoinColumn(name = "device_id") @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; ...@@ -2,6 +2,7 @@ package edu.prlab.tyler.iotgateway.cloud.services;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public abstract class CrudService<T , ID> { public abstract class CrudService<T , ID> {
...@@ -12,6 +13,7 @@ public abstract class CrudService<T , ID> { ...@@ -12,6 +13,7 @@ public abstract class CrudService<T , ID> {
} }
public Optional<T> add(T pojo) { public Optional<T> add(T pojo) {
System.out.println(pojo.toString());
return Optional.of(repository.save(pojo)); 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; 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 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.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Icon; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Icon;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model;
...@@ -22,10 +24,9 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; ...@@ -22,10 +24,9 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import java.math.BigInteger; import java.util.ArrayList;
import java.util.Arrays; 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.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
...@@ -44,6 +45,7 @@ public class CloudHttpApiTest { ...@@ -44,6 +45,7 @@ public class CloudHttpApiTest {
.webAppContextSetup(context) .webAppContextSetup(context)
.build(); .build();
this.mapper = new ObjectMapper(); this.mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
} }
@Test @Test
...@@ -73,7 +75,6 @@ public class CloudHttpApiTest { ...@@ -73,7 +75,6 @@ public class CloudHttpApiTest {
.andDo(print()) .andDo(print())
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
device = mapper.readValue(result.getResponse().getContentAsString(), Device.class); device = mapper.readValue(result.getResponse().getContentAsString(), Device.class);
Assert.assertNotNull(device); Assert.assertNotNull(device);
...@@ -83,7 +84,6 @@ public class CloudHttpApiTest { ...@@ -83,7 +84,6 @@ public class CloudHttpApiTest {
.andDo(print()) .andDo(print())
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
device = mapper.readValue(result.getResponse().getContentAsString(), Device.class); device = mapper.readValue(result.getResponse().getContentAsString(), Device.class);
Assert.assertNotNull(device); Assert.assertNotNull(device);
...@@ -93,33 +93,31 @@ public class CloudHttpApiTest { ...@@ -93,33 +93,31 @@ public class CloudHttpApiTest {
.andDo(print()) .andDo(print())
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
Assert.assertNotNull(result); ArrayList<Device> devices = mapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<ArrayList<Device>>() {});
Assert.assertNotNull(devices);
result = mvc.perform(MockMvcRequestBuilders result = mvc.perform(MockMvcRequestBuilders
.get("/device/privacy/" + "a1252c49-4188-4e6d-a32e-66604c664fb8") .get("/device/privacy/" + device.getUdn())
.accept(MediaType.APPLICATION_JSON_UTF8)) .accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(print()) .andDo(print())
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
PrivacyPolicyReport report = mapper.readValue(result.getResponse().getContentAsString(), PrivacyPolicyReport.class); PrivacyPolicyReport report = mapper.readValue(result.getResponse().getContentAsString(), PrivacyPolicyReport.class);
Assert.assertNotNull(report); Assert.assertNotNull(report);
PrivacyChoice privacyChoice = PrivacyChoice.builder() PrivacyChoice privacyChoice = PrivacyChoice.builder()
.privacyContent(PrivacyContent.builder() .privacyContent(PrivacyContent.builder()
.user(User.builder() .user(SensitiveUser.builder()
.account("test") .username("test")
.password("test") .password("test")
.publicKey(BigInteger.ONE) .build())
.privateKey(BigInteger.ONE)
.address("test Address").build())
.device(device) .device(device)
.policy(report.getPolicies().get(0)) .policy(report.getPolicies().get(0))
.build()) .build())
.isAccepted(true) .isAccepted(true)
.build(); .build();
mapper.writeValueAsString(privacyChoice);
result = mvc.perform(MockMvcRequestBuilders result = mvc.perform(MockMvcRequestBuilders
.post("/choice") .post("/choice")
.contentType(MediaType.APPLICATION_JSON_UTF8) .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; package edu.prlab.tyler.iotgateway.contract;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.runners.MethodSorters; 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.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService; import org.web3j.protocol.http.HttpService;
import org.web3j.quorum.Quorum; import org.web3j.quorum.Quorum;
import org.web3j.tx.ClientTransactionManager;
import org.web3j.tx.gas.ContractGasProvider; import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.tx.gas.StaticGasProvider; import org.web3j.tx.gas.StaticGasProvider;
import java.math.BigInteger; import java.math.BigInteger;
...@@ -16,23 +21,22 @@ import java.math.BigInteger; ...@@ -16,23 +21,22 @@ import java.math.BigInteger;
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING) @FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class ContractTester { 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.ZERO,
BigInteger.valueOf(1000000000L) DefaultGasProvider.GAS_LIMIT
); );
@BeforeEach @BeforeClass
public void setUp() { public static void setUp() {
} }
@Test @Test
public void deployContract() throws Exception { public void test1deployDeviceContract() throws Exception {
System.out.println("test start"); // Credentials credentials = Credentials.create(privateKey);
Quorum quorum = Quorum.build(new HttpService(rpcUrl)); Quorum quorum = Quorum.build(new HttpService(rpcUrl));
Web3ClientVersion web3ClientVersion = quorum.web3ClientVersion().sendAsync().get(); Web3ClientVersion web3ClientVersion = quorum.web3ClientVersion().sendAsync().get();
...@@ -42,15 +46,34 @@ public class ContractTester { ...@@ -42,15 +46,34 @@ public class ContractTester {
String userAddress = quorum.ethAccounts().send().getAccounts().get(0); String userAddress = quorum.ethAccounts().send().getAccounts().get(0);
System.out.println("User : " + userAddress); System.out.println("User : " + userAddress);
// ClientTransactionManager manager = new ClientTransactionManager(quorum, userAddress); ClientTransactionManager manager = new ClientTransactionManager(quorum, userAddress);
// Credentials credentials = Credentials.create(privateKey);
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 { ...@@ -33,6 +33,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation project(":cloud") implementation project(":cloud")
implementation project(":contract")
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2' runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
......
...@@ -7,3 +7,6 @@ rootProject.name = 'gateway' ...@@ -7,3 +7,6 @@ rootProject.name = 'gateway'
include ":cloud" include ":cloud"
project(":cloud").projectDir = file("../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; 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.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService; import edu.prlab.tyler.iotgateway.gateway.services.PrivacyService;
...@@ -15,6 +16,12 @@ public class GatewayController { ...@@ -15,6 +16,12 @@ public class GatewayController {
this.privacyService = privacyService; this.privacyService = privacyService;
} }
// TODO
// @GetMapping("")
// public ResponseEntity<Iterable<Device>> readDevicesOfUser() {
//
// }
@GetMapping("/privacy/{UDN}") @GetMapping("/privacy/{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)
...@@ -28,5 +35,4 @@ public class GatewayController { ...@@ -28,5 +35,4 @@ public class GatewayController {
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(()->ResponseEntity.noContent().build()); .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 { ...@@ -14,13 +14,17 @@ public class PrivacyService {
private RestTemplate template; private RestTemplate template;
@Value("${iotgateway.cloud.address}")
private String cloudAddress; private String cloudAddress;
public PrivacyService() { public PrivacyService() {
this.template = new RestTemplate(); this.template = new RestTemplate();
} }
@Value("${iotgateway.cloud.address}")
private void setCloudAddress(String cloudAddress) {
this.cloudAddress = cloudAddress;
}
public Optional<PrivacyPolicyReport> getRelatedPrivacyPolicies(String Udn) { public Optional<PrivacyPolicyReport> getRelatedPrivacyPolicies(String Udn) {
return Optional.ofNullable(template.getForObject(cloudAddress + "/device/privacy/" + Udn, PrivacyPolicyReport.class)); return Optional.ofNullable(template.getForObject(cloudAddress + "/device/privacy/" + Udn, PrivacyPolicyReport.class));
} }
......
package edu.prlab.tyler.iotgateway.gateway; package edu.prlab.tyler.iotgateway.gateway;
import com.fasterxml.jackson.databind.ObjectMapper; 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.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;
...@@ -20,8 +20,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; ...@@ -20,8 +20,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; 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.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
...@@ -45,6 +43,10 @@ public class GatewayHttpApiTest { ...@@ -45,6 +43,10 @@ public class GatewayHttpApiTest {
@Test @Test
public void getPrivacyPolicyAndSetChoice() throws Exception{ public void getPrivacyPolicyAndSetChoice() throws Exception{
//取得裝置清單
//拿取隱私政策
MvcResult result = mvc.perform(MockMvcRequestBuilders MvcResult result = mvc.perform(MockMvcRequestBuilders
.get("/privacy/" + "a1252c49-4188-4e6d-a32e-66604c664fb8") .get("/privacy/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.accept(MediaType.APPLICATION_JSON_UTF8)) .accept(MediaType.APPLICATION_JSON_UTF8))
...@@ -52,25 +54,21 @@ public class GatewayHttpApiTest { ...@@ -52,25 +54,21 @@ public class GatewayHttpApiTest {
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
PrivacyPolicyReport report = mapper.readValue(result.getResponse().getContentAsString(), PrivacyPolicyReport.class); PrivacyPolicyReport report = mapper.readValue(result.getResponse().getContentAsString(), PrivacyPolicyReport.class);
Assert.assertNotNull(report); Assert.assertNotNull(report);
//表達隱私偏好
Device device = report.getDevice(); Device device = report.getDevice();
PrivacyChoice privacyChoice = PrivacyChoice.builder() PrivacyChoice privacyChoice = PrivacyChoice.builder()
.privacyContent(PrivacyContent.builder() .privacyContent(PrivacyContent.builder()
.user(User.builder() .user(SensitiveUser.builder()
.account("test") .username("test")
.password("test") .password("test")
.publicKey(BigInteger.ONE) .build())
.privateKey(BigInteger.ONE)
.address("test Address").build())
.device(device) .device(device)
.policy(report.getPolicies().get(0)) .policy(report.getPolicies().get(0))
.build()) .build())
.isAccepted(true) .isAccepted(true)
.build(); .build();
mapper.writeValueAsString(privacyChoice); mapper.writeValueAsString(privacyChoice);
result = mvc.perform(MockMvcRequestBuilders result = mvc.perform(MockMvcRequestBuilders
.post("/choice") .post("/choice")
...@@ -80,8 +78,6 @@ public class GatewayHttpApiTest { ...@@ -80,8 +78,6 @@ public class GatewayHttpApiTest {
.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