Skip to content

Commit

Permalink
feat: added gnssType to position rest service (eclipse-kura#5429)
Browse files Browse the repository at this point in the history
* feat: added gnssType to position rest service

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* refactor: modified how the gnsstype info is retrieved

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

---------

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>
  • Loading branch information
sfiorani authored Oct 15, 2024
1 parent 24d329c commit c82bbd2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 45 deletions.
2 changes: 1 addition & 1 deletion kura/distrib/config/kura.build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ org.eclipse.kura.rest.network.configuration.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.inventory.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.command.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.packages.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.position.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.position.provider.version=2.1.0-SNAPSHOT
org.eclipse.kura.rest.security.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.identity.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.service.listing.provider.version=2.0.0-SNAPSHOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.eclipse.kura.rest.position.provider
Bundle-SymbolicName: org.eclipse.kura.rest.position.provider;singleton:=true
Bundle-Version: 2.0.0.qualifier
Bundle-Version: 2.1.0.qualifier
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Bundle-ClassPath: .
Bundle-ActivationPolicy: lazy
Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.rest.position.provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@

<artifactId>org.eclipse.kura.rest.position.provider</artifactId>
<packaging>eclipse-plugin</packaging>
<version>2.0.0-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -84,7 +84,7 @@ public void unsetRequestHandlerRegistry(final RequestHandlerRegistry registry) {
@Produces(MediaType.APPLICATION_JSON)
public PositionDTO getPosition() {
if (positionServiceImpl.isLocked()) {
return new PositionDTO(positionServiceImpl.getPosition());
return new PositionDTO(positionServiceImpl.getPosition(), positionServiceImpl.getGnssType());
}

throw DefaultExceptionHandler.toWebApplicationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -12,6 +12,10 @@
*******************************************************************************/
package org.eclipse.kura.rest.position.api;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.kura.position.GNSSType;
import org.osgi.util.position.Position;

public class PositionDTO {
Expand All @@ -21,8 +25,9 @@ public class PositionDTO {
private Double altitude;
private Double speed;
private Double track;
private Set<String> gnssType;

public PositionDTO(Position position) {
public PositionDTO(Position position, Set<GNSSType> gnssTypeSet) {
if (position.getLongitude() != null) {
this.longitude = Math.toDegrees(position.getLongitude().getValue());
}
Expand All @@ -42,6 +47,13 @@ public PositionDTO(Position position) {
if (position.getTrack() != null) {
this.track = Math.toDegrees(position.getTrack().getValue());
}

if (gnssTypeSet != null) {
this.gnssType = new HashSet<>();
for (GNSSType type : gnssTypeSet) {
this.gnssType.add(type.getValue());
}
}
}

/**
Expand Down Expand Up @@ -78,4 +90,11 @@ public Double getSpeed() {
public Double getTrack() {
return track;
}

/*
* Returns the GNSS Type used to retrieve position information
*/
public Set<String> getGnssType() {
return gnssType;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -13,34 +13,27 @@
package org.eclipse.kura.rest.position.provider.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.WebApplicationException;
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
import org.osgi.util.position.Position;
import org.osgi.util.measurement.Measurement;
import org.osgi.util.measurement.Unit;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.cloudconnection.message.KuraMessage;
import org.eclipse.kura.message.KuraPayload;
import org.eclipse.kura.message.KuraResponsePayload;
import org.eclipse.kura.position.PositionService;

import org.eclipse.kura.internal.rest.position.PositionRestService;
import org.eclipse.kura.rest.position.api.IsLockedDTO;
import org.eclipse.kura.position.GNSSType;
import org.eclipse.kura.position.PositionService;
import org.eclipse.kura.rest.position.api.DateTimeDTO;
import org.eclipse.kura.rest.position.api.IsLockedDTO;
import org.eclipse.kura.rest.position.api.PositionDTO;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.osgi.util.measurement.Measurement;
import org.osgi.util.measurement.Unit;
import org.osgi.util.position.Position;

public class PositionRestServiceTest {

Expand All @@ -57,23 +50,35 @@ public class PositionRestServiceTest {
public void getPositionTest() {
givenMockedPositionService();
givenIsLocked(true);
givenPosition(0.1, 0.2, 4.5, null, null);
givenPosition(0.1, 0.2, 4.5, null, null, null);

whenGetPosition();

thenPositionIs(0.1, 0.2, 4.5, null, null);
thenPositionIs(0.1, 0.2, 4.5, null, null, null);
thenNoExceptionIsThrown();
}

@Test
public void getPositionTestWithFullParameters() {
givenMockedPositionService();
givenIsLocked(true);
givenPosition(0.1, 0.2, 4.5, 50.6, 9.8);
givenPosition(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList(GNSSType.GPS)));

whenGetPosition();

thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8);
thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList("Gps")));
thenNoExceptionIsThrown();
}

@Test
public void getPositionTestWithFullParametersAndMultipleGNSSTypes() {
givenMockedPositionService();
givenIsLocked(true);
givenPosition(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList(GNSSType.GPS, GNSSType.GLONASS)));

whenGetPosition();

thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList("Gps", "Glonass")));
thenNoExceptionIsThrown();
}

Expand Down Expand Up @@ -112,7 +117,7 @@ public void getIsLockedTestFalse() {
}

@Test
public void getPositionNoLockTest(){
public void getPositionNoLockTest() {
givenMockedPositionService();
givenIsLocked(false);

Expand All @@ -122,7 +127,7 @@ public void getPositionNoLockTest(){
}

@Test
public void getLocalDateTimeNoLockTest(){
public void getLocalDateTimeNoLockTest() {
givenMockedPositionService();
givenIsLocked(false);

Expand All @@ -131,30 +136,33 @@ public void getLocalDateTimeNoLockTest(){
thenExceptionIsThrown();
}


private void givenMockedPositionService(){
private void givenMockedPositionService() {
positionRestService.setPositionServiceImpl(positionService);
}

private void givenPosition(Double longitude, Double latitude, Double altitude, Double speed, Double track) {
private void givenPosition(Double longitude, Double latitude, Double altitude, Double speed, Double track,
Set<GNSSType> gnssTypeSet) {

Measurement latitudeMesurment = latitude != null ? new Measurement(Math.toRadians(latitude), Unit.rad) : null;
Measurement longitudeMesurment = longitude != null ? new Measurement(Math.toRadians(longitude), Unit.rad) : null;
Measurement longitudeMesurment = longitude != null ? new Measurement(Math.toRadians(longitude), Unit.rad)
: null;
Measurement altitudeMesurment = altitude != null ? new Measurement(altitude, Unit.m) : null;
Measurement speedMesurment = speed != null ? new Measurement(speed, Unit.m_s) : null;
Measurement trackMesurment = track != null ? new Measurement(Math.toRadians(track), Unit.rad) : null;

Position testPosition = new Position(latitudeMesurment, longitudeMesurment, altitudeMesurment, speedMesurment, trackMesurment);
Position testPosition = new Position(latitudeMesurment, longitudeMesurment, altitudeMesurment, speedMesurment,
trackMesurment);

when(positionService.getPosition()).thenReturn(testPosition);
when(positionService.getGnssType()).thenReturn(gnssTypeSet);
}

private void givenLocalDateTime(String zonedDateTime) {
LocalDateTime testLocalDateTime = LocalDateTime.parse(zonedDateTime);
when(positionService.getDateTime()).thenReturn(testLocalDateTime);
}

private void givenIsLocked(boolean isLocked){
private void givenIsLocked(boolean isLocked) {
when(positionService.isLocked()).thenReturn(isLocked);
}

Expand All @@ -167,51 +175,59 @@ private void whenGetPosition() {
}

private void whenGetLocalDateTime() {
try{
try {
localDateTimeDTO = positionRestService.getLocalDateTime();
} catch (Exception e) {
testException = e;
}
}

private void whenGetIsLocked() {
try{
try {
isLockedDTO = positionRestService.getIsLocked();
} catch (Exception e) {
testException = e;
}
}

private void thenPositionIs(Double longitude, Double latitude, Double altitude, Double speed, Double track) {
if (longitude != null){
private void thenPositionIs(Double longitude, Double latitude, Double altitude, Double speed, Double track,
Set<String> gnssTypeSet) {

if (longitude != null) {
assertEquals(longitude, positionDTO.getLongitude(), 0.0);
} else {
assertNull(positionDTO.getLongitude());
}

if (latitude != null){
if (latitude != null) {
assertEquals(latitude, positionDTO.getLatitude(), 0.0);
} else {
assertNull(positionDTO.getLatitude());
}

if (altitude != null){
if (altitude != null) {
assertEquals(altitude, positionDTO.getAltitude(), 0.0);
} else {
assertNull(positionDTO.getAltitude());
}

if (speed != null){
if (speed != null) {
assertEquals(speed, positionDTO.getSpeed(), 0.0);
} else {
assertNull(positionDTO.getSpeed());
}

if (track != null){
if (track != null) {
assertEquals(track, positionDTO.getTrack(), 0.0);
} else {
assertNull(positionDTO.getTrack());
}

if (gnssTypeSet != null) {
assertEquals(gnssTypeSet, positionDTO.getGnssType());
} else {
assertNull(positionDTO.getGnssType());
}
}

private void thenLocalDateTimeIs(String zonedDateTime) {
Expand Down

0 comments on commit c82bbd2

Please sign in to comment.