Add tests to CAPIF Java version
Change-Id: Ie341022e34823f2eff130a590499f86147f2cdf2
diff --git a/capif/.gitignore b/capif/.gitignore
index 3354d0c..c6af413 100644
--- a/capif/.gitignore
+++ b/capif/.gitignore
@@ -5,3 +5,4 @@
internal/enumfixer/enumfixer
internal/commoncollector/commoncollector
internal/specificationfixer/specificationfixer
+.idea
diff --git a/capif/README.md b/capif/README.md
index 1be383d..24f1d3d 100644
--- a/capif/README.md
+++ b/capif/README.md
@@ -26,7 +26,7 @@
## Generation
-The CAPIF interfaces are generated in to Go code into the `internal/readonly` folder. To generate the code run the `generate.sh` script. This script will download the specifications for release 16, unzip them, remove some not needed definitions, and generate the code. NOTE! The specifications should not be checked in, only the generated code.
+The CAPIF interfaces are generated in to Go code into the `internal/readonly` folder. To generate the code run the `generate.sh` script. This script will download the specifications for release 17, unzip them, remove some not needed definitions, and generate the code. NOTE! The specifications should not be checked in, only the generated code.
## Run
diff --git a/capif/internal/providermanagement/providermanagement.go b/capif/internal/providermanagement/providermanagement.go
index d08dbde..358f8ab 100644
--- a/capif/internal/providermanagement/providermanagement.go
+++ b/capif/internal/providermanagement/providermanagement.go
@@ -106,8 +106,8 @@
return ctx.NoContent(http.StatusNoContent)
}
-func (cf *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
- registeredProvider, ok := cf.onboardedProviders[registrationId]
+func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
+ registeredProvider, ok := pm.onboardedProviders[registrationId]
if !ok {
return sendCoreError(ctx, http.StatusBadRequest, "Provider must be onboarded before updating it")
@@ -121,11 +121,11 @@
for _, function := range *updatedProvider.ApiProvFuncs {
if function.ApiProvFuncId == nil {
- function.ApiProvFuncId = cf.getFuncId(function.ApiProvFuncRole, function.ApiProvFuncInfo)
+ function.ApiProvFuncId = pm.getFuncId(function.ApiProvFuncRole, function.ApiProvFuncInfo)
registeredFuncs := *registeredProvider.ApiProvFuncs
newFuncs := append(registeredFuncs, function)
registeredProvider.ApiProvFuncs = &newFuncs
- cf.onboardedProviders[*registeredProvider.ApiProvDomId] = registeredProvider
+ pm.onboardedProviders[*registeredProvider.ApiProvDomId] = registeredProvider
}
}
@@ -138,7 +138,7 @@
return nil
}
-func (cf *ProviderManager) ModifyIndApiProviderEnrolment(ctx echo.Context, registrationId string) error {
+func (pm *ProviderManager) ModifyIndApiProviderEnrolment(ctx echo.Context, registrationId string) error {
return ctx.NoContent(http.StatusNotImplemented)
}
diff --git a/capif/java/capifcore/pom.xml b/capif/java/capifcore/pom.xml
index b08daad..7ed5137 100644
--- a/capif/java/capifcore/pom.xml
+++ b/capif/java/capifcore/pom.xml
@@ -1,4 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
+<!--
+* ========================LICENSE_START=================================
+* O-RAN-SC
+* %%
+* Copyright (C) 2022 Nordix Foundation
+* %%
+* 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===================================
+-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@@ -81,18 +100,15 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
- <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
- <!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.6</version>
</dependency>
- <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
@@ -102,19 +118,30 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
- <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-engine -->
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ </dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
- </dependencies>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-launcher</artifactId>
+ <scope>test</scope>
+ </dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ </plugin>
</plugins>
</build>
</project>
diff --git a/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/OnboardedInvokersApiImpl.java b/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/InvokerManager.java
similarity index 79%
rename from capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/OnboardedInvokersApiImpl.java
rename to capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/InvokerManager.java
index 82d42a4..20b926e 100644
--- a/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/OnboardedInvokersApiImpl.java
+++ b/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/InvokerManager.java
@@ -25,14 +25,17 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
+
+import java.net.URI;
@RestController
@RequestMapping("api-invoker-management/v1")
-public class OnboardedInvokersApiImpl implements OnboardedInvokersApi {
+public class InvokerManager implements OnboardedInvokersApi {
private final ApiRegister apiRegister;
private int nextId;
- public OnboardedInvokersApiImpl(ApiRegister apiRegister) {
+ public InvokerManager(ApiRegister apiRegister) {
this.apiRegister = apiRegister;
}
@@ -40,7 +43,12 @@
public ResponseEntity<APIInvokerEnrolmentDetails> onboardedInvokersPost(APIInvokerEnrolmentDetails apIInvokerEnrolmentDetails) {
apIInvokerEnrolmentDetails.apiInvokerId(getApiId(apIInvokerEnrolmentDetails.getApiInvokerInformation()));
- return ResponseEntity.ok(apIInvokerEnrolmentDetails);
+ URI uri = ServletUriComponentsBuilder
+ .fromCurrentRequest()
+ .path("/{id}")
+ .buildAndExpand(apIInvokerEnrolmentDetails.getApiInvokerId())
+ .toUri();
+ return ResponseEntity.created(uri).body(apIInvokerEnrolmentDetails);
}
private String getApiId(String invokerInfo) {
diff --git a/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/RegistrationsApiImpl.java b/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/ProviderManager.java
similarity index 78%
rename from capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/RegistrationsApiImpl.java
rename to capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/ProviderManager.java
index 6d9e845..b364105 100644
--- a/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/RegistrationsApiImpl.java
+++ b/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/ProviderManager.java
@@ -25,15 +25,24 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
+
+import java.net.URI;
@RestController
@RequestMapping("/api-provider-management/v1")
-public class RegistrationsApiImpl implements RegistrationsApi {
+public class ProviderManager implements RegistrationsApi {
@Override
public ResponseEntity<APIProviderEnrolmentDetails> registrationsPost(APIProviderEnrolmentDetails apIProviderEnrolmentDetails) {
apIProviderEnrolmentDetails.apiProvDomId(getDomainId(apIProviderEnrolmentDetails.getApiProvDomInfo()));
- return ResponseEntity.ok(apIProviderEnrolmentDetails);
+ URI uri = ServletUriComponentsBuilder
+ .fromCurrentRequest()
+ .path("/{id}")
+ .buildAndExpand(apIProviderEnrolmentDetails.getApiProvDomId())
+ .toUri();
+
+ return ResponseEntity.created(uri).body(apIProviderEnrolmentDetails);
}
diff --git a/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/PublishService.java b/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/PublishService.java
index 1bee71c..6a9c032 100644
--- a/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/PublishService.java
+++ b/capif/java/capifcore/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/PublishService.java
@@ -20,42 +20,93 @@
package org.oransc.nonrtric.plt.capifcore.controller;
-import java.util.HashMap;
-import java.util.Map;
-
+import org.oransc.nonrtric.plt.capifapi.model.AefProfile;
import org.oransc.nonrtric.plt.capifapi.model.ServiceAPIDescription;
import org.oransc.nonrtric.plt.capifapi.publishservice.ApfIdApi;
+import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
@RestController
@RequestMapping("/published-apis/v1")
public class PublishService implements ApfIdApi, ApiRegister {
- private Map<String, ServiceAPIDescription> publishedServices = new HashMap<>();
+ private final Map<String, ServiceAPIDescription> publishedServices = new HashMap<>();
@Override
public ResponseEntity<ServiceAPIDescription> apfIdServiceApisPost(String apfId, ServiceAPIDescription serviceAPIDescription) {
- serviceAPIDescription.apiId("api_id_"+serviceAPIDescription.getApiName());
- publishedServices.put(serviceAPIDescription.getApiId(), serviceAPIDescription);
- return ResponseEntity.ok(serviceAPIDescription);
+ serviceAPIDescription.apiId("api_id_" + serviceAPIDescription.getApiName());
+ synchronized (publishedServices) {
+ publishedServices.put(serviceAPIDescription.getApiId(), serviceAPIDescription);
+ }
+ URI uri = ServletUriComponentsBuilder
+ .fromCurrentRequest()
+ .path("/{id}")
+ .buildAndExpand(serviceAPIDescription.getApiId())
+ .toUri();
+ return ResponseEntity.created(uri).body(serviceAPIDescription);
+ }
+
+ @Override
+ public ResponseEntity<ServiceAPIDescription> apfIdServiceApisServiceApiIdGet(String serviceApiId, String apfId) {
+ synchronized (publishedServices) {
+ if (publishedServices.containsKey(serviceApiId)) {
+ return ResponseEntity.ok(publishedServices.get(serviceApiId));
+ }
+ return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
+ }
}
@Override
public boolean areAPIsRegistered(ServiceAPIDescription[] serviceDescriptions) {
- // TODO Auto-generated method stub
return false;
}
@Override
public ServiceAPIDescription[] getAPIs() {
- // TODO Auto-generated method stub
- return null;
+ synchronized (publishedServices) {
+ ServiceAPIDescription[] serviceAPIDescriptions = publishedServices.values().toArray(new ServiceAPIDescription[publishedServices.size()]);
+ return serviceAPIDescriptions;
+ }
}
@Override
public boolean isAPIRegistered(String aefId, String apiName) {
- // TODO Auto-generated method stub
- return false;
+ synchronized (publishedServices) {
+ boolean registered = false;
+ Iterator<ServiceAPIDescription> servicesIter = publishedServices.values().iterator();
+ while (servicesIter.hasNext()) {
+ ServiceAPIDescription api = servicesIter.next();
+ if (apiName.equals(api.getApiName())) {
+ if (isProfileRegistered(aefId, api))
+ registered = true;
+ break;
+ }
+ }
+ return registered;
+ }
+ }
+
+ private boolean isProfileRegistered(String aefId, ServiceAPIDescription api) {
+ synchronized (publishedServices) {
+ boolean registered = false;
+ Iterator<AefProfile> profilesIter = api.getAefProfiles().iterator();
+ while (profilesIter.hasNext()) {
+ AefProfile aefProfile = profilesIter.next();
+ if (aefId.equals(aefProfile.getAefId())) {
+ registered = true;
+ break;
+ }
+ }
+ return registered;
+ }
}
}
diff --git a/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/CapifCoreApplicationTests.java b/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/CapifCoreApplicationTests.java
index 2a5f9a7..16dfd23 100644
--- a/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/CapifCoreApplicationTests.java
+++ b/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/CapifCoreApplicationTests.java
@@ -1,13 +1,81 @@
+// -
+// ========================LICENSE_START=================================
+// O-RAN-SC
+// %%
+// Copyright (C) 2022: Nordix Foundation
+// %%
+// 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.oransc.nonrtric.plt.capifcore;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.oransc.nonrtric.plt.capifapi.model.APIInvokerEnrolmentDetails;
+import org.oransc.nonrtric.plt.capifapi.model.APIProviderEnrolmentDetails;
+import org.oransc.nonrtric.plt.capifapi.model.ServiceAPIDescription;
+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.boot.test.web.server.LocalServerPort;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+
+import java.lang.reflect.Type;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
+@AutoConfigureMockMvc
class CapifCoreApplicationTests {
+ @Autowired
+ private MockMvc mockMvc;
- @Test
- void contextLoads() {
- }
+ @Autowired
+ private ObjectMapper objectMapper;
+
+ @Test
+ void servicesRegistered() throws Exception {
+ APIProviderEnrolmentDetails newProvider = new APIProviderEnrolmentDetails();
+ newProvider.apiProvDomInfo("domainInfo");
+ mockMvc.perform(post("/api-provider-management/v1/registrations")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(newProvider))
+ )
+ .andExpect(status().isCreated());
+
+ ServiceAPIDescription newServiceApi = new ServiceAPIDescription();
+ newServiceApi.apiName("apiName");
+
+ mockMvc.perform(post("/published-apis/v1/apfId/service-apis")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(newServiceApi))
+ )
+ .andExpect(status().isCreated());
+
+ APIInvokerEnrolmentDetails newInvoker = new APIInvokerEnrolmentDetails();
+ newInvoker.apiInvokerInformation("invokerInfo");
+ mockMvc.perform(post("/api-invoker-management/v1/onboardedInvokers")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(newInvoker))
+ )
+ .andExpect(status().isCreated());
+ }
}
diff --git a/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/PublishServiceTest.java b/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/PublishServiceTest.java
index aad2768..5127cac 100644
--- a/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/PublishServiceTest.java
+++ b/capif/java/capifcore/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/PublishServiceTest.java
@@ -20,43 +20,66 @@
package org.oransc.nonrtric.plt.capifcore.controller;
-import static org.assertj.core.api.Assertions.assertThat;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.oransc.nonrtric.plt.capifapi.model.AefProfile;
import org.oransc.nonrtric.plt.capifapi.model.ServiceAPIDescription;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+import java.net.URI;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@ExtendWith(SpringExtension.class)
class PublishServiceTest {
- // ApiRegister apiRegisterMock = mock(ApiRegister.class);
+ // ApiRegister apiRegisterMock = mock(ApiRegister.class);
@Test
- void testPostNewInvokerShouldGetInvokerId() {
+ void testPostNewServiceShouldGetApfIdAndResponseShouldIncludeLocationHeader() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
+
PublishService apiUnderTest = new PublishService();
- ServiceAPIDescription newInvoker = new ServiceAPIDescription();
- newInvoker.apiName("apiName");
- ResponseEntity<ServiceAPIDescription> response = apiUnderTest.apfIdServiceApisPost("apfId", newInvoker);
- assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
- assertThat(Objects.requireNonNull(response.getBody()).getApiId()).isEqualTo("api_id_apiName");
+ ServiceAPIDescription newServiceApi = new ServiceAPIDescription();
+ newServiceApi.apiName("apiName");
+ ResponseEntity<ServiceAPIDescription> response = apiUnderTest.apfIdServiceApisPost("apfId", newServiceApi);
+ assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
+ assertThat(response.getBody().getApiId()).isEqualTo("api_id_apiName");
+ assertThat(response.getHeaders().getLocation()).isEqualTo(new URI("http://localhost/api_id_apiName"));
}
-
+
@Test
- void testPostNewInvokerShouldBeStored() {
+ void testPostNewServiceShouldBeStored() {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
+
PublishService apiUnderTest = new PublishService();
- ServiceAPIDescription newInvoker = new ServiceAPIDescription();
+ ServiceAPIDescription newServiceAPI = new ServiceAPIDescription();
String apiName = "apiName";
- newInvoker.apiName(apiName);
+ newServiceAPI.apiName(apiName);
AefProfile aefProfile = new AefProfile();
String aefId = "aefId";
aefProfile.aefId(aefId);
-// aefProfile.
- newInvoker.addAefProfilesItem(aefProfile);
- ResponseEntity<ServiceAPIDescription> response = apiUnderTest.apfIdServiceApisPost("apfId", newInvoker);
- assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
+ newServiceAPI.addAefProfilesItem(aefProfile);
+ ResponseEntity<ServiceAPIDescription> response = apiUnderTest.apfIdServiceApisPost("apfId", newServiceAPI);
+ assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(apiUnderTest.isAPIRegistered(aefId, apiName)).isTrue();
- }
+ assertThat(apiUnderTest.getAPIs()).hasSize(1).contains(newServiceAPI);
+ response = apiUnderTest.apfIdServiceApisServiceApiIdGet("api_id_apiName", "apfId");
+ assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
+ assertThat(response.getBody()).isEqualTo(newServiceAPI);
+ }
+
+ @Test
+ void testGetPublishedServicesWithNotPublishedShouldReturnNotFound() {
+ PublishService apiUnderTest = new PublishService();
+
+ ResponseEntity<ServiceAPIDescription> response = apiUnderTest.apfIdServiceApisServiceApiIdGet("api_id_apiName", "apfId");
+
+ assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
+ }
}
\ No newline at end of file