diff --git a/src/tamagotchi-service/pom.xml b/src/tamagotchi-service/pom.xml index d130b8b..1800402 100644 --- a/src/tamagotchi-service/pom.xml +++ b/src/tamagotchi-service/pom.xml @@ -1,69 +1,76 @@ - - 4.0.0 + + 4.0.0 - com.sap - tamagotchi-service - 0.1.0 + com.sap + tamagotchi-service + 0.1.0 - - 1.8 - 1.8 - 1.8 - + + 1.8 + 1.8 + 1.8 + - - org.springframework.boot - spring-boot-starter-parent - 2.1.6.RELEASE - + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + - - - - com.google.cloud - libraries-bom - 2.9.0 - pom - import - - - + + + + com.google.cloud + libraries-bom + 2.9.0 + pom + import + + + - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-test - test - - - com.google.cloud - google-cloud-pubsub - - - com.jayway.jsonpath - json-path - test - - + + + org.springframework.boot + spring-boot-starter-test + + test + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + com.google.cloud + google-cloud-pubsub + + + com.jayway.jsonpath + json-path + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - com.google.appengine - appengine-maven-plugin - 1.9.77 - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.google.appengine + appengine-maven-plugin + 1.9.77 + + + diff --git a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java index 87d1be8..0f8ad6f 100644 --- a/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java +++ b/src/tamagotchi-service/src/main/java/com/sap/tamagotchi/model/Owner.java @@ -3,14 +3,15 @@ */ package com.sap.tamagotchi.model; -import com.sap.tamagotchi.service.TamagotchiService; +import static com.sap.tamagotchi.service.TamagotchiService.DEVICE_EVENT_PROCESSOR_SCHEDULE; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; - -import static com.sap.tamagotchi.service.TamagotchiService.DEVICE_EVENT_PROCESSOR_SCHEDULE; +import com.sap.tamagotchi.service.TamagotchiService; @Service +@EnableScheduling public class Owner { private TamagotchiService tamagotchiService; @@ -23,13 +24,23 @@ public class Owner { @Scheduled(fixedDelay = DEVICE_EVENT_PROCESSOR_SCHEDULE) public void setData() { for (Device d : tamagotchiService.getDevices()) { - double random = Math.random(); + + String userName = d.getOwner().substring(0, d.getOwner().indexOf("@")).toUpperCase(); + + int careAboutThePet = 0; + + for (int i = 0; i < userName.length(); i++) { + if (d.getColor().toString().indexOf(userName.charAt(i)) != -1) { + careAboutThePet++; + } + } + Care care = new Care(); - if (random <= 0.5) { - care.setFeed(-(int) (random * 10)); + if (careAboutThePet == 0) { + care.setFeed(-5); } else { - care.setFeed((int) (random * 10)); + care.setFeed(5); } tamagotchiService.takeCare(d.getId(), care); } diff --git a/src/tamagotchi-service/src/test/java/com/sap/tamagotchi/model/OwnerTest.java b/src/tamagotchi-service/src/test/java/com/sap/tamagotchi/model/OwnerTest.java index bbb7bcb..35e2820 100644 --- a/src/tamagotchi-service/src/test/java/com/sap/tamagotchi/model/OwnerTest.java +++ b/src/tamagotchi-service/src/test/java/com/sap/tamagotchi/model/OwnerTest.java @@ -1,7 +1,40 @@ +/** + * Copyright (c) 2019, SAP SE, All rights reserved. + */ package com.sap.tamagotchi.model; -import static org.junit.jupiter.api.Assertions.*; +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;; -class OwnerTest { +@RunWith(SpringRunner.class) +@SpringBootTest +public class OwnerTest { -} \ No newline at end of file + @Autowired + Owner owner; + + @MockBean + TamagotchiService tamagotchiService; + + Collection 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(); + + } +}