Commit cc986f08 authored by Aman Verma's avatar Aman Verma
Browse files

deleting the api code from azure

parent 81151760
Pipeline #61721 passed with stages
in 32 minutes
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.schema.azure.api;
import org.opengroup.osdu.schema.azure.interfaces.ISchemaServiceAzure;
import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.exceptions.BadRequestException;
import org.opengroup.osdu.schema.model.SchemaInfo;
import org.opengroup.osdu.schema.model.SchemaRequest;
import org.opengroup.osdu.schema.model.SchemaUpsertResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("/schemas/system")
public class AzureSchemaApi {
@Autowired
ISchemaServiceAzure schemaService;
@PutMapping()
@PreAuthorize("@authorizationFilterSP.hasPermissions()")
public ResponseEntity<SchemaInfo> upsertSystemSchema(@Valid @RequestBody SchemaRequest schemaRequest)
throws ApplicationException, BadRequestException {
SchemaUpsertResponse upsertResp = schemaService.upsertSystemSchema(schemaRequest);
ResponseEntity<SchemaInfo> response = new ResponseEntity<>(upsertResp.getSchemaInfo(), upsertResp.getHttpCode());
return response;
}
}
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.schema.azure.service.serviceimpl;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.schema.azure.di.SystemResourceConfig;
import org.opengroup.osdu.schema.azure.interfaces.ISchemaServiceAzure;
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.model.SchemaInfo;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class SchemaServiceAzure implements ISchemaServiceAzure {
private final ISchemaService schemaServiceCore;
final DpsHeaders headers;
private final SystemResourceConfig systemResourceConfig;
public SchemaInfo createSystemSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
updateDataPartitionId();
return schemaServiceCore.createSchema(schemaRequest);
}
public SchemaUpsertResponse upsertSystemSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
updateDataPartitionId();
return schemaServiceCore.upsertSchema(schemaRequest);
}
private void updateDataPartitionId() {
headers.put(SchemaConstants.DATA_PARTITION_ID, systemResourceConfig.getSharedTenant());
}
}
package org.opengroup.osdu.schema.provider.azure.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.schema.azure.api.AzureSchemaApi;
import org.opengroup.osdu.schema.azure.interfaces.ISchemaServiceAzure;
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.NotFoundException;
import org.opengroup.osdu.schema.model.SchemaIdentity;
import org.opengroup.osdu.schema.model.SchemaInfo;
import org.opengroup.osdu.schema.model.SchemaRequest;
import org.opengroup.osdu.schema.model.SchemaUpsertResponse;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
public class AzureSchemaApiTest {
@Mock
ISchemaServiceAzure schemaServiceAzure;
@InjectMocks
AzureSchemaApi azureSchemaApi;
private SchemaRequest schemaRequest;
@Test
public void testCreateSchema()
throws ApplicationException, NotFoundException, BadRequestException, JsonProcessingException {
schemaRequest = getSchemaRequestObject();
when(schemaServiceAzure.createSystemSchema(schemaRequest)).thenReturn(getSchemaInfoObject());
assertNotNull(schemaServiceAzure.createSystemSchema(schemaRequest));
}
@Test
public void testUpsertSchema_update() throws ApplicationException, BadRequestException {
schemaRequest = getSchemaRequestObject();
when(schemaServiceAzure.upsertSystemSchema(schemaRequest)).thenReturn(getSchemaUpsertResponse_Updated());
assertNotNull(azureSchemaApi.upsertSystemSchema(schemaRequest));
}
@Test
public void testUpsertSchema_create() throws ApplicationException, BadRequestException {
schemaRequest = getSchemaRequestObject();
when(schemaServiceAzure.upsertSystemSchema(schemaRequest)).thenReturn(getSchemaUpsertResponse_Created());
assertNotNull(azureSchemaApi.upsertSystemSchema(schemaRequest));
}
@Test(expected = BadRequestException.class)
public void testUpsertSchema_Failed() throws ApplicationException, BadRequestException {
schemaRequest = getSchemaRequestObject();
when(schemaServiceAzure.upsertSystemSchema(schemaRequest)).thenThrow(BadRequestException.class);
azureSchemaApi.upsertSystemSchema(schemaRequest);
}
private SchemaRequest getSchemaRequestObject() {
return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator")
.dateCreated(new Date(System.currentTimeMillis()))
.schemaIdentity(SchemaIdentity.builder().authority("os").entityType("well").id("os..wks.well.1.1")
.schemaVersionMajor(1L).schemaVersionMinor(1L).source("wks").build())
.scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT)
.supersededBy(SchemaIdentity.builder().authority("os").entityType("well").id("os..wks.well.1.4")
.schemaVersionMajor(1L).schemaVersionMinor(1L).source("wks").build())
.build()).build();
}
private SchemaInfo getSchemaInfoObject() {
return SchemaInfo.builder().createdBy("creator").dateCreated(new Date(System.currentTimeMillis()))
.schemaIdentity(SchemaIdentity.builder().authority("os").entityType("well").id("os..wks.well.1.1")
.schemaVersionMajor(1L).schemaVersionMinor(1L).source("wks").build())
.scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT)
.supersededBy(SchemaIdentity.builder().authority("os").entityType("well").id("os..wks.well.1.4")
.schemaVersionMajor(1L).schemaVersionMinor(1L).source("wks").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();
}
}
package org.opengroup.osdu.schema.provider.azure.service.serviceimpl;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.schema.azure.di.SystemResourceConfig;
import org.opengroup.osdu.schema.azure.service.serviceimpl.SchemaServiceAzure;
import org.opengroup.osdu.schema.constants.SchemaConstants;
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.NotFoundException;
import org.opengroup.osdu.schema.model.SchemaIdentity;
import org.opengroup.osdu.schema.model.SchemaInfo;
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.Value;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.mockito.MockitoAnnotations.initMocks;
public class SchemaServiceAzureTest {
@InjectMocks
SchemaServiceAzure schemaServiceAzure;
@Mock
ISchemaService schemaServiceCore;
@Mock
DpsHeaders headers;
@Mock
SchemaUpsertResponse upsertResponse;
@Mock
SystemResourceConfig systemResourceConfig;
private Date currDate = new Date();
@Rule
public ExpectedException expectedException = ExpectedException.none();
private static final String SHARED_TENANT = "common";
private static final String PRIVATE_TENANT = "opendes";
@Before
public void setUp() {
initMocks(this);
Mockito.when(systemResourceConfig.getSharedTenant()).thenReturn(SHARED_TENANT);
}
@Test
public void testCreateSharedSchema() throws BadRequestException, ApplicationException {
SchemaRequest schemaRequest = getMockSchemaObject_published_SharedScope();
SchemaInfo schemaInfo = getMockSchemaInfo_Published_SharedScope();
Mockito.when(schemaServiceCore.createSchema(schemaRequest)).thenReturn(schemaInfo);
assertEquals(schemaInfo, schemaServiceAzure.createSystemSchema(schemaRequest));
verify(this.headers, times(1)).put(SchemaConstants.DATA_PARTITION_ID, SHARED_TENANT);
}
@Test
public void testCreateSharedSchema_SchemaAlreadyPresent() throws BadRequestException, ApplicationException {
expectedException.expect(BadRequestException.class);
expectedException.expectMessage(SchemaConstants.SCHEMA_ID_EXISTS);
SchemaRequest schemaRequest = getMockSchemaObject_published_SharedScope();
Mockito.when(schemaServiceCore.createSchema(schemaRequest)).thenThrow(new BadRequestException(SchemaConstants.SCHEMA_ID_EXISTS));
schemaServiceAzure.createSystemSchema(schemaRequest);
}
@Test
public void testUpsertSharedSchema_SuccessfulUpdate() throws BadRequestException, ApplicationException {
SchemaRequest schemaRequest = getMockSchemaObject_published_SharedScope();
SchemaInfo schemaInfo = getMockSchemaInfo_Published_SharedScope();
Mockito.when(upsertResponse.getHttpCode()).thenReturn(HttpStatus.OK);
Mockito.when(schemaServiceCore.upsertSchema(schemaRequest)).thenReturn(upsertResponse);
assertEquals(HttpStatus.OK, schemaServiceAzure.upsertSystemSchema(schemaRequest).getHttpCode());
verify(this.headers, times(1)).put(SchemaConstants.DATA_PARTITION_ID, SHARED_TENANT);
verify(this.schemaServiceCore, times(1)).upsertSchema(schemaRequest);
}
@Test
public void testUpsertSharedSchema_SuccessfulCreate () throws BadRequestException, ApplicationException {
SchemaRequest schemaRequest = getMockSchemaObject_published_SharedScope();
SchemaInfo schemaInfo = getMockSchemaInfo_Published_SharedScope();
Mockito.when(upsertResponse.getHttpCode()).thenReturn(HttpStatus.CREATED);
Mockito.when(schemaServiceCore.upsertSchema(schemaRequest)).thenReturn(upsertResponse);
assertEquals(HttpStatus.CREATED, schemaServiceAzure.upsertSystemSchema(schemaRequest).getHttpCode());
verify(this.headers, times(1)).put(SchemaConstants.DATA_PARTITION_ID, SHARED_TENANT);
verify(this.schemaServiceCore, times(1)).upsertSchema(schemaRequest);
}
@Test(expected = BadRequestException.class)
public void testUpsertSharedSchema_WhenSchemaExistInOtherTenant()
throws ApplicationException, NotFoundException, BadRequestException {
SchemaRequest schemaRequest = getMockSchemaObject_published_SharedScope();
Mockito.when(schemaServiceCore.upsertSchema(schemaRequest)).thenThrow(new BadRequestException());
schemaServiceAzure.upsertSystemSchema(schemaRequest).getHttpCode();
}
private SchemaRequest getMockSchemaObject_published_SharedScope() {
return SchemaRequest.builder().schema("{}")
.schemaInfo(SchemaInfo.builder()
.schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well")
.schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L)
.id("os:wks:well:1.1.1").build())
.dateCreated(currDate).scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build())
.build();
}
private SchemaInfo getMockSchemaInfo_Published_SharedScope() {
return SchemaInfo.builder()
.schemaIdentity(
SchemaIdentity.builder().authority("os").source("wks").entityType("well").schemaVersionMajor(1L)
.schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well:1.1.1").build())
.dateCreated(currDate).scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment