Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ buildNumber.properties
# JDT-specific (Eclipse Java Development Tools)
.classpath

## VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
!default.code-workspace

.vscode
# Local History for Visual Studio Code
.history/
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"streetsidesoftware.code-spell-checker",
"davidanson.vscode-markdownlint",
"vscjava.vscode-java-pack"
]
}
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"java.format.comments.enabled": false,
"java.inlayHints.parameterNames.enabled": "none",
"files.encoding": "utf8",
"java.home": "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home",
"java.jdt.ls.vmargs": "-Dfile.encoding=UTF-8",
"javac-linter.javac": "javac -Dfile.encoding=UTF-8",
"java.configuration.updateBuildConfiguration": "interactive",
"cSpell.words": ["fiware", "makeour", "ngsi", "Ngsiv"]
}
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# MoC Java

MoC client for Java

[![Java CI with Maven](https://github.com/makeOurCity/moc-java/actions/workflows/test.yml/badge.svg)](https://github.com/makeOurCity/moc-java/actions/workflows/test.yml)


# Usage
## Usage

See [city.makeour.moc.examples](./src/main/java/city/makeour/moc/examples/)

# Development
## Development

## Testing
### Testing

Set `.vscode/settings` env
Set `xxxx.code-workspace` env and use it.

```json
{
Expand All @@ -28,6 +28,6 @@ Set `.vscode/settings` env
```

```console
$ mvn clean install
$ mvn test
```
mvn clean install
mvn test
```
10 changes: 9 additions & 1 deletion default.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"files.encoding": "utf8",
"java.home": "/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home",
"java.jdt.ls.vmargs": "-Dfile.encoding=UTF-8",
"javac-linter.javac": "javac -Dfile.encoding=UTF-8"
"java.configuration.updateBuildConfiguration": "interactive",
"java.test.config": {
"env": {
"TEST_COGNITO_USER_POOL_ID": "",
"TEST_COGNITO_CLIENT_ID": "",
"TEST_COGNITO_USERNAME": "",
"TEST_COGNITO_PASSWORD": ""
}
}
}
}
24 changes: 16 additions & 8 deletions src/main/java/city/makeour/moc/MocClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import city.makeour.moc.ngsiv2.Ngsiv2Client;
import city.makeour.ngsi.v2.api.EntitiesApi;
import city.makeour.ngsi.v2.invoker.ApiClient;

public class MocClient {
protected ApiClient apiClient;

protected EntitiesApi entitiesApi;
protected Ngsiv2Client client;

protected TokenFetcherInterface tokenFetcher;

Expand All @@ -20,15 +19,16 @@ public MocClient() {
}

public MocClient(String basePath) {
this.apiClient = new ApiClient();
this.apiClient.setBasePath(basePath);
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(basePath);

this.client = new Ngsiv2Client(apiClient);

this.entitiesApi = new EntitiesApi(this.apiClient);
this.refreshTokenStorage = new RefreshTokenStorage();
}

public EntitiesApi entities() {
return this.entitiesApi;
return this.client.getEntitiesApi();
}

public void setMocAuthInfo(String cognitoUserPoolId, String cognitoClientId) {
Expand Down Expand Up @@ -80,6 +80,14 @@ public void refreshToken() throws InvalidKeyException, NoSuchAlgorithmException
}

public void setToken(String token) {
this.apiClient.addDefaultHeader("Authorization", token);
this.client.getApiClient().addDefaultHeader("Authorization", token);
}

public void setFiwareService(String fiwareService) {
this.client.getApiClient().addDefaultHeader("Fiware-Service", fiwareService);
}

public void createEntity(String contentType, Object body) {
this.client.createEntity(contentType, body);
}
}
2 changes: 1 addition & 1 deletion src/main/java/city/makeour/moc/examples/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) {
entity.setType("TestEntity");
entity.setId("urn:ngsi-ld:TestEntity:" + uuid);
try {
mocClient.entities().createEntity("application/json", entity, "keyValues");
mocClient.createEntity("application/json", entity);
} catch (RestClientResponseException e) {
System.err.println("Error creating entity: " + e.getMessage());
}
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/city/makeour/moc/ngsiv2/Ngsiv2Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package city.makeour.moc.ngsiv2;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientResponseException;

import city.makeour.ngsi.v2.api.EntitiesApi;
import city.makeour.ngsi.v2.invoker.ApiClient;

public class Ngsiv2Client {
protected ApiClient apiClient;

protected EntitiesApi entitiesApi;

public Ngsiv2Client(ApiClient apiClient) {
this.apiClient = apiClient;

this.entitiesApi = new EntitiesApi(this.apiClient);
}

public ApiClient getApiClient() {
return apiClient;
}

public EntitiesApi getEntitiesApi() {
return entitiesApi;
}

public RestClient.ResponseSpec createEntity(String contentType, Object body) {
if (contentType == null) {
throw new RestClientResponseException(
"Missing the required parameter 'contentType' when calling createEntity",
HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), (HttpHeaders) null,
(byte[]) null, (Charset) null);
} else if (body == null) {
throw new RestClientResponseException("Missing the required parameter 'body' when calling createEntity",
HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), (HttpHeaders) null,
(byte[]) null, (Charset) null);
} else {
Map<String, Object> pathParams = new HashMap();
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap();
HttpHeaders headerParams = new HttpHeaders();
MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap();
MultiValueMap<String, Object> formParams = new LinkedMultiValueMap();
queryParams.putAll(
this.apiClient.parameterToMultiValueMap((ApiClient.CollectionFormat) null, "options", "keyValues"));
if (contentType != null) {
headerParams.add("Content-Type", this.apiClient.parameterToString(contentType));
}

String[] localVarAccepts = new String[0];
List<MediaType> localVarAccept = this.apiClient.selectHeaderAccept(localVarAccepts);
String[] localVarContentTypes = new String[] { "application/json" };
MediaType localVarContentType = this.apiClient.selectHeaderContentType(localVarContentTypes);
String[] localVarAuthNames = new String[0];
ParameterizedTypeReference<Void> localVarReturnType = new ParameterizedTypeReference<>() {
};
return this.apiClient.invokeAPI("/v2/entities", HttpMethod.POST, pathParams, queryParams, body,
headerParams, cookieParams, formParams, localVarAccept, localVarContentType, localVarAuthNames,
localVarReturnType);
}
}
}
6 changes: 3 additions & 3 deletions src/test/java/city/makeour/moc/MocClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class MocClientTest {
@DisplayName("デフォルトコンストラクタで正しいベースパスが設定されることを確認")
void defaultConstructorShouldSetCorrectBasePath() {
MocClient client = new MocClient();
assertEquals("https://orion.sandbox.makeour.city", client.apiClient.getBasePath());
assertEquals("https://orion.sandbox.makeour.city", client.client.getApiClient().getBasePath());
}

@Test
@DisplayName("カスタムベースパスが正しく設定されることを確認")
void constructorWithBasePathShouldSetCustomBasePath() {
String customBasePath = "https://custom.orion.example.com";
MocClient client = new MocClient(customBasePath);
assertEquals(customBasePath, client.apiClient.getBasePath());
assertEquals(customBasePath, client.client.getApiClient().getBasePath());
}

@Test
Expand All @@ -41,7 +41,7 @@ void entitiesMethodShouldReturnEntitiesApiInstance() {
EntitiesApi entitiesApi = client.entities();

assertNotNull(entitiesApi);
assertEquals(client.entitiesApi, entitiesApi);
assertEquals(client.client.getEntitiesApi(), entitiesApi);
}

@Test
Expand Down