add product id

This commit is contained in:
Tibor Tarnai 2019-12-11 13:17:40 +01:00
parent f70c18694c
commit 620aac9b67
6 changed files with 37 additions and 43 deletions

View file

@ -1,5 +1,8 @@
package com.sap.tamagotchi.controller; package com.sap.tamagotchi.controller;
import static com.sap.tamagotchi.model.Color.BLUE;
import static com.sap.tamagotchi.model.Color.PINK;
import static com.sap.tamagotchi.model.Color.PURPLE;
import static com.sap.tamagotchi.model.Color.RED; import static com.sap.tamagotchi.model.Color.RED;
import static com.sap.tamagotchi.model.Color.YELLOW; import static com.sap.tamagotchi.model.Color.YELLOW;
import static org.springframework.http.ResponseEntity.ok; import static org.springframework.http.ResponseEntity.ok;
@ -32,8 +35,18 @@ public class DeviceController {
private static Color mapColor(String productId) { private static Color mapColor(String productId) {
switch (productId) { switch (productId) {
case "66VCHSJNUP": case "0000000001":
return BLUE;
case "0000000002":
return RED; return RED;
case "0000000003":
return YELLOW;
case "0000000004":
return BLUE;
case "0000000005":
return PURPLE;
case "0000000006":
return PINK;
default: default:
return YELLOW; return YELLOW;
} }
@ -53,7 +66,7 @@ public class DeviceController {
public ResponseEntity createDevice(@RequestBody Collection<CreateDevicePayload> payload) { public ResponseEntity createDevice(@RequestBody Collection<CreateDevicePayload> payload) {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
for (CreateDevicePayload p : payload) { for (CreateDevicePayload p : payload) {
devices.add(tamagotchiService.createDevice(new Device(p.getOwner(), mapColor(p.getProductId())))); devices.add(tamagotchiService.createDevice(new Device(p.getProductId(), p.getOwner(), mapColor(p.getProductId()))));
} }
return ok(devices); return ok(devices);
} }

View file

@ -1,5 +1,5 @@
package com.sap.tamagotchi.model; package com.sap.tamagotchi.model;
public enum Color { public enum Color {
RED, YELLOW, BLUE, GREEN RED, YELLOW, BLUE, GREEN, PURPLE, PINK
} }

View file

@ -14,6 +14,8 @@ public class Device {
@JsonProperty("id") @JsonProperty("id")
private final String id = UUID.randomUUID().toString(); private final String id = UUID.randomUUID().toString();
@JsonProperty("productId")
private final String productId;
@JsonProperty("owner") @JsonProperty("owner")
private final String owner; private final String owner;
@JsonProperty("color") @JsonProperty("color")
@ -24,7 +26,8 @@ public class Device {
private int healthScore = 100; private int healthScore = 100;
private final Queue<IoTMessage> messages = new ConcurrentLinkedQueue<>(); private final Queue<IoTMessage> messages = new ConcurrentLinkedQueue<>();
public Device(String owner, Color color) { public Device(String productId, String owner, Color color) {
this.productId = productId;
this.owner = owner; this.owner = owner;
this.color = color; this.color = color;
} }
@ -34,6 +37,11 @@ public class Device {
return id; return id;
} }
@JsonProperty("productId")
public String getProductId() {
return productId;
}
@JsonProperty("owner") @JsonProperty("owner")
public String getOwner() { public String getOwner() {
return owner; return owner;
@ -70,9 +78,9 @@ public class Device {
if (healthScore < 1) { if (healthScore < 1) {
healthScore = 0; healthScore = 0;
messages.add(new DeviceEvent(id, owner, color, born, healthScore, oldScore, Instant.now())); messages.add(new DeviceEvent(id, productId, owner, color, born, healthScore, oldScore, Instant.now()));
} else } else
messages.add(new DeviceEvent(id, owner, color, born, healthScore, null, Instant.now())); messages.add(new DeviceEvent(id, productId, owner, color, born, healthScore, null, Instant.now()));
} }
@JsonIgnore @JsonIgnore

View file

@ -11,6 +11,8 @@ public class DeviceEvent implements IoTMessage {
@JsonProperty("id") @JsonProperty("id")
private final String id; private final String id;
@JsonProperty("productId")
private final String productId;
@JsonProperty("owner") @JsonProperty("owner")
private final String owner; private final String owner;
@JsonProperty("color") @JsonProperty("color")
@ -24,8 +26,9 @@ public class DeviceEvent implements IoTMessage {
@JsonProperty("eventTime") @JsonProperty("eventTime")
private final Instant eventTime; private final Instant eventTime;
public DeviceEvent(String id, String owner, Color color, Instant born, Integer healthScore, Integer lastHealthScore, Instant eventTime) { public DeviceEvent(String id, String productId, String owner, Color color, Instant born, Integer healthScore, Integer lastHealthScore, Instant eventTime) {
this.id = id; this.id = id;
this.productId = productId;
this.owner = owner; this.owner = owner;
this.color = color; this.color = color;
this.born = born; this.born = born;
@ -39,6 +42,11 @@ public class DeviceEvent implements IoTMessage {
return id; return id;
} }
@JsonProperty("productId")
public String getProductId() {
return productId;
}
@JsonProperty("owner") @JsonProperty("owner")
public String getOwner() { public String getOwner() {
return owner; return owner;

View file

@ -92,7 +92,7 @@ public class TamagotchiService {
private void sendTamagotchiDefunctNotifiction(String id) { private void sendTamagotchiDefunctNotifiction(String id) {
Device device = deviceRegistry.get(id); _ Device device = deviceRegistry.get(id);
if (device == null || device.getId() == null || device.getOwner() == null) { if (device == null || device.getId() == null || device.getOwner() == null) {
return; return;
} }

View file

@ -1,40 +1,5 @@
/**
* Copyright (c) 2019, SAP SE, All rights reserved.
*/
package com.sap.tamagotchi.model; package com.sap.tamagotchi.model;
import static java.util.Arrays.asList;
import static org.mockito.Mockito.when;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import com.sap.tamagotchi.service.TamagotchiService;;
@RunWith(SpringRunner.class)
@SpringBootTest
public class OwnerTest { public class OwnerTest {
@Autowired
Owner owner;
@MockBean
TamagotchiService tamagotchiService;
Collection<Device> mockDevices;
// @Before
// public void init() {
// MockitoAnnotations.initMocks(this);
// }
@Test
public void testIt() {
when(tamagotchiService.getDevices()).thenReturn(asList(new Device("elisa@sap.com", Color.BLUE)));
owner.setData();
}
} }