Commit 1ee36857 authored by Anastasiia Gelmut's avatar Anastasiia Gelmut
Browse files

GONRG-1191 Implement Partition Service

-PartitionService implementation added;
-PartitionServiceImplTest added.
parent 97f6e1d9
/*
Copyright 2020 Google LLC
Copyright 2020 EPAM Systems, Inc
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.partition.provider.gcp.cache;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.core.common.model.entitlements.Groups;
import org.springframework.stereotype.Component;
@Component
public class GroupCache extends VmCache<String, Groups> {
public GroupCache() {
super(30, 1000);
}
}
......@@ -18,31 +18,37 @@
package org.opengroup.osdu.partition.provider.gcp.cache;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
@Qualifier("partitionListCache")
@RequiredArgsConstructor
public class PartitionListCacheImpl implements IPartitionServiceCache<String, List<String>> {
private final ICache<String, List<String>> cache;
@Override
public void put(String s, List<String> o) {
this.cache.put(s, o);
}
@Override
public List<String> get(String s) {
return null;
return this.cache.get(s);
}
@Override
public void delete(String s) {
this.cache.delete(s);
}
@Override
public void clearAll() {
this.cache.clearAll();
}
}
......@@ -17,6 +17,8 @@
package org.opengroup.osdu.partition.provider.gcp.cache;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.partition.model.PartitionInfo;
import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache;
import org.springframework.beans.factory.annotation.Qualifier;
......@@ -24,25 +26,28 @@ import org.springframework.stereotype.Service;
@Service
@Qualifier("partitionServiceCache")
@RequiredArgsConstructor
public class PartitionServiceCacheImpl implements IPartitionServiceCache<String, PartitionInfo> {
private final ICache<String, PartitionInfo> cache;
@Override
public void put(String s, PartitionInfo o) {
this.cache.put(s, o);
}
@Override
public PartitionInfo get(String s) {
return null;
return this.cache.get(s);
}
@Override
public void delete(String s) {
this.cache.delete(s);
}
@Override
public void clearAll() {
this.cache.clearAll();
}
}
/*
Copyright 2020 Google LLC
Copyright 2020 EPAM Systems, Inc
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.partition.provider.gcp.cache;
import java.util.List;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.partition.model.PartitionInfo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class VmCacheConfiguration {
private int cacheExpiration;
private int cacheMaxSize;
@Bean(name = "partitionListCache")
public VmCache<String, List<String>> partitionListCache() {
return new VmCache<>(cacheExpiration * 60, cacheMaxSize);
}
@ConfigurationProperties
@Bean(name = "partitionServiceCache")
public VmCache<String, PartitionInfo> partitionServiceCache() {
return new VmCache<>(cacheExpiration * 60, cacheMaxSize);
}
}
package org.opengroup.osdu.partition.provider.gcp.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties
@Getter
@Setter
public class PropertiesConfiguration {
private String authorizeApi;
private String authorizeApiKey;
private int cacheExpiration;
private int cacheMaxSize;
}
......@@ -29,7 +29,7 @@ public interface PartitionPropertyEntityRepository extends
List<PartitionPropertyEntity> findByPartitionId(String partitionId);
PartitionPropertyEntity findByName(String partitionId, String name);
PartitionPropertyEntity findByPartitionIdAndName(String partitionId, String name);
void deleteByPartitionId(String partitionId);
......
......@@ -20,17 +20,14 @@ package org.opengroup.osdu.partition.provider.gcp.security;
import org.opengroup.osdu.core.common.entitlements.EntitlementsAPIConfig;
import org.opengroup.osdu.core.common.entitlements.EntitlementsFactory;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class EntitlementsClientFactory extends AbstractFactoryBean<IEntitlementsFactory> {
@Value("${AUTHORIZE_API}")
private String authorizeApi;
@Value("${AUTHORIZE_API_KEY:}")
private String authorizeApiKey;
@Override
......
......@@ -17,9 +17,11 @@
package org.opengroup.osdu.partition.provider.gcp.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.model.http.AppException;
......@@ -34,6 +36,8 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
public class PartitionServiceImpl implements IPartitionService {
private static final String UNKNOWN_ERROR_REASON = "unknown error";
private final PartitionPropertyEntityRepository partitionPropertyEntityRepository;
@Override
......@@ -44,10 +48,10 @@ public class PartitionServiceImpl implements IPartitionService {
entry.getKey(), entry.getValue());
partitionPropertyEntityRepository.save(partitionPropertyEntity);
}
return partitionInfo;
return getPartition(partitionId);
} else {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unknown error",
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, UNKNOWN_ERROR_REASON,
"Partition already exists.");
}
......@@ -56,13 +60,13 @@ public class PartitionServiceImpl implements IPartitionService {
@Override
public PartitionInfo updatePartition(String partitionId, PartitionInfo partitionInfo) {
if (!partitionExists(partitionId)) {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unknown error",
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, UNKNOWN_ERROR_REASON,
"An attempt to update not existing partition.");
} else {
for (Map.Entry<String, Property> entry : partitionInfo.getProperties().entrySet()) {
PartitionPropertyEntity entity = partitionPropertyEntityRepository.findByName(partitionId,
entry.getKey());
PartitionPropertyEntity entity = partitionPropertyEntityRepository
.findByPartitionIdAndName(partitionId, entry.getKey());
if (entity != null) {
entity.setSensitive(entry.getValue().isSensitive());
entity.setValue(entry.getValue().getValue());
......@@ -70,11 +74,10 @@ public class PartitionServiceImpl implements IPartitionService {
} else {
entity = new PartitionPropertyEntity(partitionId, entry.getKey(), entry.getValue());
}
//ToDo updating doesn't work
partitionPropertyEntityRepository.save(entity);
}
}
return partitionInfo;
return getPartition(partitionId);
}
@Override
......@@ -93,7 +96,7 @@ public class PartitionServiceImpl implements IPartitionService {
return partitionInfo;
} else {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unknown error",
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, UNKNOWN_ERROR_REASON,
"Partition does not exist.");
}
......@@ -106,7 +109,7 @@ public class PartitionServiceImpl implements IPartitionService {
return true;
} else {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unknown error",
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, UNKNOWN_ERROR_REASON,
"An attempt to delete not existing partition.");
}
......@@ -114,7 +117,13 @@ public class PartitionServiceImpl implements IPartitionService {
@Override
public List<String> getAllPartitions() {
return null;
Iterable<PartitionPropertyEntity> entities = partitionPropertyEntityRepository.findAll();
List<String> allPartitions = new ArrayList<>();
entities.forEach(
partitionPropertyEntity -> allPartitions.add(partitionPropertyEntity.getPartitionId()));
List<String> distinctPartitions = allPartitions.stream().distinct()
.collect(Collectors.toList());
return distinctPartitions.isEmpty() ? null : distinctPartitions;
}
private boolean partitionExists(String partitionId) {
......
......@@ -6,6 +6,9 @@ AUTHORIZE_API=https://os-entitlements-gcp-jvmvia5dea-uc.a.run.app/entitlements/v
spring.cloud.gcp.datastore.namespace=partitiontest
#ACCEPT_HTTP=true
cache_expiration=1
cache_maxSize=1000
#logging configuration
logging.level.org.springframework.web=DEBUG
logging.level.org.opengroup.osdu=debug
......
package org.opengroup.osdu.partition.provider.gcp.service;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.partition.model.PartitionInfo;
import org.opengroup.osdu.partition.model.Property;
import org.opengroup.osdu.partition.provider.gcp.model.PartitionPropertyEntity;
import org.opengroup.osdu.partition.provider.gcp.repository.PartitionPropertyEntityRepository;
@RunWith(MockitoJUnitRunner.class)
public class PartitionServiceImplTest {
@Mock
private PartitionPropertyEntityRepository partitionPropertyEntityRepository;
@InjectMocks
private PartitionServiceImpl partitionServiceImpl;
@Test
public void should_createPartition_when_partitionDoesNotExist() {
String partitionId = "newPartition";
boolean sensitive = false;
String name = "new-key";
String value = "new-value";
PartitionInfo expectedPartitionInfo = new PartitionInfo();
Property property = new Property();
property.setSensitive(sensitive);
property.setValue(value);
Map<String, Property> properties = new HashMap<>();
properties.put(name, property);
expectedPartitionInfo.setProperties(properties);
when(partitionPropertyEntityRepository.save(any())).thenReturn(new PartitionPropertyEntity());
PartitionPropertyEntity partitionPropertyEntity = new PartitionPropertyEntity();
partitionPropertyEntity.setPartitionId(partitionId);
partitionPropertyEntity.setName(name);
partitionPropertyEntity.setSensitive(sensitive);
partitionPropertyEntity.setValue(value);
List<PartitionPropertyEntity> partitionPropertyEntityList = new ArrayList<>();
partitionPropertyEntityList.add(partitionPropertyEntity);
List<PartitionPropertyEntity> emptyList = new ArrayList<>();
when(partitionPropertyEntityRepository.findByPartitionId(partitionId))
.thenReturn(emptyList, partitionPropertyEntityList);
PartitionInfo actualPartitionInfo = partitionServiceImpl
.createPartition(partitionId, expectedPartitionInfo);
assertEquals(expectedPartitionInfo, actualPartitionInfo);
}
@Test(expected = AppException.class)
public void should_throwAnException_when_createPartitionWhichAlreadyExists() {
String partitionId = "newPartition";
PartitionInfo partitionInfo = new PartitionInfo();
List<PartitionPropertyEntity> emptyList = new ArrayList<>();
when(partitionPropertyEntityRepository.findByPartitionId(partitionId)).thenReturn(emptyList);
partitionServiceImpl.createPartition(partitionId, partitionInfo);
}
@Test
public void should_updatePartition_when_partitionExists() {
}
@Test(expected = AppException.class)
public void should_throwAnException_when_updatePartitionWhichDoesNotExist() {
String partitionId = "newPartition";
PartitionInfo partitionInfo = new PartitionInfo();
List<PartitionPropertyEntity> emptyList = new ArrayList<>();
when(partitionPropertyEntityRepository.findByPartitionId(partitionId)).thenReturn(emptyList);
partitionServiceImpl.createPartition(partitionId, partitionInfo);
}
@Test
public void should_getPartition_when_partitionExists() {
}
@Test(expected = AppException.class)
public void should_throwAnException_when_getPartitionWhichDoesNotExist() {
String partitionId = "newPartition";
List<PartitionPropertyEntity> emptyList = new ArrayList<>();
when(partitionPropertyEntityRepository.findByPartitionId(partitionId)).thenReturn(emptyList);
partitionServiceImpl.getPartition(partitionId);
}
@Test
public void should_deletePartition_when_partitionExists() {
}
@Test(expected = AppException.class)
public void should_throwAnException_when_deletePartitionWhichDoesNotExist() {
String partitionId = "newPartition";
List<PartitionPropertyEntity> emptyList = new ArrayList<>();
when(partitionPropertyEntityRepository.findByPartitionId(partitionId)).thenReturn(emptyList);
partitionServiceImpl.deletePartition(partitionId);
}
@Test
public void should_getAllPartitions() {
}
}
\ No newline at end of file
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