Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
basic mocking implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
Harald Meyer committed Dec 23, 2022
1 parent 8180cbb commit d423721
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
12 changes: 10 additions & 2 deletions ax-mm-hubconnector/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="test/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="test/main/java">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
Expand Down
9 changes: 7 additions & 2 deletions ax-mm-hubconnector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.0.3</version>
</dependency>

</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.adamos.hubconnector.services;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Collections;

import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;

import com.adamos.hubconnector.CustomProperties;
import com.adamos.hubconnector.model.HubConnectorGlobalSettings;
import com.adamos.hubconnector.model.events.EventMapping;
import com.adamos.hubconnector.model.hub.EquipmentDTO;
import com.cumulocity.model.idtype.GId;
import com.cumulocity.rest.representation.event.EventRepresentation;
import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;

class EventRulesServiceTest {

@Mock
CumulocityService cumulocityService;

@Mock
private HubConnectorService hubConnectorService;

@Mock
private HubService hubService;


@InjectMocks
EventRulesService ers;


@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
}

@Test
void testMapEventsNoDevices() {
EventMapping mapping = new EventMapping();
mapping.setId("123");
ers.mapEvents(mapping, Collections.emptyList());
verifyNoInteractions(cumulocityService);
}

@Test
void testMapEventsOneDeviceOneEvent() {
EventMapping mapping = new EventMapping();
mapping.setId("m123");
mapping.setC8yEventType("cet123");
mapping.setAdamosEventType("aet123");
mapping.setC8yFragments(Collections.emptyList());
mapping.setEnabled(true);
ManagedObjectRepresentation device = device("d123");
when(
cumulocityService.getEvents(eq(device), eq(mapping.getC8yEventType()), any())
).thenReturn(
Collections.singletonList(event(mapping.getC8yEventType(), device))
);
when(
hubConnectorService.getGlobalSettings()
).thenReturn(
new HubConnectorGlobalSettings(true)
);
ers.mapEvents(mapping, Collections.singletonList(device));
}

private EventRepresentation event(String type, ManagedObjectRepresentation source) {
EventRepresentation event = new EventRepresentation();
event.setType(type);
event.setSource(source);
event.setCreationDateTime(DateTime.now());
return event;
}

private ManagedObjectRepresentation device(String id) {
ManagedObjectRepresentation mo = new ManagedObjectRepresentation();
mo.setId(GId.asGId(id));
EquipmentDTO hubdata = new EquipmentDTO();
hubdata.setUuid("uuid123");
mo.set(hubdata, CustomProperties.HUB_DATA);
return mo;
}
}

0 comments on commit d423721

Please sign in to comment.