-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix uTransport doc & test coverage & add UEntityBuilder
- Loading branch information
czfdcn
committed
Jan 26, 2024
1 parent
152e445
commit ae5d2fa
Showing
6 changed files
with
372 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/org/eclipse/uprotocol/uri/builder/UEntityBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2024 General Motors GTO LLC | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
* | ||
* SPDX-FileType: SOURCE | ||
* SPDX-FileCopyrightText: 2024 General Motors GTO LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.eclipse.uprotocol.uri.builder; | ||
|
||
import org.eclipse.uprotocol.UprotocolOptions; | ||
import org.eclipse.uprotocol.v1.UEntity; | ||
|
||
import com.google.protobuf.DescriptorProtos.ServiceOptions; | ||
import com.google.protobuf.Descriptors.ServiceDescriptor; | ||
|
||
/** | ||
* Builder interface for UEntity to help to build UEntity from common use cases | ||
*/ | ||
public interface UEntityBuilder { | ||
/** | ||
* Builds a UEntity for an protobuf generated code Service Descriptor. | ||
* @param descriptor The protobuf generated code Service Descriptor. | ||
* @return Returns a UEntity for an protobuf generated code Service Descriptor. | ||
*/ | ||
static UEntity fromProto(ServiceDescriptor descriptor) { | ||
if (descriptor == null) { | ||
return UEntity.getDefaultInstance(); | ||
} | ||
|
||
ServiceOptions options = descriptor.getOptions(); | ||
|
||
return UEntity.newBuilder() | ||
.setName(options.<String>getExtension(UprotocolOptions.name)) | ||
.setId(options.<Integer>getExtension(UprotocolOptions.id)) | ||
.setVersionMajor(options.<Integer>getExtension(UprotocolOptions.versionMajor)) | ||
.build(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/eclipse/uprotocol/uri/builder/UResourceBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/test/java/org/eclipse/uprotocol/transport/UTransportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* Copyright (c) 2024 General Motors GTO LLC | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
* | ||
* SPDX-FileType: SOURCE | ||
* SPDX-FileCopyrightText: 2024 General Motors GTO LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.eclipse.uprotocol.transport; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.eclipse.uprotocol.v1.UAttributes; | ||
import org.eclipse.uprotocol.v1.UCode; | ||
import org.eclipse.uprotocol.v1.UMessage; | ||
import org.eclipse.uprotocol.v1.UPayload; | ||
import org.eclipse.uprotocol.v1.UStatus; | ||
import org.eclipse.uprotocol.v1.UUri; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Test implementing and using uTransport API | ||
*/ | ||
public class UTransportTest { | ||
@Test | ||
@DisplayName("Test happy path send message parts") | ||
public void test_happy_send_message_parts() { | ||
UTransport transport = new HappyUTransport(); | ||
|
||
UStatus status = transport.send( | ||
UUri.getDefaultInstance(), | ||
UPayload.getDefaultInstance(), | ||
UAttributes.getDefaultInstance()); | ||
|
||
assertEquals(status.getCode(), UCode.OK); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test happy path send message") | ||
public void test_happy_send_message() { | ||
UTransport transport = new HappyUTransport(); | ||
UStatus status = transport.send(UMessage.getDefaultInstance()); | ||
assertEquals(status.getCode(), UCode.OK); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test happy path register listener") | ||
public void test_happy_register_listener() { | ||
UTransport transport = new HappyUTransport(); | ||
UStatus status = transport.registerListener(UUri.getDefaultInstance(), new MyListener()); | ||
assertEquals(status.getCode(), UCode.OK); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test happy path unregister listener") | ||
public void test_happy_register_unlistener() { | ||
UTransport transport = new HappyUTransport(); | ||
UStatus status = transport.unregisterListener(UUri.getDefaultInstance(), new MyListener()); | ||
assertEquals(status.getCode(), UCode.OK); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test sending null message") | ||
public void test_sending_null_message() { | ||
UTransport transport = new HappyUTransport(); | ||
UStatus status = transport.send(null); | ||
assertEquals(status.getCode(), UCode.INVALID_ARGUMENT); | ||
} | ||
|
||
|
||
@Test | ||
@DisplayName("Test unhappy path send message parts") | ||
public void test_unhappy_send_message_parts() { | ||
UTransport transport = new SadUTransport(); | ||
|
||
UStatus status = transport.send( | ||
UUri.getDefaultInstance(), | ||
UPayload.getDefaultInstance(), | ||
UAttributes.getDefaultInstance()); | ||
|
||
assertEquals(status.getCode(), UCode.INTERNAL); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test unhappy path send message") | ||
public void test_unhappy_send_message() { | ||
UTransport transport = new SadUTransport(); | ||
UStatus status = transport.send(UMessage.getDefaultInstance()); | ||
assertEquals(status.getCode(), UCode.INTERNAL); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test unhappy path register listener") | ||
public void test_unhappy_register_listener() { | ||
UTransport transport = new SadUTransport(); | ||
UStatus status = transport.registerListener(UUri.getDefaultInstance(), new MyListener()); | ||
assertEquals(status.getCode(), UCode.INTERNAL); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test unhappy path unregister listener") | ||
public void test_unhappy_register_unlistener() { | ||
UTransport transport = new SadUTransport(); | ||
UStatus status = transport.unregisterListener(UUri.getDefaultInstance(), new MyListener()); | ||
assertEquals(status.getCode(), UCode.INTERNAL); | ||
} | ||
|
||
final class MyListener implements UListener { | ||
@Override | ||
public void onReceive(UUri topic, UPayload payload, UAttributes attributes) {} | ||
} | ||
|
||
private class HappyUTransport implements UTransport { | ||
@Override | ||
public UStatus send(UUri source, UPayload payload, UAttributes attributes) { | ||
return UStatus.newBuilder().setCode(UCode.OK).build(); | ||
} | ||
|
||
|
||
@Override | ||
public UStatus registerListener(UUri topic, UListener listener) { | ||
listener.onReceive(UMessage.getDefaultInstance()); | ||
return UStatus.newBuilder().setCode(UCode.OK).build(); | ||
} | ||
|
||
@Override | ||
public UStatus unregisterListener(UUri topic, UListener listener) { | ||
return UStatus.newBuilder().setCode(UCode.OK).build(); | ||
} | ||
} | ||
|
||
private class SadUTransport implements UTransport { | ||
@Override | ||
public UStatus send(UUri source, UPayload payload, UAttributes attributes) { | ||
return UStatus.newBuilder().setCode(UCode.INTERNAL).build(); | ||
} | ||
|
||
@Override | ||
public UStatus registerListener(UUri topic, UListener listener) { | ||
listener.onReceive(null); | ||
return UStatus.newBuilder().setCode(UCode.INTERNAL).build(); | ||
} | ||
|
||
@Override | ||
public UStatus unregisterListener(UUri topic, UListener listener) { | ||
return UStatus.newBuilder().setCode(UCode.INTERNAL).build(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.