owner fix
This commit is contained in:
parent
2d0e06bfce
commit
ef9daa5d4f
3 changed files with 121 additions and 70 deletions
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -32,6 +33,12 @@
|
|||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
||||
@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();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue