Commits (2)
......@@ -28,6 +28,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule;
import org.apache.commons.logging.Log;
......@@ -449,6 +450,7 @@ public class ServiceRepoService {
boolean serviceCharacteristicChanged = false;
boolean serviceCharacteristicChangedContainsPrimitive = false;
boolean serviceCharacteristicChangedContainsNSLCM = false;
String charChangedForNotes = "";
List<Characteristic> childCharacteristicsChanged = new ArrayList<>();
......@@ -474,13 +476,25 @@ public class ServiceRepoService {
if ( n.getName().toUpperCase().contains( "PRIMITIVE::" ) ){
serviceCharacteristicChangedContainsPrimitive = true;
}
// Check if the name contains "NSLCM"
if (n.getName().toUpperCase().contains("NSLCM")) {
// Flag to indicate that service characteristic with NSLCM is present
serviceCharacteristicChangedContainsNSLCM = true;
// Update the NSLCM Characteristic
updateNSLCMCharacteristic(service, n);
}
}
}
service.getServiceCharacteristicByName( n.getName() ).setValue(
new Any( n.getValue().getValue(), n.getValue().getAlias() )
);
// As the NSLCM Characteristic was already updated, skip that one
if (!serviceCharacteristicChangedContainsNSLCM) {
service.getServiceCharacteristicByName( n.getName() ).setValue(
new Any( n.getValue().getValue(), n.getValue().getAlias() )
);
}
} else {
service.addServiceCharacteristicItem(n);
serviceCharacteristicChanged = true;
......@@ -679,6 +693,58 @@ public class ServiceRepoService {
return service;
}
/**
* Updates the NSLCM characteristic within a given service.
*
* @param service The service object containing the characteristics.
* @param n The characteristic object to be checked and potentially updated.
*/
public void updateNSLCMCharacteristic(Service service, Characteristic n) {
// Create an object mapper for JSON serialization/deserialization
ObjectMapper primitivesObjectMapper = new ObjectMapper();
// Retrieve the service characteristic based on the name
Characteristic NSLCMCharacteristic = service.getServiceCharacteristicByName(n.getName());
// Retrieve the current value of the characteristic
Any NSLCMCharacteristicValue = NSLCMCharacteristic.getValue();
// Retrieve the current value as a string directly from the service characteristic
String ServiceNSLCMValue = service.getServiceCharacteristicByName(n.getName()).getValue().getValue();
// Check if the current service characteristic value is null or explicitly "null" and initialize if needed
if (ServiceNSLCMValue == null || "null".equals(ServiceNSLCMValue)) {
service.getServiceCharacteristicByName(n.getName()).getValue().setValue("[]");
}
// Check if the current characteristic value is not null and not explicitly "null"
if (NSLCMCharacteristicValue.getValue() != null || !"null".equals(NSLCMCharacteristicValue.getValue())) {
// Convert the current characteristic value to a string
String NSLCMCharacteristicValueString = NSLCMCharacteristicValue.getValue();
ArrayList<String> arrayList = null;
// Deserialize the current value back to an array list
try {
arrayList = primitivesObjectMapper.readValue(NSLCMCharacteristicValueString, new TypeReference<ArrayList<String>>() {});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
// Add the new value to the list if it's not already present and is not null
if (!arrayList.contains(n.getValue().getValue()) && n.getValue().getValue() != null) {
arrayList.add(n.getValue().getValue());
}
// Update the characteristic with the newly modified list
try {
NSLCMCharacteristic.setValue(new Any(primitivesObjectMapper.writeValueAsString(arrayList), n.getValue().getAlias()));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
/**
* @param service
* @param parentService
......
......@@ -2,40 +2,39 @@
* @Author: Eduardo Santos
* @Date: 2024-05-01 19:42:14
* @Last Modified by: Eduardo Santos
* @Last Modified time: 2024-05-08 14:01:04
* @Last Modified time: 2024-05-09 16:58:32
*/
package org.etsi.osl.services.services;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.util.Optional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.etsi.osl.tmf.OpenAPISpringBoot;
import org.etsi.osl.tmf.common.model.Any;
import org.etsi.osl.tmf.common.model.service.Characteristic;
import org.etsi.osl.tmf.common.model.service.Note;
import org.etsi.osl.tmf.common.model.service.Place;
import org.etsi.osl.tmf.common.model.service.ServiceStateType;
import org.etsi.osl.tmf.prm669.model.RelatedParty;
import org.etsi.osl.tmf.sim638.model.Service;
import org.etsi.osl.tmf.sim638.model.ServiceUpdate;
import org.etsi.osl.tmf.sim638.repo.ServiceActionQueueRepository;
import org.etsi.osl.tmf.sim638.repo.ServiceRepository;
import org.etsi.osl.tmf.sim638.service.ServiceRepoService;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ActiveProfiles;
......@@ -43,8 +42,6 @@ import org.springframework.test.context.junit4.SpringRunner;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityManagerFactory;
@RunWith(SpringRunner.class)
@ActiveProfiles("testing")
@SpringBootTest(classes = OpenAPISpringBoot.class)
......@@ -59,32 +56,24 @@ public class ServiceRepoServiceTest {
private static Service initialService;
private static Service finalService;
private static ServiceUpdate servUpd;
private static ObjectMapper mapper;
private static ObjectMapper objectMapper;
@BeforeClass
public static void setupBeforeClass() {
try {
mapper = new ObjectMapper();
objectMapper = new ObjectMapper();
initialService = mapper.readValue(
initialService = objectMapper.readValue(
new File(
"src/test/resources/ServiceRepoServiceTests/18:53:38.012079607/initial_service.json"),
"src/test/resources/ServiceRepoServiceTests/18:50:47.858919041/initial_service.json"),
Service.class
);
finalService = mapper.readValue(
servUpd = objectMapper.readValue(
new File(
"src/test/resources/ServiceRepoServiceTests/18:53:38.012079607/final_service.json"),
Service.class
);
servUpd = mapper.readValue(
new File(
"src/test/resources/ServiceRepoServiceTests/18:53:38.012079607/supd.json"),
"src/test/resources/ServiceRepoServiceTests/18:50:47.858919041/supd.json"),
ServiceUpdate.class
);
......@@ -131,37 +120,133 @@ public class ServiceRepoServiceTest {
verify(serviceRepoService, times(2)).getServiceEager(anyString());
}
//@Test
//public void testWhenInitialServiceThenReturnFinalService() {
// // Setup the expectation
// when(serviceRepoService.updateService(
// "123",
// servUpd,
// false,
// null,
// null)
// ).thenReturn(finalService);
//
// // When method getServiceEager is called, it will retrieve
// // an initialService, but the updateService should retrieve
// // a finalService
// Service result = serviceRepoService.updateService("123", servUpd, false, null, null);
//
// // Assert that the result is equal to the finalService content
// assertEquals(result, finalService);
//}
//@Test
//public void testAddPrimitiveCharacteristicValuesToArrayList (){
// Service result = serviceRepoService.updateService("123", servUpd, false, null, null);
//
// System.out.println("The result is: " + result);
//
// assertNotNull(result);
//}
@Test
public void testUpdateNSLCMCharacteristicMethodWhenNSLCMValueToUpdateIsNull() {
Service service = initialService;
// Mimic initial behaviour of the updateService method
updateServiceDetails(service, servUpd);
if ( servUpd.getServiceCharacteristic()!=null ) {
for (Characteristic n : servUpd.getServiceCharacteristic()) {
if ( service.getServiceCharacteristicByName( n.getName() )!= null ) {
Characteristic origChar = service.getServiceCharacteristicByName( n.getName() );
if ( ( origChar !=null ) && ( origChar.getValue() !=null ) && ( origChar.getValue().getValue() !=null )) {
if ( !origChar.getValue().getValue().equals(n.getValue().getValue()) ) {
// Check if the name contains "NSLCM" in any case
if (n.getName().toUpperCase().contains("NSLCM")) {
// Set the value of NSLCM to null
n.getValue().setValue(null);
serviceRepoService.updateNSLCMCharacteristic(service, n);
assertEquals("[]", service.getServiceCharacteristicByName(n.getName()).getValue().getValue());
}
}
}
}
}
}
System.out.println("service is: " + service);
}
@Test
public void testUpdateNSLCMCharacteristicMethodWhenNSLCMValueToUpdateIsNotNullAndNSLCMDoesntAlreadyExist() {
Service service = initialService;
// Mimic initial behaviour of the updateService method
updateServiceDetails(service, servUpd);
if ( servUpd.getServiceCharacteristic()!=null ) {
for (Characteristic n : servUpd.getServiceCharacteristic()) {
if ( service.getServiceCharacteristicByName( n.getName() )!= null ) {
Characteristic origChar = service.getServiceCharacteristicByName( n.getName() );
if ( ( origChar !=null ) && ( origChar.getValue() !=null ) && ( origChar.getValue().getValue() !=null )) {
if ( !origChar.getValue().getValue().equals(n.getValue().getValue()) ) {
// Check if the name contains "NSLCM" in any case
if (n.getName().toUpperCase().contains("NSLCM")) {
serviceRepoService.updateNSLCMCharacteristic(service, n);
System.out.println("service.getServiceCharacteristicByName(n.getName()).getValue().getValue(): " + service.getServiceCharacteristicByName(n.getName()).getValue().getValue());
assertEquals(
"[\"{\\\"queuePosition\\\":0,\\\"lcmOperationType\\\":\\\"instantiate\\\",\\\"detailed-status\\\":\\\"Done\\\",\\\"operationState\\\":\\\"COMPLETED\\\",\\\"errorMessage\\\":null,\\\"nsInstanceId\\\":\\\"26da619d-190a-468f-9847-10a67f58ed55\\\",\\\"_admin\\\":{\\\"projects_write\\\":[\\\"b5b1b949-33d6-4b60-a1b9-ac3be0f32829\\\"],\\\"created\\\":1.7145890775375292E9,\\\"modified\\\":1.714589413680587E9,\\\"worker\\\":\\\"3c542ab5c513\\\",\\\"projects_read\\\":[\\\"b5b1b949-33d6-4b60-a1b9-ac3be0f32829\\\"]},\\\"detailedStatus\\\":null,\\\"stage\\\":\\\"\\\",\\\"operationParams\\\":{\\\"nsInstanceId\\\":\\\"26da619d-190a-468f-9847-10a67f58ed55\\\",\\\"ssh_keys\\\":[\\\"\\\"],\\\"lcmOperationType\\\":\\\"instantiate\\\",\\\"nsdId\\\":\\\"e09cfccf-44b7-474a-a67e-4acee85a5240\\\",\\\"nsName\\\":\\\"Service_Order_e8831a4c-6a13-48fc-ac60-9ab7ff813a78\\\",\\\"vimAccountId\\\":\\\"fc0e7959-3d3c-4a58-bb8b-862cf0f4fcc1\\\"},\\\"startTime\\\":1.7145890775374498E9,\\\"links\\\":{\\\"nsInstance\\\":\\\"/osm/nslcm/v1/ns_instances/26da619d-190a-468f-9847-10a67f58ed55\\\",\\\"self\\\":\\\"/osm/nslcm/v1/ns_lcm_op_occs/b2c72a49-28b3-47ca-a5c1-c0d4cb384589\\\"},\\\"_id\\\":\\\"b2c72a49-28b3-47ca-a5c1-c0d4cb384589\\\",\\\"id\\\":\\\"b2c72a49-28b3-47ca-a5c1-c0d4cb384589\\\",\\\"isAutomaticInvocation\\\":false,\\\"isCancelPending\\\":false,\\\"statusEnteredTime\\\":1.714589413680586E9}\"]",
service.getServiceCharacteristicByName(n.getName()).getValue().getValue()
);
}
}
}
}
}
}
System.out.println("service is: " + service);
}
@Test
public void testUpdateNSLCMCharacteristicMethodWhenNSLCMValueToUpdateIsNotNullAndNSLCMAlreadyExists() {
Service service = initialService;
// Mimic initial behaviour of the updateService method
updateServiceDetails(service, servUpd);
if ( servUpd.getServiceCharacteristic()!=null ) {
for (Characteristic n : servUpd.getServiceCharacteristic()) {
if ( service.getServiceCharacteristicByName( n.getName() )!= null ) {
Characteristic origChar = service.getServiceCharacteristicByName( n.getName() );
if ( ( origChar !=null ) && ( origChar.getValue() !=null ) && ( origChar.getValue().getValue() !=null )) {
if ( !origChar.getValue().getValue().equals(n.getValue().getValue()) ) {
// Check if the name contains "NSLCM" in any case
if (n.getName().toUpperCase().contains("NSLCM")) {
// Set the value of NSLCM to null
service.getServiceCharacteristicByName(n.getName()).getValue().setValue("[\"existingValue\"]");
serviceRepoService.updateNSLCMCharacteristic(service, n);
assertEquals(
"[\"existingValue\",\"{\\\"queuePosition\\\":0,\\\"lcmOperationType\\\":\\\"instantiate\\\",\\\"detailed-status\\\":\\\"Done\\\",\\\"operationState\\\":\\\"COMPLETED\\\",\\\"errorMessage\\\":null,\\\"nsInstanceId\\\":\\\"26da619d-190a-468f-9847-10a67f58ed55\\\",\\\"_admin\\\":{\\\"projects_write\\\":[\\\"b5b1b949-33d6-4b60-a1b9-ac3be0f32829\\\"],\\\"created\\\":1.7145890775375292E9,\\\"modified\\\":1.714589413680587E9,\\\"worker\\\":\\\"3c542ab5c513\\\",\\\"projects_read\\\":[\\\"b5b1b949-33d6-4b60-a1b9-ac3be0f32829\\\"]},\\\"detailedStatus\\\":null,\\\"stage\\\":\\\"\\\",\\\"operationParams\\\":{\\\"nsInstanceId\\\":\\\"26da619d-190a-468f-9847-10a67f58ed55\\\",\\\"ssh_keys\\\":[\\\"\\\"],\\\"lcmOperationType\\\":\\\"instantiate\\\",\\\"nsdId\\\":\\\"e09cfccf-44b7-474a-a67e-4acee85a5240\\\",\\\"nsName\\\":\\\"Service_Order_e8831a4c-6a13-48fc-ac60-9ab7ff813a78\\\",\\\"vimAccountId\\\":\\\"fc0e7959-3d3c-4a58-bb8b-862cf0f4fcc1\\\"},\\\"startTime\\\":1.7145890775374498E9,\\\"links\\\":{\\\"nsInstance\\\":\\\"/osm/nslcm/v1/ns_instances/26da619d-190a-468f-9847-10a67f58ed55\\\",\\\"self\\\":\\\"/osm/nslcm/v1/ns_lcm_op_occs/b2c72a49-28b3-47ca-a5c1-c0d4cb384589\\\"},\\\"_id\\\":\\\"b2c72a49-28b3-47ca-a5c1-c0d4cb384589\\\",\\\"id\\\":\\\"b2c72a49-28b3-47ca-a5c1-c0d4cb384589\\\",\\\"isAutomaticInvocation\\\":false,\\\"isCancelPending\\\":false,\\\"statusEnteredTime\\\":1.714589413680586E9}\"]",
service.getServiceCharacteristicByName(n.getName()).getValue().getValue()
);
}
}
}
}
}
}
System.out.println("service is: " + service);
}
/**
* Updates the details of the given service based on the non-null values of the provided service update object.
*
* @param service The service object to be updated.
* @param servUpd The service update object containing new values.
*/
public void updateServiceDetails(Service service, ServiceUpdate servUpd) {
if (servUpd.getType() != null) service.setType(servUpd.getType());
if (servUpd.getName() != null) service.setName(servUpd.getName());
if (servUpd.getCategory() != null) service.setCategory(servUpd.getCategory());
if (servUpd.getDescription() != null) service.setDescription(servUpd.getDescription());
if (servUpd.getStartDate() != null) service.setStartDate(servUpd.getStartDate());
if (servUpd.getEndDate() != null) service.setEndDate(servUpd.getEndDate());
if (servUpd.isHasStarted() != null) service.setHasStarted(servUpd.isHasStarted());
if (servUpd.isIsServiceEnabled() != null) service.setIsServiceEnabled(servUpd.isIsServiceEnabled());
if (servUpd.isIsStateful() != null) service.setIsStateful(servUpd.isIsStateful());
if (servUpd.getServiceDate() != null) service.setServiceDate(servUpd.getServiceDate());
if (servUpd.getServiceType() != null) service.setServiceType(servUpd.getServiceType());
if (servUpd.getStartMode() != null) service.setStartMode(servUpd.getStartMode());
if (servUpd.getState() != null) service.setState(servUpd.getState());
if (servUpd.getServiceSpecificationRef() != null) service.setServiceSpecificationRef(servUpd.getServiceSpecificationRef());
}
}