[SECARSP-115] *change device info registration, add pageable for get all devices
authorm.dalakov <m.dalakov@samsung.com>
Tue, 27 Feb 2018 13:44:25 +0000 (15:44 +0200)
committerDmytro Lomtiev <d.lomtev@samsung.com>
Fri, 16 Mar 2018 14:16:49 +0000 (16:16 +0200)
Change-Id: I7b5c2beb0dad3b8c538a6dd079045b6cff816464

12 files changed:
server/domain-jdl.jh
server/src/main/java/com/samsung/samserver/domain/Device.java
server/src/main/java/com/samsung/samserver/service/DeviceService.java
server/src/main/java/com/samsung/samserver/service/impl/DeviceServiceImpl.java
server/src/main/java/com/samsung/samserver/web/rest/controller/IRestDashboard.java
server/src/main/java/com/samsung/samserver/web/rest/controller/IRestDevice.java
server/src/main/java/com/samsung/samserver/web/rest/controller/impl/RestDashboard.java
server/src/main/java/com/samsung/samserver/web/rest/controller/impl/RestDevice.java
server/src/main/java/com/samsung/samserver/web/rest/service/GetDevicesUIRestService.java
server/src/main/java/com/samsung/samserver/web/rest/service/RegisterDeviceRestService.java
server/src/main/resources/config/liquibase/changelog/20180210112805_added_entity_Device.xml
server/src/test/java/com/samsung/samserver/web/rest/crud/DeviceResourceTest.java

index 94053e0..1a33cd1 100644 (file)
@@ -22,7 +22,7 @@ entity Device {
     duid String,
     ctime Instant,
     model String,
-    location String,
+    ipaddr String,
     sn String,
     descr String,
     locked String,
index b46b5a5..e978a23 100644 (file)
@@ -37,18 +37,12 @@ public class Device implements Serializable {
     @Column(name = "model")
     private String model;
 
-    @Column(name = "location")
-    private String location;
+    @Column(name = "ipaddr")
+    private String ipaddr;
 
     @Column(name = "sn")
     private String sn;
 
-    @Column(name = "descr")
-    private String descr;
-
-    @Column(name = "jhi_locked")
-    private String locked;
-
     @Column(name = "sw")
     private String sw;
 
@@ -58,6 +52,12 @@ public class Device implements Serializable {
     @Column(name = "osver")
     private String osver;
 
+    @Column(name = "descr")
+    private String descr;
+
+    @Column(name = "jhi_locked")
+    private String locked;
+
     @OneToMany(mappedBy = "device")
     @JsonIgnore
     private Set<Report> reports = new HashSet<>();
@@ -69,23 +69,6 @@ public class Device implements Serializable {
     @JsonIgnore
     private Policy policy;
 
-    public Device() {
-    }
-
-    public Device(String duid, String model, String location, String sn, String descr,
-                  DeviceType dtype, String sw, String osname, String osver) {
-        this.duid = duid;
-        this.model = model;
-        this.location = location;
-        this.sn = sn;
-        this.descr = descr;
-        this.dtype = dtype;
-        this.ctime = Instant.now();
-        this.sw = sw;
-        this.osname = osname;
-        this.osver = osver;
-    }
-
     public Device duid(String duid) {
         this.duid = duid;
         return this;
@@ -101,8 +84,8 @@ public class Device implements Serializable {
         return this;
     }
 
-    public Device location(String location) {
-        this.location = location;
+    public Device ipaddr(String ipaddr) {
+        this.ipaddr = ipaddr;
         return this;
     }
 
@@ -111,6 +94,21 @@ public class Device implements Serializable {
         return this;
     }
 
+    public Device sw(String sw) {
+        this.sw = sw;
+        return this;
+    }
+
+    public Device osname(String osname) {
+        this.osname = osname;
+        return this;
+    }
+
+    public Device osver(String osver) {
+        this.osver = osver;
+        return this;
+    }
+
     public Device descr(String descr) {
         this.descr = descr;
         return this;
index b3e469d..9eab144 100644 (file)
@@ -6,6 +6,9 @@
 package com.samsung.samserver.service;
 
 import com.samsung.samserver.domain.Device;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
 import java.util.List;
 import java.util.Optional;
 
@@ -29,6 +32,14 @@ public interface DeviceService {
      */
     List<Device> findAll();
 
+
+    /**
+     * Get page of all the devices.
+     *
+     * @return the page of entities
+     */
+    Page<Device> findAll(Pageable pageable);
+
     /**
      * Get the "id" device.
      *
index 4653f11..e9b96e6 100644 (file)
@@ -11,6 +11,8 @@ import com.samsung.samserver.repository.DeviceRepository;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -57,6 +59,18 @@ public class DeviceServiceImpl implements DeviceService {
     }
 
     /**
+     * Get page of all the devices.
+     *
+     * @return the page of entities
+     */
+    @Override
+    @Transactional(readOnly = true)
+    public Page<Device> findAll(Pageable pageable) {
+        log.debug("Request to get page of all Devices"); //NOSONAR
+        return deviceRepository.findAll(pageable);
+    }
+
+    /**
      * Get one device by id.
      *
      * @param id the id of the entity
index c9bcd04..3d779b5 100644 (file)
@@ -7,6 +7,7 @@ package com.samsung.samserver.web.rest.controller;
 
 import com.samsung.samserver.web.rest.service.*;
 import com.samsung.samserver.web.rest.errors.UserServiceError;
+import org.springframework.data.domain.Pageable;
 import org.springframework.http.*;
 import org.springframework.web.bind.annotation.*;
 import io.swagger.annotations.*;
@@ -35,7 +36,7 @@ public interface IRestDashboard {
     })
     @RequestMapping(value ="/devices", method = RequestMethod.GET)
     @ResponseStatus(HttpStatus.OK)
-    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices();
+    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices(Pageable pageable);
 
 
     /**
index e99e4be..8b7e119 100644 (file)
@@ -10,6 +10,7 @@ import org.springframework.http.*;
 import org.springframework.web.bind.annotation.*;
 import io.swagger.annotations.*;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
 import java.util.List;
 
@@ -38,7 +39,7 @@ public interface IRestDevice {
     @RequestMapping(value ="/register-device", method = RequestMethod.POST)
     @ResponseStatus(HttpStatus.OK)
     public ResponseEntity<String> registerDevice(
-        @ApiParam(value = "Device info", required = true) @Valid @RequestBody RegisterDeviceRestService.DInfo dInfo);
+        @ApiParam(value = "Device info", required = true) @Valid @RequestBody RegisterDeviceRestService.DInfo dInfo, HttpServletRequest request);
 
 
     /*
index 24187fe..8b02663 100644 (file)
@@ -9,6 +9,7 @@ import com.samsung.samserver.service.DeviceService;
 import com.samsung.samserver.web.rest.controller.IRestDashboard;
 import com.samsung.samserver.web.rest.service.*;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Pageable;
 import org.springframework.http.*;
 import org.springframework.web.bind.annotation.*;
 import javax.validation.Valid;
@@ -41,8 +42,8 @@ public class RestDashboard  implements IRestDashboard {
      * @return the ResponseEntity with status 200 (OK) and the list of devices in body
      */
     @Override
-    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices() {
-        return getDevicesUIRestService.getAllDevices();
+    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices(Pageable pageable) {
+        return getDevicesUIRestService.getAllDevices(pageable);
     }
 
     /**
index ed3af43..fadc48d 100644 (file)
@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
 import java.util.List;
 
@@ -33,8 +34,8 @@ public class RestDevice implements IRestDevice {
     private SendDataRestService sendDataRestService;
 
     @Override
-    public ResponseEntity<String> registerDevice(@Valid @RequestBody RegisterDeviceRestService.DInfo dInfo) {
-        return registerDeviceRestService.registerDevice(dInfo);
+    public ResponseEntity<String> registerDevice(@Valid @RequestBody RegisterDeviceRestService.DInfo dInfo, HttpServletRequest request) {
+        return registerDeviceRestService.registerDevice(dInfo, request);
     }
 
     @Override
index 50ff4c6..2564e58 100644 (file)
@@ -8,7 +8,9 @@ package com.samsung.samserver.web.rest.service;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.samsung.samserver.domain.Device;
 import com.samsung.samserver.service.DeviceService;
+import com.samsung.samserver.web.rest.util.PaginationUtil;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.*;
 import org.springframework.stereotype.Service;
 import org.springframework.http.*;
 import org.slf4j.*;
@@ -30,9 +32,9 @@ public class GetDevicesUIRestService {
     @Autowired
     private DeviceService deviceService;
 
-    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices() {
-        log.debug("UI request to get all devices"); //NO_SONAR
-        List<Device> devices = deviceService.findAll();
+    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices(Pageable pageable) {
+        log.debug("UI request to get all devices"); //NOSONAR
+        Page<Device> devices = deviceService.findAll(pageable);
         List<GetDevicesUIRestService.UIDevice> uiDevices = new ArrayList<>();
         for (Device device: devices) {
             UIDevice uiDevice = new UIDevice();
@@ -40,7 +42,7 @@ public class GetDevicesUIRestService {
             uiDevice.setDuid(device.getDuid());
             uiDevice.setCtime(device.getCtime());
             uiDevice.setModel(device.getModel());
-            uiDevice.setLocation("UA"/*device.getLocation()*/);
+            uiDevice.setIpaddr(device.getIpaddr());
             uiDevice.setSn(device.getSn());
             uiDevice.setDescr(device.getDescr());
             uiDevice.setLocked(device.getLocked()!=null && !device.getLocked().equals("0") ? 1:0);
@@ -48,9 +50,9 @@ public class GetDevicesUIRestService {
             uiDevice.setUiDeviceType(new UIDeviceType (device.getDtype().getName(), device.getDtype().getDescr()));
             uiDevices.add(uiDevice);
         }
-        final HttpHeaders httpHeaders = new HttpHeaders();
-        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
-        return new ResponseEntity<>(uiDevices , httpHeaders, HttpStatus.OK);
+        Page<UIDevice> page = new PageImpl(uiDevices, pageable, devices.getTotalPages());
+        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/dashboard/devices");
+        return new ResponseEntity<>(page.getContent() , headers, HttpStatus.OK);
     }
 
     @Getter @Setter @ToString
@@ -67,8 +69,8 @@ public class GetDevicesUIRestService {
         @JsonProperty("model")
         private String model;
 
-        @JsonProperty("location")
-        private String location;
+        @JsonProperty("ipaddr")
+        private String ipaddr;
 
         @JsonProperty("sn")
         private String sn;
index cb5cfd9..8ed8572 100644 (file)
@@ -13,8 +13,11 @@ import com.fasterxml.jackson.annotation.JsonProperty;
 import com.samsung.samserver.domain.*;
 import com.samsung.samserver.service.*;
 import com.samsung.samserver.web.rest.errors.DeviceServiceError;
+
+import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
 import javax.validation.constraints.NotNull;
+import java.time.Instant;
 import java.util.Optional;
 import lombok.*;
 
@@ -37,7 +40,7 @@ public class RegisterDeviceRestService {
         this.deviceService = deviceService;
     }
 
-    public ResponseEntity<String> registerDevice(@Valid @RequestBody DInfo dInfo) {
+    public ResponseEntity<String> registerDevice(@Valid @RequestBody DInfo dInfo, HttpServletRequest request) {
         log.debug("request to register device : {}", dInfo);
 
         String dtype = dInfo.getType().toLowerCase();
@@ -50,10 +53,17 @@ public class RegisterDeviceRestService {
         String duid = deviceService.getDeviceUID(dtype, dInfo.getModel(), dInfo.getSn());
         deviceService.findOne(duid).ifPresent(d -> {throw new DeviceServiceError.DeviceAlreadyRegisteredException();});
 
-        Device result = deviceService.save(
-            new Device(duid, dInfo.getModel(), dInfo.getLocation(), dInfo.getSn(), dInfo.getDescr(), dt.get(),
-                dInfo.getSw(), dInfo.getOsname(), dInfo.getOsver()));
-
+        Device result = deviceService.save(new Device()
+            .duid(duid)
+            .ctime(Instant.now())
+            .model(dInfo.getModel())
+            .sn(dInfo.getSn())
+            .sw(dInfo.getSw())
+            .osname(dInfo.getOsname())
+            .osver(dInfo.getOsver())
+            .ipaddr(request.getRemoteAddr())
+            .descr(dInfo.getDescr())
+            .dtype(dt.get()));
         return ResponseEntity.ok()
             .headers(new HttpHeaders())
             .body(result.getDuid());
@@ -72,8 +82,8 @@ public class RegisterDeviceRestService {
         @NotNull
         private String model;
 
-        @JsonProperty("location")
-        private String location;
+        @JsonProperty("ipaddr")
+        private String ipaddr;
 
         @JsonProperty("sn")
         @NotNull
index db69327..f43a2c4 100644 (file)
@@ -33,7 +33,7 @@
                 <constraints nullable="true" />
             </column>
 
-            <column name="location" type="varchar(255)">
+            <column name="ipaddr" type="varchar(255)">
                 <constraints nullable="true" />
             </column>
 
index afddc98..3668e84 100644 (file)
@@ -56,8 +56,8 @@ public class DeviceResourceTest {
     private static final String DEFAULT_MODEL = "AAAAAAAAAA";
     private static final String UPDATED_MODEL = "BBBBBBBBBB";
 
-    private static final String DEFAULT_LOCATION = "AAAAAAAAAA";
-    private static final String UPDATED_LOCATION = "BBBBBBBBBB";
+    private static final String DEFAULT_IPADDR = "AAAAAAAAAA";
+    private static final String UPDATED_IPADDR = "BBBBBBBBBB";
 
     private static final String DEFAULT_SN = "AAAAAAAAAA";
     private static final String UPDATED_SN = "BBBBBBBBBB";
@@ -112,7 +112,7 @@ public class DeviceResourceTest {
             .duid(DEFAULT_DUID)
             .ctime(DEFAULT_CTIME)
             .model(DEFAULT_MODEL)
-            .location(DEFAULT_LOCATION)
+            .ipaddr(DEFAULT_IPADDR)
             .sn(DEFAULT_SN)
             .descr(DEFAULT_DESCR)
             .locked(DEFAULT_LOCKED);
@@ -142,7 +142,7 @@ public class DeviceResourceTest {
         assertThat(testDevice.getDuid()).isEqualTo(DEFAULT_DUID);
         assertThat(testDevice.getCtime()).isEqualTo(DEFAULT_CTIME);
         assertThat(testDevice.getModel()).isEqualTo(DEFAULT_MODEL);
-        assertThat(testDevice.getLocation()).isEqualTo(DEFAULT_LOCATION);
+        assertThat(testDevice.getIpaddr()).isEqualTo(DEFAULT_IPADDR);
         assertThat(testDevice.getSn()).isEqualTo(DEFAULT_SN);
         assertThat(testDevice.getDescr()).isEqualTo(DEFAULT_DESCR);
         assertThat(testDevice.getLocked()).isEqualTo(DEFAULT_LOCKED);
@@ -181,7 +181,7 @@ public class DeviceResourceTest {
             .andExpect(jsonPath("$.[*].duid").value(hasItem(DEFAULT_DUID.toString())))
             .andExpect(jsonPath("$.[*].ctime").value(hasItem(DEFAULT_CTIME.toString())))
             .andExpect(jsonPath("$.[*].model").value(hasItem(DEFAULT_MODEL.toString())))
-            .andExpect(jsonPath("$.[*].location").value(hasItem(DEFAULT_LOCATION.toString())))
+            .andExpect(jsonPath("$.[*].ipaddr").value(hasItem(DEFAULT_IPADDR.toString())))
             .andExpect(jsonPath("$.[*].sn").value(hasItem(DEFAULT_SN.toString())))
             .andExpect(jsonPath("$.[*].descr").value(hasItem(DEFAULT_DESCR.toString())))
             .andExpect(jsonPath("$.[*].locked").value(hasItem(DEFAULT_LOCKED.toString())));
@@ -201,7 +201,7 @@ public class DeviceResourceTest {
             .andExpect(jsonPath("$.duid").value(DEFAULT_DUID.toString()))
             .andExpect(jsonPath("$.ctime").value(DEFAULT_CTIME.toString()))
             .andExpect(jsonPath("$.model").value(DEFAULT_MODEL.toString()))
-            .andExpect(jsonPath("$.location").value(DEFAULT_LOCATION.toString()))
+            .andExpect(jsonPath("$.ipaddr").value(DEFAULT_IPADDR.toString()))
             .andExpect(jsonPath("$.sn").value(DEFAULT_SN.toString()))
             .andExpect(jsonPath("$.descr").value(DEFAULT_DESCR.toString()))
             .andExpect(jsonPath("$.locked").value(DEFAULT_LOCKED.toString()));
@@ -231,7 +231,7 @@ public class DeviceResourceTest {
             .duid(UPDATED_DUID)
             .ctime(UPDATED_CTIME)
             .model(UPDATED_MODEL)
-            .location(UPDATED_LOCATION)
+            .ipaddr(UPDATED_IPADDR)
             .sn(UPDATED_SN)
             .descr(UPDATED_DESCR)
             .locked(UPDATED_LOCKED);
@@ -248,7 +248,7 @@ public class DeviceResourceTest {
         assertThat(testDevice.getDuid()).isEqualTo(UPDATED_DUID);
         assertThat(testDevice.getCtime()).isEqualTo(UPDATED_CTIME);
         assertThat(testDevice.getModel()).isEqualTo(UPDATED_MODEL);
-        assertThat(testDevice.getLocation()).isEqualTo(UPDATED_LOCATION);
+        assertThat(testDevice.getIpaddr()).isEqualTo(UPDATED_IPADDR);
         assertThat(testDevice.getSn()).isEqualTo(UPDATED_SN);
         assertThat(testDevice.getDescr()).isEqualTo(UPDATED_DESCR);
         assertThat(testDevice.getLocked()).isEqualTo(UPDATED_LOCKED);