Skip to content

Commit

Permalink
new field timeToLive for endpoints introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
bs-sili committed Dec 12, 2023
1 parent 790f11b commit 75a57e5
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 37 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.6
### Added
- new timeToLive field added to Endpoints

## fixed


## 0.2.5
### Added
- Introduced versioning of the APIs of the Discovery Finder.First version of this API is 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,47 @@
********************************************************************************/
package org.eclipse.tractusx.discoveryfinder;

import java.util.List;

import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.tractusx.discoveryfinder.model.Error;
import org.eclipse.tractusx.discoveryfinder.model.ErrorResponse;
import org.eclipse.tractusx.discoveryfinder.service.EntityNotFoundException;
import org.eclipse.tractusx.discoveryfinder.service.ValidationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import jakarta.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ApiExceptionHandler extends ResponseEntityExceptionHandler {


@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid( final MethodArgumentNotValidException ex,
final HttpHeaders headers,
final HttpStatusCode status, final WebRequest request ) {

List<Error> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
.map( fieldError -> new Error()
.message( Optional.ofNullable( fieldError.getDefaultMessage() ).orElseGet( () -> "null" ) )
).collect( Collectors.toList());
return new ResponseEntity<>(
errors, HttpStatus.BAD_REQUEST );
}


@ExceptionHandler( { ValidationException.class } )
public ResponseEntity<ErrorResponse> handleValidationException( final HttpServletRequest request,
final ValidationException exception ) {
Expand All @@ -61,4 +86,5 @@ public ResponseEntity<ErrorResponse> handleDuplicateKeyException( final HttpServ
.message( "Entity for the given resourceId already exists." )
.path( request.getRequestURI() ) ), HttpStatus.BAD_REQUEST );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

Expand Down Expand Up @@ -51,5 +53,8 @@ public static class InitialEndpoint {
private String description;
private String documentation;

@Min( value = 1, message = "value must be greater or equal to 1" )
@Max( value = 31536000, message = "value must be lesser or equal to 31536000")
private Integer timeToLive;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ public class Endpoint {
String endpointAddress;
String documentation;
UUID resourceId;
Integer timeToLive;
}
11 changes: 6 additions & 5 deletions backend/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ spring:

discoveryfinder:
publicClientId: discovery-finder-client
# initialEndpoints:
# - type: "EDC"
# endpointAddress: "https://"
# description: ""
# documentation: ""
# initialEndpoints:
# - type: "EDC"
# endpointAddress: "https://"
# description: "ss"
# documentation: "htt"
# timeToLive: 31536000

# Properties develop against a postgres instance
# You can start a postgres instance using the docker-compose file located in the backend/postgres/ directory
Expand Down
11 changes: 10 additions & 1 deletion backend/src/main/resources/db/changelog/db.changelog-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ databaseChangeLog:
columnNames: resource_id
constraintName: endpoint_ak_AK_02
tableName: endpoint
validate: true
validate: true
- changeSet:
id: 29112023-01
author: slindner
changes:
- addColumn:
tableName: endpoint
columns:
name: time_to_live
type: integer
20 changes: 15 additions & 5 deletions backend/src/main/resources/static/discovery-finder-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ components:
- type
- description
- endpointAddress
- timeToLive
properties:
type:
type: string
Expand All @@ -151,6 +152,10 @@ components:
maxLength: 500
resourceId:
type: string
timeToLive:
type: integer
minimum: 1
maximum: 31536000
SearchRequest:
title: SearchRequest
required:
Expand Down Expand Up @@ -209,7 +214,8 @@ components:
"type": "oen",
"description": "Service to discover BPN to a particular OEN",
"endpointAddress": "http://...",
"documentation": "http://.../swagger/index.html"
"documentation": "http://.../swagger/index.html",
"timeToLive": 31536000
}
discovery-endpoint-result:
value:
Expand All @@ -218,7 +224,8 @@ components:
"description": "Service to discover BPN to a particular OEN",
"endpointAddress": "http://...",
"documentation": "http://.../swagger/index.html",
"resourceId": "ec6f407b-4296-418c-9e4e-fb739fe72a67"
"resourceId": "ec6f407b-4296-418c-9e4e-fb739fe72a67",
"timeToLive": 31536000
}
discovey-endpoint-search:
value:
Expand All @@ -234,21 +241,24 @@ components:
"description": "Service to discover BPN to a particular OEN",
"endpointAddress": "http://...",
"documentation": "http://.../swagger/index.html",
"resourceId": "ec6f407b-4296-418c-9e4e-fb739fe72a67"
"resourceId": "ec6f407b-4296-418c-9e4e-fb739fe72a67",
"timeToLive": 31536000
},
{
"type": "bpid",
"description": "Service to discover BPN to a particular Battery Pass ID",
"endpointAddress": "http://...",
"documentation": "http://.../swagger/index.html",
"resourceId": "08702529-3714-4c4f-b022-346b9b4fbbe2"
"resourceId": "08702529-3714-4c4f-b022-346b9b4fbbe2",
"timeToLive": 31536000
},
{
"type": "bpn",
"description": "Service to discover EDC to a particular BPN",
"endpointAddress": "http://...",
"documentation": "http://.../swagger/index.html",
"resourceId": "316417cd-0fb5-4daf-8dfa-8f68125923f1"
"resourceId": "316417cd-0fb5-4daf-8dfa-8f68125923f1",
"timeToLive": 31536000
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
********************************************************************************/
package org.eclipse.tractusx.discoveryfinder;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.eclipse.tractusx.discoveryfinder.configuration.TestJwtTokenFactory;
import org.eclipse.tractusx.discoveryfinder.repository.EndpointRepository;
Expand All @@ -37,6 +37,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jayway.jsonpath.JsonPath;

@SpringBootTest
@AutoConfigureMockMvc
Expand Down Expand Up @@ -72,19 +73,17 @@ protected String performDiscoveryEndpointCreateRequest( String requestToJson ) t
.andExpect( status().isCreated() )
.andReturn();

String sResult = result.getResponse().getContentAsString();
String resourceID = sResult.substring( sResult.lastIndexOf( ":" ) );
resourceID = resourceID.replace( "\"}", "" ).replace( ":\"", "" ).trim();

String resourceID = JsonPath.read( result.getResponse().getContentAsString(), "$.resourceId" );
return resourceID;
}

protected ObjectNode createDiscoveryEndpoint( String type, String endpointAddress, String description, String documentation ) {
protected ObjectNode createDiscoveryEndpoint( String type, String endpointAddress, String description, String documentation, Integer timeToLive ) {
ObjectNode requestNode = mapper.createObjectNode();
requestNode.put( "type", type );
requestNode.put( "description", description );
requestNode.put( "endpointAddress", endpointAddress );
requestNode.put( "documentation", documentation );
requestNode.put("timeToLive", timeToLive);
return requestNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void cleanUp() {
@Test
public void givenValidDiscoveryEndpoint_whenSave_thenSaveSucceeds() throws Exception {
// given
ObjectNode givenValidPayload = createDiscoveryEndpoint( "oen", "oen-url-1", "description", "http://oen-swagger" );
ObjectNode givenValidPayload = createDiscoveryEndpoint( "oen", "oen-url-1", "description", "http://oen-swagger",31536000 );

//when -> then
mockMvc.perform(
Expand All @@ -59,7 +59,7 @@ public void givenValidDiscoveryEndpoint_whenSave_thenSaveSucceeds() throws Excep
@Test
public void givenDuplicateDiscoveryEndpoint_whenSave_thenThrowDuplicateKeyException() throws Exception {
// given
ObjectNode givenPayload = createDiscoveryEndpoint( "oen", "oen-url-2", "description", "http://oen-swagger" );
ObjectNode givenPayload = createDiscoveryEndpoint( "oen", "oen-url-2", "description", "http://oen-swagger", 31536000 );
performDiscoveryEndpointCreateRequest( toJson( givenPayload ) );
ObjectNode duplicatedPayload = givenPayload;

Expand All @@ -80,7 +80,7 @@ public void givenDuplicateDiscoveryEndpoint_whenSave_thenThrowDuplicateKeyExcept
@Test
public void givenValidResourceId_whenDelete_thenDeleteSucceeds() throws Exception {
// given
ObjectNode givenPayload = createDiscoveryEndpoint( "oen", "oen-url-3", "description", "http://oen-swagger" );
ObjectNode givenPayload = createDiscoveryEndpoint( "oen", "oen-url-3", "description", "http://oen-swagger", 31536000 );
String givenResourceId = performDiscoveryEndpointCreateRequest( toJson( givenPayload ) );

// when -> then
Expand Down Expand Up @@ -136,8 +136,8 @@ public void givenInvalidResourceIdFormat_whenDelete_thenThrowValidationException
@Test
public void givenSearchRequest_whenGetDiscoveryEndpoints_thenReturnResult() throws Exception {
// given
ObjectNode givenOenNode1 = createDiscoveryEndpoint( "oen", "oen-url-4", "description", "http://oen-swagger" );
ObjectNode givenOenNode2 = createDiscoveryEndpoint( "bpId", "bpId-url-1", "description", "http://oen-swagger" );
ObjectNode givenOenNode1 = createDiscoveryEndpoint( "oen", "oen-url-4", "description", "http://oen-swagger", 31536000 );
ObjectNode givenOenNode2 = createDiscoveryEndpoint( "bpId", "bpId-url-1", "description", "http://oen-swagger", 31536000 );
performDiscoveryEndpointCreateRequest( toJson( givenOenNode1 ) );
performDiscoveryEndpointCreateRequest( toJson( givenOenNode2 ) );

Expand All @@ -164,9 +164,9 @@ public void givenSearchRequest_whenGetDiscoveryEndpoints_thenReturnResult() thro
@Test
public void givenEmptySearchRequest_whenGetDiscoveryEndpoints_thenReturnAllDiscoveryEndpoints() throws Exception {
// given
ObjectNode givenOenNode1 = createDiscoveryEndpoint( "bpId", "bpId-url-2", "description", "http://oen-swagger" );
ObjectNode givenOenNode2 = createDiscoveryEndpoint( "oen", "oen-url-5", "description", "http://oen-swagger" );
ObjectNode givenOenNode3 = createDiscoveryEndpoint( "serialId", "serialId-url-1", "description", "http://oen-swagger" );
ObjectNode givenOenNode1 = createDiscoveryEndpoint( "bpId", "bpId-url-2", "description", "http://oen-swagger", 31536000 );
ObjectNode givenOenNode2 = createDiscoveryEndpoint( "oen", "oen-url-5", "description", "http://oen-swagger", 31536000 );
ObjectNode givenOenNode3 = createDiscoveryEndpoint( "serialId", "serialId-url-1", "description", "http://oen-swagger", 31536000 );
performDiscoveryEndpointCreateRequest( toJson( givenOenNode1 ) );
performDiscoveryEndpointCreateRequest( toJson( givenOenNode2 ) );
performDiscoveryEndpointCreateRequest( toJson( givenOenNode3 ) );
Expand All @@ -189,4 +189,40 @@ public void givenEmptySearchRequest_whenGetDiscoveryEndpoints_thenReturnAllDisco
.andExpect( jsonPath( "$.endpoints[1].type", equalTo( "oen" ) ) )
.andExpect( jsonPath( "$.endpoints[2].type", equalTo( "serialId" ) ) );
}

@Test
public void givenToBigTimeToLive_whenSave_thenThrowMethodArgumentNotValidException() throws Exception {
//given
ObjectNode givenInvalidTtl = createDiscoveryEndpoint( "bpId", "bpId-url-2", "description", "http://oen-swagger", 31536001 );
//when -> then
mockMvc.perform(
MockMvcRequestBuilders
.post( DISCOVERY_FINDER_BASE_PATH )
.accept( MediaType.APPLICATION_JSON )
.contentType( MediaType.APPLICATION_JSON )
.content( toJson( givenInvalidTtl ) )
.with( jwtTokenFactory.allRoles() )
)
.andDo( MockMvcResultHandlers.print() )
.andExpect( status().isBadRequest() )
.andExpect( jsonPath( "$.[0].message", equalTo( "must be less than or equal to 31536000" ) ) );
}

@Test
public void givenToSmallTimeToLive_whenSave_thenThrowMethodArgumentNotValidException() throws Exception {
//given
ObjectNode givenInvalidTtl = createDiscoveryEndpoint( "bpId", "bpId-url-2", "description", "http://oen-swagger", 0 );
//when -> then
mockMvc.perform(
MockMvcRequestBuilders
.post( DISCOVERY_FINDER_BASE_PATH )
.accept( MediaType.APPLICATION_JSON )
.contentType( MediaType.APPLICATION_JSON )
.content( toJson( givenInvalidTtl ) )
.with( jwtTokenFactory.allRoles() )
)
.andDo( MockMvcResultHandlers.print() )
.andExpect( status().isBadRequest() )
.andExpect( jsonPath( "$.[0].message", equalTo( "must be greater than or equal to 1" ) ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void givenAuthenticationTokenWithoutViewRole_whenGetDiscoveryEndpoints_th
@Test
public void givenAuthenticationTokenWithoutAddRole_whenSave_thenReturnForbidden() throws Exception {
// given
ObjectNode givenValidPayload = createDiscoveryEndpoint( "oen", "oen-url-sec-0", "description", "http://oen-swagger" );
ObjectNode givenValidPayload = createDiscoveryEndpoint( "oen", "oen-url-sec-0", "description", "http://oen-swagger", 31536000 );

//when -> then
mockMvc.perform(
Expand Down Expand Up @@ -159,7 +159,7 @@ public void givenAuthenticationTokenWithoutDeleteRole_whenDelete_thenReturnForbi
@Test
public void givenAuthenticationTokenWithAddRole_whenSave_thenSaveSucceeds() throws Exception {
// given
ObjectNode givenValidPayload = createDiscoveryEndpoint( "oen", "oen-url-sec-1", "description", "http://oen-swagger" );
ObjectNode givenValidPayload = createDiscoveryEndpoint( "oen", "oen-url-sec-1", "description", "http://oen-swagger", 31536000 );

//when -> then
mockMvc.perform(
Expand All @@ -177,7 +177,7 @@ public void givenAuthenticationTokenWithAddRole_whenSave_thenSaveSucceeds() thro
@Test
public void givenAuthenticationTokenWithDeleteRole_whenDelete_thenDeleteSucceeds() throws Exception {
// given
ObjectNode givenPayload = createDiscoveryEndpoint( "oen", "oen-url-sec-2", "description", "http://oen-swagger" );
ObjectNode givenPayload = createDiscoveryEndpoint( "oen", "oen-url-sec-2", "description", "http://oen-swagger", 31536000 );
String givenResourceId = performDiscoveryEndpointCreateRequest( toJson( givenPayload ) );

// when -> then
Expand All @@ -195,7 +195,7 @@ public void givenAuthenticationTokenWithDeleteRole_whenDelete_thenDeleteSucceeds
@Test
public void givenAuthenticationTokenWithViewRole_whenGetDiscoveryEndpoints_thenReturnResult() throws Exception {
// given
ObjectNode givenOenNode1 = createDiscoveryEndpoint( "oen", "oen-url-sec-3", "description", "http://oen-swagger" );
ObjectNode givenOenNode1 = createDiscoveryEndpoint( "oen", "oen-url-sec-3", "description", "http://oen-swagger",31536000 );
performDiscoveryEndpointCreateRequest( toJson( givenOenNode1 ) );

String givenTypes = "{\n\"types\": [\n \"oen\",\n \"bpId\",\n \"bpId\"\n ]\n}";
Expand Down
Loading

0 comments on commit 75a57e5

Please sign in to comment.