Skip to content
Snippets Groups Projects
Commit 1875320a authored by Labros Papadopoulos's avatar Labros Papadopoulos
Browse files

Merge branch 'develop' into tmf-674-feature

parents 0ea8555d 5d1afedf
No related branches found
No related tags found
3 merge requests!36Merging 2024Q2_RC into main, creating 2024Q2 Release,!24Tmf 674 feature,!19Tmf 674 feature
/*-
* ========================LICENSE_START=================================
* org.etsi.osl.tmf.api
* %%
* Copyright (C) 2019 - 2024 openslice.io
* %%
* 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.
* =========================LICENSE_END==================================
*/
package org.etsi.osl.services.api.sim638;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.etsi.osl.tmf.sim638.api.*;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import java.io.IOException;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class CommonTests {
@Test
public void testApiException() {
int errorCode = 404;
String errorMessage = "Not Found";
ApiException exception = new ApiException(errorCode, errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
public void testApiOriginFilter() throws IOException, ServletException {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);
ApiOriginFilter filter = new ApiOriginFilter();
filter.doFilter(request, response, chain);
verify(response).addHeader("Access-Control-Allow-Origin", "*");
verify(response).addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
verify(response).addHeader("Access-Control-Allow-Headers", "Content-Type");
verify(chain).doFilter(request, response);
}
@Test
public void testApiResponseMessage() {
int[] codes = {ApiResponseMessage.ERROR, ApiResponseMessage.WARNING, ApiResponseMessage.INFO, ApiResponseMessage.OK, ApiResponseMessage.TOO_BUSY, 6};
String[] types = {"error", "warning", "info", "ok", "too busy", "unknown"};
String[] messages = {"An error occured", "This is a warning", "Given info", "ok", "System is too busy", "unknown code"};
for (int i = 0; i < codes.length; i++) {
int code = codes[i];
String type = types[i];
String message = messages[i];
ApiResponseMessage responseMessage = new ApiResponseMessage(code, message);
assertEquals(message, responseMessage.getMessage());
assertEquals(code, responseMessage.getCode());
assertEquals(type, responseMessage.getType());
}
ApiResponseMessage responseMessage = new ApiResponseMessage();
responseMessage.setMessage("Error");
assertEquals("Error", responseMessage.getMessage());
responseMessage.setType("ok");
assertEquals("ok", responseMessage.getType());
responseMessage.setCode(ApiResponseMessage.OK);
assertEquals(ApiResponseMessage.OK, responseMessage.getCode());
}
@Test
public void testHubApiController() {
ObjectMapper objectMapper = new ObjectMapper();
HttpServletRequest request = new MockHttpServletRequest();
HubApiController controller = new HubApiController(objectMapper, request);
Optional<ObjectMapper> returnedObjectMapper = controller.getObjectMapper();
Optional<HttpServletRequest> returnedRequest = controller.getRequest();
assertTrue(returnedObjectMapper.isPresent());
assertTrue(returnedRequest.isPresent());
assertEquals(objectMapper, returnedObjectMapper.get());
assertEquals(request, returnedRequest.get());
}
@Test
public void testListenerApiController() {
ObjectMapper objectMapper = new ObjectMapper();
HttpServletRequest request = new MockHttpServletRequest();
ListenerApiController controller = new ListenerApiController(objectMapper, request);
Optional<ObjectMapper> returnedObjectMapper = controller.getObjectMapper();
Optional<HttpServletRequest> returnedRequest = controller.getRequest();
assertTrue(returnedObjectMapper.isPresent());
assertTrue(returnedRequest.isPresent());
assertEquals(objectMapper, returnedObjectMapper.get());
assertEquals(request, returnedRequest.get());
}
@Test
public void testNotFoundException() {
int errorCode = 404;
String errorMessage = "Not Found";
NotFoundException exception = new NotFoundException(errorCode, errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
}
package org.etsi.osl.services.api.sim638;
import java.text.FieldPosition;
import java.util.Date;
import org.etsi.osl.tmf.sim638.api.RFC3339DateFormat;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RFC3339DateFormatTest {
@Test
public void testFormat() {
RFC3339DateFormat rfc3339DateFormat = new RFC3339DateFormat();
Date date = new Date(1637484321000L);
StringBuffer stringBuffer = new StringBuffer();
FieldPosition fieldPosition = new FieldPosition(0);
StringBuffer formattedDate = rfc3339DateFormat.format(date, stringBuffer, fieldPosition);
assertEquals("2021-11-21T08:45:21.000Z", formattedDate.toString());
}
}
package org.etsi.osl.services.api.sim638;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.etsi.osl.tmf.JsonUtils;
import org.etsi.osl.tmf.OpenAPISpringBoot;
import org.etsi.osl.tmf.common.model.service.ServiceSpecificationRef;
import org.etsi.osl.tmf.scm633.model.ServiceSpecification;
import org.etsi.osl.tmf.scm633.model.ServiceSpecificationCreate;
import org.etsi.osl.tmf.sim638.api.ServiceApiController;
import org.etsi.osl.tmf.sim638.model.Service;
import org.etsi.osl.tmf.sim638.model.ServiceCreate;
import org.etsi.osl.tmf.sim638.service.ServiceRepoService;
import org.etsi.osl.tmf.so641.model.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.Principal;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class)
//@AutoConfigureTestDatabase //this automatically uses h2
@AutoConfigureMockMvc
@ActiveProfiles("testing")
//@TestPropertySource(
// locations = "classpath:application-testing.yml")
public class ServiceApiControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
ServiceRepoService serviceRepoService;
@Autowired
private WebApplicationContext context;
@Autowired
private ObjectMapper objectMapper;
private ServiceApiController mockServiceApiController;
private ServiceRepoService mockServiceRepoService;
@Before
public void setup() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
// Mocks
mockServiceRepoService = mock(ServiceRepoService.class);
ObjectMapper mockObjectMapper = mock(ObjectMapper.class);
mockServiceApiController = new ServiceApiController(mockObjectMapper, null);
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testDeleteService() throws Exception {
String response = createService();
Service responsesService = JsonUtils.toJsonObj(response, Service.class);
String id = responsesService.getId();
mvc.perform(MockMvcRequestBuilders.delete("/serviceInventory/v4/service/" + id )
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is(501) );
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testListService() throws Exception {
String response = mvc.perform(MockMvcRequestBuilders.get("/serviceInventory/v4/service" )
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk() )
.andReturn().getResponse().getContentAsString();
List<Service> serviceList = objectMapper.readValue(response, new TypeReference<List<Service>>() {});
assertThat(serviceList.size()).isEqualTo(serviceRepoService.findAll().size());
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testRetrieveService() throws Exception {
String response = createService();
Service responsesService = JsonUtils.toJsonObj(response, Service.class);
String id = responsesService.getId();
String response2 = mvc.perform(MockMvcRequestBuilders.get("/serviceInventory/v4/service/" + id)
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk() )
.andReturn().getResponse().getContentAsString();
Service responsesService2 = JsonUtils.toJsonObj(response2, Service.class);
assertThat(responsesService2.getId()).isEqualTo(responsesService.getId());
assertThat(responsesService2.getCategory()).isEqualTo(responsesService.getCategory());
assertThat(responsesService2.getName()).isEqualTo(responsesService.getName());
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testCreateServiceHandleException() {
ServiceCreate serviceCreate = new ServiceCreate();
serviceCreate.setName("Test name");
when(mockServiceRepoService.addService(any()))
.thenThrow(new RuntimeException("Test exception"));
ResponseEntity<Service> response = mockServiceApiController.createService(mock(Principal.class), serviceCreate);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testListServiceHandleException(){
when(mockServiceRepoService.findAll())
.thenThrow(new RuntimeException("Test exception"));
ResponseEntity<List<Service>> response = mockServiceApiController.listService(mock(Principal.class), null, null, null);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testRetrieveServiceHandleException(){
when(mockServiceRepoService.findByUuid(any()))
.thenThrow(new RuntimeException("Test exception"));
ResponseEntity<Service> response = mockServiceApiController.retrieveService(mock(Principal.class), "test id", "");
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
private String createService() throws Exception {
int servicesCount = serviceRepoService.findAll().size();
File sspec = new File("src/test/resources/testServiceSpec.json");
InputStream in = new FileInputStream(sspec);
String sspectext = IOUtils.toString(in, "UTF-8");
ServiceSpecificationCreate sspeccr = JsonUtils.toJsonObj(sspectext, ServiceSpecificationCreate.class);
sspeccr.setName("Spec1");
ServiceSpecification responsesSpec = createServiceSpec( sspeccr);
ServiceOrderCreate serviceOrder = new ServiceOrderCreate();
serviceOrder.setCategory("Test Category");
serviceOrder.setDescription("A Test Service");
serviceOrder.setRequestedStartDate(OffsetDateTime.now(ZoneOffset.UTC).toString());
serviceOrder.setRequestedCompletionDate(OffsetDateTime.now(ZoneOffset.UTC).toString());
ServiceOrderItem soi = new ServiceOrderItem();
serviceOrder.getOrderItem().add(soi);
soi.setState(ServiceOrderStateType.ACKNOWLEDGED);
ServiceRestriction serviceRestriction = new ServiceRestriction();
ServiceSpecificationRef aServiceSpecificationRef = new ServiceSpecificationRef();
aServiceSpecificationRef.setId(responsesSpec.getId());
aServiceSpecificationRef.setName(responsesSpec.getName());
serviceRestriction.setServiceSpecification(aServiceSpecificationRef);
serviceRestriction.setName("aServiceRestriction");
soi.setService(serviceRestriction);
String response = mvc
.perform(MockMvcRequestBuilders.post("/serviceInventory/v4/service")
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON).content(JsonUtils.toJson(serviceOrder)))
.andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
Service responseService = JsonUtils.toJsonObj(response, Service.class);
assertThat( serviceRepoService.findAll().size() ).isEqualTo( servicesCount + 1 );
assertThat(responseService.getCategory()).isEqualTo("Test Category");
assertThat(responseService.getDescription()).isEqualTo("A Test Service");
return response;
}
private ServiceSpecification createServiceSpec( ServiceSpecificationCreate serviceSpecificationCreate) throws Exception {
String response = mvc
.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/serviceSpecification")
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON).content(JsonUtils.toJson(serviceSpecificationCreate)))
.andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class);
return responsesSpec;
}
}
package org.etsi.osl.services.api.sim638;
import org.apache.commons.io.IOUtils;
import org.etsi.osl.model.nfv.DeploymentDescriptor;
import org.etsi.osl.tmf.JsonUtils;
import org.etsi.osl.tmf.OpenAPISpringBoot;
import org.etsi.osl.tmf.common.model.Any;
import org.etsi.osl.tmf.common.model.AttachmentRefOrValue;
import org.etsi.osl.tmf.common.model.UserPartRoleType;
import org.etsi.osl.tmf.common.model.service.*;
import org.etsi.osl.tmf.common.model.service.Characteristic;
import org.etsi.osl.tmf.common.model.service.Place;
import org.etsi.osl.tmf.prm669.model.RelatedParty;
import org.etsi.osl.tmf.rcm634.model.LogicalResourceSpecification;
import org.etsi.osl.tmf.rcm634.model.LogicalResourceSpecificationCreate;
import org.etsi.osl.tmf.rcm634.model.ResourceSpecificationRef;
import org.etsi.osl.tmf.ri639.model.*;
import org.etsi.osl.tmf.ri639.reposervices.ResourceRepoService;
import org.etsi.osl.tmf.scm633.model.ServiceSpecification;
import org.etsi.osl.tmf.scm633.model.ServiceSpecificationCreate;
import org.etsi.osl.tmf.sim638.model.Service;
import org.etsi.osl.tmf.sim638.model.ServiceActionQueueItem;
import org.etsi.osl.tmf.sim638.model.ServiceCreate;
import org.etsi.osl.tmf.sim638.model.ServiceUpdate;
import org.etsi.osl.tmf.sim638.service.ServiceRepoService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class)
//@AutoConfigureTestDatabase //this automatically uses h2
@AutoConfigureMockMvc
@ActiveProfiles("testing")
//@TestPropertySource(
// locations = "classpath:application-testing.yml")
public class ServiceRepoServiceTest {
@Autowired
private MockMvc mvc;
@Autowired
ServiceRepoService serviceRepoService;
@Autowired
ResourceRepoService resourceRepoService;
@Autowired
private WebApplicationContext context;
@Before
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testFindAll() throws Exception {
String response = createService();
Service responsesService = JsonUtils.toJsonObj(response, Service.class);
String id = responsesService.getId();
List<Service> serviceList = serviceRepoService.findAll("Test Party", UserPartRoleType.REQUESTER);
boolean idExists = false;
for (Service s : serviceList) {
if ( s.getId().equals( id ) ) {
idExists= true;
}
}
assertThat( idExists ).isTrue();
}
// AlarmManagementIntegrationTest.testAlarmCreateAndUpdateRoutes:224 » CamelExecution Exception occurred during execution on the exchange: Exchange[0054D8F070E6449-0000000000000001]
// PartyManagementIntegrationTest.addOrganization:246 » JDBCConnection Unable to acquire JDBC Connection [HikariPool-1 - Connection is not available, request timed out after 30000ms.]
// @WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
// @Test
// public void testUpdateService() throws Exception {
// String response = createService();
// Service responsesService = JsonUtils.toJsonObj(response, Service.class);
// String id = responsesService.getId();
//
// ServiceUpdate serviceUpdate = createServiceUpdateObject();
// serviceRepoService.updateService(id, serviceUpdate, true, null, null);
//
// Service updatedService = serviceRepoService.findByUuid(id);
// assertThat(updatedService.getType()).isEqualTo("Updated type");
// assertThat(updatedService.getName()).isEqualTo("Updated name");
// assertThat(updatedService.getServiceType()).isEqualTo("Updated Service Type");
// }
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testDeleteServiceActionQueueItemByUuid() throws Exception {
ServiceActionQueueItem saqi = new ServiceActionQueueItem();
ServiceActionQueueItem saqiResponse = serviceRepoService.addServiceActionQueueItem(saqi);
String uuid = saqiResponse.getUuid();
serviceRepoService.deleteServiceActionQueueItemByUuid(uuid);
List<ServiceActionQueueItem> saqiList = serviceRepoService.findAllServiceActionQueueItems();
boolean idExists = false;
for (ServiceActionQueueItem s : saqiList) {
if ( s.getUuid().equals( uuid ) ) {
idExists= true;
break;
}
}
assertThat( idExists ).isFalse();
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testFindAllActiveServicesToTerminate() throws Exception {
String response = createService();
Service responsesService = JsonUtils.toJsonObj(response, Service.class);
String id = responsesService.getId();
List<String> serviceList = serviceRepoService.findAllActiveServicesToTerminate();
boolean idExists = false;
for (String serviceId : serviceList) {
if (serviceId.equals(id)) {
idExists = true;
break;
}
}
assertThat( idExists ).isTrue();
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testFindAllActiveAndReservedServicesOfPartners() throws Exception {
String response = createService();
Service responsesService = JsonUtils.toJsonObj(response, Service.class);
String id = responsesService.getId();
List<String> serviceList = serviceRepoService.findAllActiveAndReservedServicesOfPartners();
boolean idExists = false;
for (String serviceId : serviceList) {
if (serviceId.equals(id)) {
idExists = true;
break;
}
}
assertThat( idExists ).isTrue();
}
@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
@Test
public void testFindDeploymentRequestID() throws Exception {
String response = createService();
Service responsesService = JsonUtils.toJsonObj(response, Service.class);
String id = responsesService.getId();
List<Service> serviceList = serviceRepoService.findDeploymentRequestID("1234567890");
boolean idExists = false;
for (Service s : serviceList) {
if ( s.getId().equals( id ) ) {
idExists= true;
}
}
assertThat( idExists ).isTrue();
}
// // org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection [HikariPool-1 - Connection is not available, request timed out after 30000ms.]
// @WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
// @Test
// public void testNfvCatalogNSResourceChanged() throws Exception {
// String response = createService();
// Service responsesService = JsonUtils.toJsonObj(response, Service.class);
// String id = responsesService.getId();
//
// DeploymentDescriptor dd = new DeploymentDescriptor();
// dd.setId(1234567890);
//
// serviceRepoService.nfvCatalogNSResourceChanged(dd);
//
// Service updatedService = serviceRepoService.findByUuid(id);
// Set<Note> noteList = updatedService.getNote();
//
// boolean expectedNoteExists = false;
// for (Note n : noteList) {
// if ( n.getText().equals("NS Resource LCM Changed") && n.getAuthor().equals("SIM638-API")) {
// expectedNoteExists= true;
// break;
// }
// }
// assertThat( expectedNoteExists ).isTrue();
// }
// // org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection [HikariPool-1 - Connection is not available, request timed out after 30000ms.]
// @WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
// @Test
// public void testResourceStateChangedEvent() throws Exception {
// String response = createService();
// Service responsesService = JsonUtils.toJsonObj(response, Service.class);
// String id = responsesService.getId();
// Set<ResourceRef> resourceRefSet = responsesService.getSupportingResource();
// List<ResourceRef> resourceRefList = new ArrayList<>(resourceRefSet);
//
// assertThat(resourceRefList.size()).isEqualTo(1);
// ResourceRef firstResourceRef = resourceRefList.get(0);
//
// Resource resource = resourceRepoService.findByUuid(firstResourceRef.getId());
//
// ResourceStateChangeNotification resourceCreateNotification = new ResourceStateChangeNotification();
// ResourceStateChangeEvent event = new ResourceStateChangeEvent();
// event.getEvent().setResource(resource);
// resourceCreateNotification.setEvent(event);
//
// serviceRepoService.resourceStateChangedEvent(resourceCreateNotification);
// Service updatedService = serviceRepoService.findByUuid(id);
//
// Set<Note> noteSet = updatedService.getNote();
// List<Note> noteList = new ArrayList<>(noteSet);
//
// boolean expectedNoteExists = false;
// for (Note n : noteList) {
// if ( n.getText().contains("State Changed with status:") && n.getAuthor().equals("SIM638-API")) {
// expectedNoteExists= true;
// break;
// }
// }
// assertThat( expectedNoteExists ).isTrue();
// }
private String createService() throws Exception {
int servicesCount = serviceRepoService.findAll().size();
File sspec = new File( "src/test/resources/testServiceSpec.json" );
InputStream in = new FileInputStream( sspec );
String sspectext = IOUtils.toString(in, "UTF-8");
ServiceSpecificationCreate sspeccr1 = JsonUtils.toJsonObj( sspectext, ServiceSpecificationCreate.class);
sspeccr1.setName("Spec1");
ServiceSpecification responsesSpec1 = createServiceSpec(sspectext, sspeccr1);
//service 2 is an RFS
ServiceSpecificationCreate sspeccr2 = JsonUtils.toJsonObj( sspectext, ServiceSpecificationCreate.class);
sspeccr2.setName("Spec2");
ResourceSpecificationRef resourceSpecificationItem = new ResourceSpecificationRef();
resourceSpecificationItem.setId("resourceid");
resourceSpecificationItem.setName("resourceName");
sspeccr2.addResourceSpecificationItem(resourceSpecificationItem);
ServiceSpecification responsesSpec2 = createServiceSpec(sspectext, sspeccr2);
// Add them as bundle
ServiceSpecificationCreate sspeccr3 = JsonUtils.toJsonObj( sspectext, ServiceSpecificationCreate.class);
sspeccr3.setName("BundleExampleSpec");
sspeccr3.isBundle(true);
sspeccr3.addServiceSpecRelationshipWith( responsesSpec1 );
sspeccr3.addServiceSpecRelationshipWith( responsesSpec2 );
ServiceSpecification responsesSpec3 = createServiceSpec(sspectext, sspeccr3);
ServiceCreate aService = new ServiceCreate();
aService.setName("aNew Service");
aService.setCategory("Test Category");
aService.setDescription("A Test Service");
aService.setStartDate( OffsetDateTime.now(ZoneOffset.UTC ).toString() );
aService.setEndDate( OffsetDateTime.now(ZoneOffset.UTC ).toString() );
aService.setState(ServiceStateType.ACTIVE);
Note noteItem = new Note();
noteItem.text("test note");
aService.addNoteItem(noteItem);
Characteristic serviceCharacteristicItem = new Characteristic();
serviceCharacteristicItem.setName( "ConfigStatus" );
serviceCharacteristicItem.setValue( new Any("NONE"));
aService.addServiceCharacteristicItem(serviceCharacteristicItem);
serviceCharacteristicItem = new Characteristic();
serviceCharacteristicItem.setName( "NSLCM" );
serviceCharacteristicItem.setValue( new Any("nslcm_test"));
aService.addServiceCharacteristicItem(serviceCharacteristicItem);
serviceCharacteristicItem = new Characteristic();
serviceCharacteristicItem.setName( "NSR" );
serviceCharacteristicItem.setValue( new Any("nsr_test"));
aService.addServiceCharacteristicItem(serviceCharacteristicItem);
serviceCharacteristicItem = new Characteristic();
serviceCharacteristicItem.setName( "externalPartnerServiceId" );
serviceCharacteristicItem.setValue( new Any("ext_test"));
aService.addServiceCharacteristicItem(serviceCharacteristicItem);
serviceCharacteristicItem = new Characteristic();
serviceCharacteristicItem.setName( "DeploymentRequestID" );
serviceCharacteristicItem.setValue( new Any("1234567890"));
aService.addServiceCharacteristicItem(serviceCharacteristicItem);
serviceCharacteristicItem = new Characteristic();
serviceCharacteristicItem.setName( "long_string" );
serviceCharacteristicItem.setValue( new Any("12345"));
aService.addServiceCharacteristicItem(serviceCharacteristicItem);
ServiceSpecificationRef aServiceSpecificationRef = new ServiceSpecificationRef();
aServiceSpecificationRef.setId(responsesSpec3.getId() );
aServiceSpecificationRef.setName(responsesSpec3.getName());
aService.setServiceSpecificationRef(aServiceSpecificationRef );
List<RelatedParty> rpl = new ArrayList<>();
RelatedParty rp = new RelatedParty();
rp.setName("Test Party");
rpl.add(rp);
aService.setRelatedParty(rpl);
ResourceCreate rc = new ResourceCreate();
rc.setName("Test Resource");
rc.setCategory("Test Resource Category");
LogicalResourceSpecification lrs = createLogicalResourceSpec();
ResourceSpecificationRef rsRef = new ResourceSpecificationRef();
rsRef.setId(lrs.getId());
rsRef.setName(lrs.getName());
rc.setResourceSpecification(rsRef);
Resource resourceResponse = resourceRepoService.addResource(rc);
List<ResourceRef> rrl = new ArrayList<>();
ResourceRef rRef = new ResourceRef();
rRef.setId(resourceResponse.getId());
rRef.setName(resourceResponse.getName());
rrl.add(rRef);
aService.setSupportingResource(rrl);
String response = mvc.perform(MockMvcRequestBuilders.post("/serviceInventory/v4/service")
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON)
.content( JsonUtils.toJson( aService ) ))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
Service responseService = JsonUtils.toJsonObj(response, Service.class);
assertThat( serviceRepoService.findAll().size() ).isEqualTo( servicesCount + 1 );
assertThat(responseService.getCategory()).isEqualTo("Test Category");
assertThat(responseService.getDescription()).isEqualTo("A Test Service");
return response;
}
private ServiceSpecification createServiceSpec(String sspectext, ServiceSpecificationCreate sspeccr1) throws Exception{
String response = mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/serviceSpecification")
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON)
.content( JsonUtils.toJson( sspeccr1 ) ))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class);
return responsesSpec;
}
private LogicalResourceSpecification createLogicalResourceSpec() throws Exception{
File sspec = new File( "src/test/resources/testResourceSpec.json" );
InputStream in = new FileInputStream( sspec );
String sspectext = IOUtils.toString(in, "UTF-8");
LogicalResourceSpecificationCreate sspeccr = JsonUtils.toJsonObj( sspectext, LogicalResourceSpecificationCreate.class);
AttachmentRefOrValue attachmentItem = new AttachmentRefOrValue();
attachmentItem.setId( "a-ref-id" );
attachmentItem.setDescription("an attachment");
attachmentItem.setUrl("a url");
attachmentItem.setName("aname");
sspeccr.addAttachmentItem(attachmentItem);
String response = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification")
.with( SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON)
.content( JsonUtils.toJson( sspeccr ) ))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("name", is("Test Resource Spec")))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
LogicalResourceSpecification responsesSpec = JsonUtils.toJsonObj(response, LogicalResourceSpecification.class);
return responsesSpec;
}
// private ServiceUpdate createServiceUpdateObject() throws Exception{
// ServiceUpdate serviceUpdate = new ServiceUpdate();
// serviceUpdate.setType("Updated type");
// serviceUpdate.setName("Updated name");
// serviceUpdate.setCategory("Updated category");
// serviceUpdate.setDescription("Updated description");
// serviceUpdate.setStartDate(OffsetDateTime.now(ZoneOffset.UTC ).toString());
// serviceUpdate.hasStarted(true);
// serviceUpdate.isServiceEnabled(true);
// serviceUpdate.isStateful(true);
// serviceUpdate.setServiceDate(OffsetDateTime.now(ZoneOffset.UTC ).toString());
// serviceUpdate.setServiceType("Updated Service Type");
// serviceUpdate.setStartMode("Updated Start Mode");
// serviceUpdate.setState(ServiceStateType.FEASIBILITYCHECKED);
// List<Place> placeList = new ArrayList<>();
// Place place = new Place();
// place.setName("Updated place");
// place.setType("Updated type");
// placeList.add(place);
// serviceUpdate.setPlace(placeList);
//
// List<RelatedParty> partyList = new ArrayList<>();
// RelatedParty party = new RelatedParty();
// party.setName("Updated party");
// party.setType("Updated type");
// partyList.add(party);
// serviceUpdate.setRelatedParty(partyList);
//
// ResourceCreate rc = new ResourceCreate();
// rc.setName("Updated Resource");
// rc.setCategory("Updated Resource Category");
// LogicalResourceSpecification lrs = createLogicalResourceSpec();
// ResourceSpecificationRef rsRef = new ResourceSpecificationRef();
// rsRef.setId(lrs.getId());
// rsRef.setName(lrs.getName());
// rc.setResourceSpecification(rsRef);
// Resource resourceResponse = resourceRepoService.addResource(rc);
// List<ResourceRef> rrl = new ArrayList<>();
// ResourceRef rRef = new ResourceRef();
// rRef.setId(resourceResponse.getId());
// rRef.setName(resourceResponse.getName());
// rrl.add(rRef);
//
// serviceUpdate.setSupportingResource(rrl);
//
// return serviceUpdate;
// }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment