Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Open Subsurface Data Universe Software
Platform
System
Schema
Commits
8f23f9d1
Commit
8f23f9d1
authored
Sep 21, 2020
by
ethiraj krishnamanaidu
Browse files
Merge branch 'slb-put-errormsgfix' into 'master'
Slb put errormsgfix See merge request
!25
parents
ccd5da9e
bae1a763
Pipeline
#9781
passed with stages
in 21 minutes and 50 seconds
Changes
7
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
schema-core/src/main/java/org/opengroup/osdu/schema/api/SchemaController.java
View file @
8f23f9d1
package
org.opengroup.osdu.schema.api
;
import
javax.validation.Valid
;
import
org.opengroup.osdu.schema.constants.SchemaConstants
;
import
org.opengroup.osdu.schema.exceptions.ApplicationException
;
import
org.opengroup.osdu.schema.exceptions.BadRequestException
;
import
org.opengroup.osdu.schema.exceptions.NoSchemaFoundException
;
import
org.opengroup.osdu.schema.exceptions.NotFoundException
;
import
org.opengroup.osdu.schema.model.QueryParams
;
import
org.opengroup.osdu.schema.model.SchemaInfo
;
import
org.opengroup.osdu.schema.model.SchemaInfoResponse
;
import
org.opengroup.osdu.schema.model.SchemaRequest
;
import
org.opengroup.osdu.schema.model.SchemaUpsertResponse
;
import
org.opengroup.osdu.schema.service.ISchemaService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -73,13 +72,8 @@ public class SchemaController {
public
ResponseEntity
<
SchemaInfo
>
upsertSchema
(
@Valid
@RequestBody
SchemaRequest
schemaRequest
)
throws
ApplicationException
,
BadRequestException
{
ResponseEntity
<
SchemaInfo
>
response
=
null
;
try
{
response
=
new
ResponseEntity
<>(
schemaService
.
updateSchema
(
schemaRequest
),
HttpStatus
.
OK
);
}
catch
(
NoSchemaFoundException
noSchemaFound
)
{
response
=
new
ResponseEntity
<>(
schemaService
.
createSchema
(
schemaRequest
),
HttpStatus
.
CREATED
);
}
SchemaUpsertResponse
upsertResp
=
schemaService
.
upsertSchema
(
schemaRequest
);
ResponseEntity
<
SchemaInfo
>
response
=
new
ResponseEntity
<>(
upsertResp
.
getSchemaInfo
(),
upsertResp
.
getHttpCode
());
return
response
;
}
...
...
schema-core/src/main/java/org/opengroup/osdu/schema/constants/SchemaConstants.java
View file @
8f23f9d1
...
...
@@ -99,6 +99,7 @@ public class SchemaConstants {
public
static
final
String
BAD_INPUT
=
"Bad input parameter"
;
public
static
final
String
INVALID_AUTHORIZATION_TOKEN
=
"Invalid authorization token"
;
public
static
final
String
SCHEMA_ID_EXISTS
=
"Schema Id is already present"
;
public
static
final
String
INVALID_UPDATE_OPERATION
=
"Update/Create failed because schema id is present in another tenant"
;
public
static
final
String
UNAUTHORIZED_EXCEPTION
=
"User is unauthorized to perform this action"
;
// OSDU
...
...
schema-core/src/main/java/org/opengroup/osdu/schema/model/SchemaUpsertResponse.java
0 → 100644
View file @
8f23f9d1
package
org.opengroup.osdu.schema.model
;
import
org.springframework.http.HttpStatus
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public
class
SchemaUpsertResponse
{
SchemaInfo
schemaInfo
;
HttpStatus
httpCode
;
}
schema-core/src/main/java/org/opengroup/osdu/schema/service/ISchemaService.java
View file @
8f23f9d1
...
...
@@ -7,6 +7,7 @@ import org.opengroup.osdu.schema.model.QueryParams;
import
org.opengroup.osdu.schema.model.SchemaInfo
;
import
org.opengroup.osdu.schema.model.SchemaInfoResponse
;
import
org.opengroup.osdu.schema.model.SchemaRequest
;
import
org.opengroup.osdu.schema.model.SchemaUpsertResponse
;
public
interface
ISchemaService
{
...
...
@@ -15,7 +16,18 @@ public interface ISchemaService {
SchemaInfo
createSchema
(
SchemaRequest
schemaRequest
)
throws
ApplicationException
,
BadRequestException
;
SchemaInfo
updateSchema
(
SchemaRequest
schemaRequest
)
throws
ApplicationException
,
BadRequestException
;
/**
* This method first tries to update the schema with the given schema-id. If there is no schema found,
* it tries to create the new schema for the given tenant.
*
* @param schemarequest
* @return SchemaUpsertResponse
* @throws ApplicationException
* @throws BadRequestException
*/
SchemaUpsertResponse
upsertSchema
(
SchemaRequest
schemaRequest
)
throws
ApplicationException
,
BadRequestException
;
SchemaInfoResponse
getSchemaInfoList
(
QueryParams
queryParams
)
throws
BadRequestException
,
ApplicationException
;
}
schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java
View file @
8f23f9d1
...
...
@@ -9,7 +9,6 @@ import java.util.LinkedList;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
org.apache.commons.lang3.StringUtils
;
import
org.json.JSONException
;
import
org.opengroup.osdu.core.common.logging.JaxRsDpsLog
;
...
...
@@ -26,6 +25,7 @@ import org.opengroup.osdu.schema.model.SchemaIdentity;
import
org.opengroup.osdu.schema.model.SchemaInfo
;
import
org.opengroup.osdu.schema.model.SchemaInfoResponse
;
import
org.opengroup.osdu.schema.model.SchemaRequest
;
import
org.opengroup.osdu.schema.model.SchemaUpsertResponse
;
import
org.opengroup.osdu.schema.provider.interfaces.schemainfostore.ISchemaInfoStore
;
import
org.opengroup.osdu.schema.provider.interfaces.schemastore.ISchemaStore
;
import
org.opengroup.osdu.schema.service.IAuthorityService
;
...
...
@@ -35,8 +35,8 @@ import org.opengroup.osdu.schema.service.ISourceService;
import
org.opengroup.osdu.schema.util.SchemaResolver
;
import
org.opengroup.osdu.schema.util.SchemaUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Service
;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.JsonMappingException
;
...
...
@@ -257,6 +257,29 @@ public class SchemaService implements ISchemaService {
return
SchemaInfoResponse
.
builder
().
schemaInfos
(
schemaFinalList
).
count
(
schemaFinalList
.
size
())
.
offset
(
queryParams
.
getOffset
()).
totalCount
(
schemaList
.
size
()).
build
();
}
@Override
public
SchemaUpsertResponse
upsertSchema
(
SchemaRequest
schemaRequest
)
throws
ApplicationException
,
BadRequestException
{
SchemaInfo
response
=
null
;
HttpStatus
httpCode
=
HttpStatus
.
BAD_REQUEST
;
SchemaUpsertResponse
.
SchemaUpsertResponseBuilder
upsertBuilder
=
SchemaUpsertResponse
.
builder
();
try
{
response
=
updateSchema
(
schemaRequest
);
httpCode
=
HttpStatus
.
OK
;
}
catch
(
NoSchemaFoundException
noSchemaFound
)
{
try
{
response
=
createSchema
(
schemaRequest
);
httpCode
=
HttpStatus
.
CREATED
;
}
catch
(
BadRequestException
badreqEx
)
{
//If there is same schema-id for other tenant then throw different error message
if
(
SchemaConstants
.
SCHEMA_ID_EXISTS
.
equals
(
badreqEx
.
getMessage
()))
throw
new
BadRequestException
(
SchemaConstants
.
INVALID_UPDATE_OPERATION
);
throw
badreqEx
;
}
}
return
upsertBuilder
.
schemaInfo
(
response
).
httpCode
(
httpCode
).
build
();
}
private
void
getSchemaInfos
(
QueryParams
queryParams
,
List
<
SchemaInfo
>
schemaList
,
String
tenant
)
throws
ApplicationException
{
...
...
schema-core/src/test/java/org/opengroup/osdu/schema/api/SchemaControllerTest.java
View file @
8f23f9d1
...
...
@@ -5,8 +5,6 @@ import static org.mockito.Mockito.when;
import
java.util.Date
;
import
java.util.LinkedList
;
import
org.json.JSONException
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.InjectMocks
;
...
...
@@ -15,16 +13,16 @@ import org.opengroup.osdu.schema.enums.SchemaScope;
import
org.opengroup.osdu.schema.enums.SchemaStatus
;
import
org.opengroup.osdu.schema.exceptions.ApplicationException
;
import
org.opengroup.osdu.schema.exceptions.BadRequestException
;
import
org.opengroup.osdu.schema.exceptions.NoSchemaFoundException
;
import
org.opengroup.osdu.schema.exceptions.NotFoundException
;
import
org.opengroup.osdu.schema.model.QueryParams
;
import
org.opengroup.osdu.schema.model.SchemaIdentity
;
import
org.opengroup.osdu.schema.model.SchemaInfo
;
import
org.opengroup.osdu.schema.model.SchemaInfoResponse
;
import
org.opengroup.osdu.schema.model.SchemaRequest
;
import
org.opengroup.osdu.schema.model.SchemaUpsertResponse
;
import
org.opengroup.osdu.schema.service.ISchemaService
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
...
...
@@ -56,27 +54,30 @@ public class SchemaControllerTest {
}
@Test
public
void
testUpsertSchema_update
()
throws
ApplicationException
,
NotFoundException
,
BadRequestException
,
JSONException
,
JsonProcessingException
{
public
void
testUpsertSchema_update
()
throws
ApplicationException
,
BadRequestException
{
schemaRequest
=
getSchemaRequestObject
();
when
(
schemaService
.
updateSchema
(
schemaRequest
)).
thenReturn
(
getSchemaInfoObject
());
when
(
schemaService
.
upsertSchema
(
schemaRequest
)).
thenReturn
(
getSchemaUpsertResponse_Updated
());
assertNotNull
(
schemaController
.
upsertSchema
(
schemaRequest
));
}
@Test
public
void
testUpsertSchema_create
()
throws
ApplicationException
,
NotFoundException
,
BadRequestException
,
JSONException
,
JsonProcessingException
{
public
void
testUpsertSchema_create
()
throws
ApplicationException
,
BadRequestException
{
schemaRequest
=
getSchemaRequestObject
();
when
(
schemaService
.
updateSchema
(
schemaRequest
)).
thenThrow
(
NoSchemaFoundException
.
class
);
when
(
schemaService
.
createSchema
(
schemaRequest
)).
thenReturn
(
getSchemaInfoObject
());
when
(
schemaService
.
upsertSchema
(
schemaRequest
)).
thenReturn
(
getSchemaUpsertResponse_Created
());
assertNotNull
(
schemaController
.
upsertSchema
(
schemaRequest
));
}
@Test
(
expected
=
BadRequestException
.
class
)
public
void
testUpsertSchema_Failed
()
throws
ApplicationException
,
BadRequestException
{
schemaRequest
=
getSchemaRequestObject
();
when
(
schemaService
.
upsertSchema
(
schemaRequest
)).
thenThrow
(
BadRequestException
.
class
);
schemaController
.
upsertSchema
(
schemaRequest
);
}
@Test
public
void
testGetSchemaInfoList
()
throws
ApplicationException
,
NotFoundException
,
BadRequestException
{
...
...
@@ -90,6 +91,8 @@ public class SchemaControllerTest {
}
private
SchemaRequest
getSchemaRequestObject
()
{
return
SchemaRequest
.
builder
().
schema
(
null
).
schemaInfo
(
SchemaInfo
.
builder
().
createdBy
(
"creator"
)
.
dateCreated
(
new
Date
(
System
.
currentTimeMillis
()))
...
...
@@ -100,6 +103,14 @@ public class SchemaControllerTest {
.
schemaVersionMajor
(
1L
).
schemaVersionMinor
(
1L
).
source
(
"wks"
).
build
())
.
build
()).
build
();
}
private
SchemaUpsertResponse
getSchemaUpsertResponse_Created
()
{
return
SchemaUpsertResponse
.
builder
().
schemaInfo
(
getSchemaInfoObject
()).
httpCode
(
HttpStatus
.
CREATED
).
build
();
}
private
SchemaUpsertResponse
getSchemaUpsertResponse_Updated
()
{
return
SchemaUpsertResponse
.
builder
().
schemaInfo
(
getSchemaInfoObject
()).
httpCode
(
HttpStatus
.
OK
).
build
();
}
private
SchemaInfo
getSchemaInfoObject
()
{
return
SchemaInfo
.
builder
().
createdBy
(
"creator"
).
dateCreated
(
new
Date
(
System
.
currentTimeMillis
()))
...
...
schema-core/src/test/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaServiceTest.java
View file @
8f23f9d1
...
...
@@ -2,6 +2,7 @@ package org.opengroup.osdu.schema.service.serviceimpl;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
mockito
.
Mockito
.
when
;
...
...
@@ -38,6 +39,7 @@ import org.opengroup.osdu.schema.service.IEntityTypeService;
import
org.opengroup.osdu.schema.service.ISourceService
;
import
org.opengroup.osdu.schema.util.SchemaResolver
;
import
org.opengroup.osdu.schema.util.SchemaUtil
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
...
...
@@ -795,6 +797,104 @@ public class SchemaServiceTest {
schemaService
.
getSchemaInfoList
(
queryParams
);
}
@Test
public
void
testUpsertSchema_SuccessfullUpdate
()
throws
ApplicationException
,
NotFoundException
,
BadRequestException
{
SchemaInfo
schInfo
=
getMockSchemaInfo_development_status
();
SchemaRequest
schReq
=
getMockSchemaObject_Development
();
Mockito
.
when
(
headers
.
getPartitionId
()).
thenReturn
(
"tenant"
);
Mockito
.
when
(
schemaInfoStore
.
getSchemaInfo
(
"os:wks:well:1.1.1"
)).
thenReturn
(
schInfo
);
Mockito
.
when
(
schemaInfoStore
.
updateSchemaInfo
(
schReq
)).
thenReturn
(
schInfo
);
Mockito
.
when
(
schemaStore
.
createSchema
(
Mockito
.
anyString
(),
Mockito
.
anyString
())).
thenReturn
(
"{}"
);
assertEquals
(
HttpStatus
.
OK
,
schemaService
.
upsertSchema
(
schReq
).
getHttpCode
());
}
@Test
public
void
testUpsertSchema_SuccessfullCreate
()
throws
ApplicationException
,
NotFoundException
,
BadRequestException
{
//throw exception while updating the schema
SchemaRequest
schReq
=
getMockSchemaObject_Development
();
SchemaInfo
schInfoCr
=
getMockSchemaInfo_development_status
();
Mockito
.
when
(
headers
.
getPartitionId
()).
thenReturn
(
"tenant"
);
Mockito
.
when
(
schemaInfoStore
.
getSchemaInfo
(
"os:wks:well:1.1.1"
)).
thenThrow
(
new
NotFoundException
());
//Create schema call
Mockito
.
when
(
headers
.
getPartitionId
()).
thenReturn
(
"tenant"
);
String
schemaId
=
"os:wks:well:1.1.1"
;
when
(
schemaInfoStore
.
isUnique
(
schemaId
,
"common"
)).
thenReturn
(
true
);
when
(
schemaInfoStore
.
isUnique
(
schemaId
,
"tenant"
)).
thenReturn
(
true
);
Mockito
.
when
(
schemaStore
.
getSchema
(
Mockito
.
anyString
(),
Mockito
.
anyString
()))
.
thenThrow
(
NotFoundException
.
class
);
Mockito
.
when
(
authorityService
.
checkAndRegisterAuthorityIfNotPresent
(
schReq
.
getSchemaInfo
().
getSchemaIdentity
().
getAuthority
()))
.
thenReturn
(
true
);
Mockito
.
when
(
sourceService
.
checkAndRegisterSourceIfNotPresent
(
schReq
.
getSchemaInfo
().
getSchemaIdentity
().
getSource
()))
.
thenReturn
(
true
);
Mockito
.
when
(
entityTypeService
.
checkAndRegisterEntityTypeIfNotPresent
(
schReq
.
getSchemaInfo
().
getSchemaIdentity
().
getEntityType
()))
.
thenReturn
(
true
);
Mockito
.
when
(
schemaResolver
.
resolveSchema
(
Mockito
.
anyString
())).
thenReturn
(
"{}"
);
Mockito
.
when
(
schemaStore
.
createSchema
(
Mockito
.
anyString
(),
Mockito
.
anyString
())).
thenReturn
(
"{}"
);
Mockito
.
when
(
schemaInfoStore
.
createSchemaInfo
(
schReq
))
.
thenReturn
(
schInfoCr
);
assertEquals
(
HttpStatus
.
CREATED
,
schemaService
.
upsertSchema
(
schReq
).
getHttpCode
());
}
@Test
(
expected
=
BadRequestException
.
class
)
public
void
testUpsertSchema_WhenSchemaExistInOtherTenant
()
throws
ApplicationException
,
NotFoundException
,
BadRequestException
{
//schemaInfoStore.isUnique(schemaId, dataPartitionId)
//throw exception while updating the schema
SchemaRequest
schReq
=
getMockSchemaObject_Development
();
Mockito
.
when
(
headers
.
getPartitionId
()).
thenReturn
(
"tenant"
);
Mockito
.
when
(
schemaInfoStore
.
getSchemaInfo
(
"os:wks:well:1.1.1"
)).
thenThrow
(
new
NotFoundException
());
//Create schema call
Mockito
.
when
(
headers
.
getPartitionId
()).
thenReturn
(
"tenant"
);
String
schemaId
=
"os:wks:well:1.1.1"
;
when
(
schemaInfoStore
.
isUnique
(
schemaId
,
"tenant"
)).
thenReturn
(
false
);
schemaService
.
upsertSchema
(
schReq
).
getHttpCode
();
}
@Test
public
void
testUpsertSchema_Badrequest
()
throws
ApplicationException
,
NotFoundException
,
BadRequestException
{
//schemaInfoStore.isUnique(schemaId, dataPartitionId)
//throw exception while updating the schema
SchemaRequest
schReq
=
getMockSchemaObject_Development
();
Mockito
.
when
(
headers
.
getPartitionId
()).
thenReturn
(
"tenant"
);
Mockito
.
when
(
schemaInfoStore
.
getSchemaInfo
(
"os:wks:well:1.1.1"
)).
thenThrow
(
new
NotFoundException
());
//Create schema call
Mockito
.
when
(
headers
.
getPartitionId
()).
thenReturn
(
"tenant"
);
String
schemaId
=
"os:wks:well:1.1.1"
;
when
(
schemaInfoStore
.
isUnique
(
schemaId
,
"tenant"
)).
thenReturn
(
false
);
try
{
schemaService
.
upsertSchema
(
schReq
).
getHttpCode
();
}
catch
(
BadRequestException
badreqEx
)
{
assertTrue
(
SchemaConstants
.
INVALID_UPDATE_OPERATION
.
equals
(
badreqEx
.
getMessage
()));
}
}
private
SchemaRequest
getMockSchemaObject_published
()
{
return
SchemaRequest
.
builder
().
schema
(
"{}"
)
.
schemaInfo
(
SchemaInfo
.
builder
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment