Skip to content

Commit f34e24d

Browse files
DeviceInfracopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 896413542
1 parent d74b0b3 commit f34e24d

11 files changed

Lines changed: 375 additions & 72 deletions

File tree

src/java/com/google/devtools/mobileharness/fe/v6/service/host/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ java_library(
3838
"//src/devtools/mobileharness/fe/v6/service/proto/host:host_resources_java_proto",
3939
"//src/devtools/mobileharness/fe/v6/service/proto/host:host_service_java_proto",
4040
"//src/java/com/google/devtools/mobileharness/fe/v6/service/host/handlers",
41+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/util",
4142
"@maven//:com_google_guava_guava",
4243
"@maven//:javax_inject_jsr330_api",
4344
],

src/java/com/google/devtools/mobileharness/fe/v6/service/host/HostServiceLogicImpl.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.google.devtools.mobileharness.fe.v6.service.host;
1818

19+
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
1920
import static com.google.common.util.concurrent.Futures.immediateFuture;
2021

2122
import com.google.common.util.concurrent.ListenableFuture;
2223
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.GetHostDeviceSummariesHandler;
24+
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.GetHostHeaderInfoHandler;
2325
import com.google.devtools.mobileharness.fe.v6.service.host.handlers.GetHostOverviewHandler;
2426
import com.google.devtools.mobileharness.fe.v6.service.proto.host.CheckRemoteControlEligibilityRequest;
2527
import com.google.devtools.mobileharness.fe.v6.service.proto.host.CheckRemoteControlEligibilityResponse;
@@ -37,7 +39,6 @@
3739
import com.google.devtools.mobileharness.fe.v6.service.proto.host.GetPopularFlagsResponse;
3840
import com.google.devtools.mobileharness.fe.v6.service.proto.host.GetReleaseConfigsRequest;
3941
import com.google.devtools.mobileharness.fe.v6.service.proto.host.GetReleaseConfigsResponse;
40-
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostActions;
4142
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostHeaderInfo;
4243
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostOverviewPageData;
4344
import com.google.devtools.mobileharness.fe.v6.service.proto.host.ReleaseLabServerRequest;
@@ -52,6 +53,8 @@
5253
import com.google.devtools.mobileharness.fe.v6.service.proto.host.StopLabServerResponse;
5354
import com.google.devtools.mobileharness.fe.v6.service.proto.host.UpdatePassThroughFlagsRequest;
5455
import com.google.devtools.mobileharness.fe.v6.service.proto.host.UpdatePassThroughFlagsResponse;
56+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseFactory;
57+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
5558
import javax.inject.Inject;
5659
import javax.inject.Singleton;
5760

@@ -61,38 +64,53 @@ public final class HostServiceLogicImpl implements HostServiceLogic {
6164

6265
private final GetHostOverviewHandler getHostOverviewHandler;
6366
private final GetHostDeviceSummariesHandler getHostDeviceSummariesHandler;
67+
private final GetHostHeaderInfoHandler getHostHeaderInfoHandler;
68+
private final UniverseFactory universeFactory;
6469

6570
@Inject
6671
HostServiceLogicImpl(
6772
GetHostOverviewHandler getHostOverviewHandler,
68-
GetHostDeviceSummariesHandler getHostDeviceSummariesHandler) {
73+
GetHostDeviceSummariesHandler getHostDeviceSummariesHandler,
74+
GetHostHeaderInfoHandler getHostHeaderInfoHandler,
75+
UniverseFactory universeFactory) {
6976
this.getHostOverviewHandler = getHostOverviewHandler;
7077
this.getHostDeviceSummariesHandler = getHostDeviceSummariesHandler;
78+
this.getHostHeaderInfoHandler = getHostHeaderInfoHandler;
79+
this.universeFactory = universeFactory;
7180
}
7281

7382
@Override
7483
public ListenableFuture<HostHeaderInfo> getHostHeaderInfo(GetHostHeaderInfoRequest request) {
75-
// TODO: - dafeng - Use the universe parameter.
76-
@SuppressWarnings("unused")
77-
String universe = request.getUniverse();
78-
79-
// TODO: Implement this method.
80-
return immediateFuture(
81-
HostHeaderInfo.newBuilder()
82-
.setHostName(request.getHostName())
83-
.setActions(HostActions.getDefaultInstance())
84-
.build());
84+
UniverseScope universe;
85+
try {
86+
universe = universeFactory.create(request.getUniverse());
87+
} catch (IllegalArgumentException e) {
88+
return immediateFailedFuture(e);
89+
}
90+
return getHostHeaderInfoHandler.getHostHeaderInfo(request, universe);
8591
}
8692

8793
@Override
8894
public ListenableFuture<HostOverviewPageData> getHostOverview(GetHostOverviewRequest request) {
89-
return getHostOverviewHandler.getHostOverview(request);
95+
UniverseScope universe;
96+
try {
97+
universe = universeFactory.create(request.getUniverse());
98+
} catch (IllegalArgumentException e) {
99+
return immediateFailedFuture(e);
100+
}
101+
return getHostOverviewHandler.getHostOverview(request, universe);
90102
}
91103

92104
@Override
93105
public ListenableFuture<GetHostDeviceSummariesResponse> getHostDeviceSummaries(
94106
GetHostDeviceSummariesRequest request) {
95-
return getHostDeviceSummariesHandler.getHostDeviceSummaries(request);
107+
UniverseScope universe;
108+
try {
109+
universe = universeFactory.create(request.getUniverse());
110+
} catch (IllegalArgumentException e) {
111+
return immediateFailedFuture(e);
112+
}
113+
return getHostDeviceSummariesHandler.getHostDeviceSummaries(request, universe);
96114
}
97115

98116
@Override

src/java/com/google/devtools/mobileharness/fe/v6/service/host/handlers/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ java_library(
4141
"//src/java/com/google/devtools/mobileharness/fe/v6/service/shared",
4242
"//src/java/com/google/devtools/mobileharness/fe/v6/service/shared/providers:lab_info_provider",
4343
"//src/java/com/google/devtools/mobileharness/fe/v6/service/shared/remotecontrol",
44+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/util",
4445
"//src/java/com/google/devtools/mobileharness/shared/util/logging:google_logger",
4546
"@maven//:com_google_guava_guava",
4647
"@maven//:javax_inject_jsr330_api",

src/java/com/google/devtools/mobileharness/fe/v6/service/host/handlers/GetHostDeviceSummariesHandler.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.google.devtools.mobileharness.fe.v6.service.shared.remotecontrol.RemoteControlEligibilityChecker;
4646
import com.google.devtools.mobileharness.fe.v6.service.shared.remotecontrol.RemoteControlEligibilityContext;
4747
import com.google.devtools.mobileharness.fe.v6.service.shared.remotecontrol.RemoteControlEligibilityResult;
48+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
4849
import com.google.devtools.mobileharness.shared.labinfo.proto.LabInfoServiceProto.GetLabInfoRequest;
4950
import com.google.devtools.mobileharness.shared.labinfo.proto.LabInfoServiceProto.GetLabInfoResponse;
5051
import javax.inject.Inject;
@@ -81,13 +82,11 @@ public final class GetHostDeviceSummariesHandler {
8182

8283
/** Gets host device summaries. */
8384
public ListenableFuture<GetHostDeviceSummariesResponse> getHostDeviceSummaries(
84-
GetHostDeviceSummariesRequest request) {
85-
// TODO: - dafeng - Use the universe parameter.
86-
@SuppressWarnings("unused")
87-
String universe = request.getUniverse();
85+
GetHostDeviceSummariesRequest request, UniverseScope universe) {
86+
8887
GetLabInfoRequest getLabInfoRequest = createGetLabInfoRequest(request.getHostName());
8988
return Futures.transform(
90-
labInfoProvider.getLabInfoAsync(getLabInfoRequest, ""),
89+
labInfoProvider.getLabInfoAsync(getLabInfoRequest, universe),
9190
this::processLabInfoResponse,
9291
executor);
9392
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host.handlers;
18+
19+
import static com.google.common.util.concurrent.Futures.immediateFuture;
20+
21+
import com.google.common.util.concurrent.ListenableFuture;
22+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.GetHostHeaderInfoRequest;
23+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostActions;
24+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostHeaderInfo;
25+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
26+
import javax.inject.Inject;
27+
import javax.inject.Singleton;
28+
29+
/** Handler for getting host header info. */
30+
@Singleton
31+
public class GetHostHeaderInfoHandler {
32+
33+
@Inject
34+
GetHostHeaderInfoHandler() {}
35+
36+
public ListenableFuture<HostHeaderInfo> getHostHeaderInfo(
37+
GetHostHeaderInfoRequest request, UniverseScope universe) {
38+
if (universe instanceof UniverseScope.RoutedUniverse) {
39+
// Scenario 2: routed universe. Configuration is disabled.
40+
return immediateFuture(
41+
HostHeaderInfo.newBuilder()
42+
.setHostName(request.getHostName())
43+
.setActions(HostActions.getDefaultInstance()) // Empty actions
44+
.build());
45+
}
46+
47+
// Scenario 1 & 3: self universe.
48+
// TODO: Implement this method fully.
49+
return immediateFuture(
50+
HostHeaderInfo.newBuilder()
51+
.setHostName(request.getHostName())
52+
.setActions(HostActions.getDefaultInstance())
53+
.build());
54+
}
55+
}

src/java/com/google/devtools/mobileharness/fe/v6/service/host/handlers/GetHostOverviewHandler.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostOverviewPageData;
4949
import com.google.devtools.mobileharness.fe.v6.service.proto.host.LabServerInfo;
5050
import com.google.devtools.mobileharness.fe.v6.service.shared.providers.LabInfoProvider;
51+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
5152
import com.google.devtools.mobileharness.shared.labinfo.proto.LabInfoServiceProto.GetLabInfoRequest;
5253
import com.google.devtools.mobileharness.shared.labinfo.proto.LabInfoServiceProto.GetLabInfoResponse;
5354
import java.util.List;
@@ -74,28 +75,26 @@ public final class GetHostOverviewHandler {
7475
this.executor = executor;
7576
}
7677

77-
public ListenableFuture<HostOverviewPageData> getHostOverview(GetHostOverviewRequest request) {
78-
// TODO: - dafeng - Use the universe parameter.
79-
@SuppressWarnings("unused")
80-
String universe = request.getUniverse();
78+
public ListenableFuture<HostOverviewPageData> getHostOverview(
79+
GetHostOverviewRequest request, UniverseScope universe) {
8180
logger.atInfo().log("Getting host overview for %s", request.getHostName());
8281
String hostName = request.getHostName();
8382

8483
ListenableFuture<GetLabInfoResponse> labInfoFuture =
85-
labInfoProvider.getLabInfoAsync(createGetLabInfoRequest(hostName), "");
84+
labInfoProvider.getLabInfoAsync(createGetLabInfoRequest(hostName), universe);
8685

8786
ListenableFuture<Optional<HostReleaseInfo>> hostReleaseInfoFuture =
88-
hostAuxiliaryInfoProvider.getHostReleaseInfo(hostName);
87+
hostAuxiliaryInfoProvider.getHostReleaseInfo(hostName, universe);
8988

9089
ListenableFuture<Optional<String>> passThroughFlagsFuture =
91-
hostAuxiliaryInfoProvider.getPassThroughFlags(hostName);
90+
hostAuxiliaryInfoProvider.getPassThroughFlags(hostName, universe);
9291

9392
ListenableFuture<List<DiagnosticLink>> logLinksFuture =
9493
Futures.transformAsync(
9594
hostReleaseInfoFuture,
9695
hostReleaseInfoOpt ->
9796
hostAuxiliaryInfoProvider.getDiagnosticLinks(
98-
hostName, hostReleaseInfoOpt.flatMap(HostReleaseInfo::labType)),
97+
hostName, hostReleaseInfoOpt.flatMap(HostReleaseInfo::labType), universe),
9998
executor);
10099

101100
return Futures.whenAllSucceed(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
load("@rules_java//java:java_library.bzl", "java_library")
17+
load("//src/javatests/com/google/devtools/mobileharness/builddefs:junit_test_suites.bzl", "junit_test_suites")
18+
19+
package(
20+
default_applicable_licenses = ["//:license"],
21+
)
22+
23+
java_library(
24+
name = "host",
25+
testonly = 1,
26+
srcs = glob(["*.java"]),
27+
deps = [
28+
"//src/devtools/mobileharness/fe/v6/service/proto/host:host_resources_java_proto",
29+
"//src/devtools/mobileharness/fe/v6/service/proto/host:host_service_java_proto",
30+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/host:host_service_logic_impl",
31+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/host/provider",
32+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/shared",
33+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/shared/providers:lab_info_provider",
34+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/shared/remotecontrol",
35+
"//src/java/com/google/devtools/mobileharness/fe/v6/service/util",
36+
"//src/javatests/com/google/devtools/mobileharness/builddefs:truth",
37+
"@maven//:com_google_guava_guava",
38+
"@maven//:com_google_inject_extensions_guice_testlib",
39+
"@maven//:com_google_inject_guice",
40+
"@maven//:junit_junit",
41+
"@maven//:org_mockito_mockito_core",
42+
],
43+
)
44+
45+
junit_test_suites(
46+
name = "gen_tests",
47+
sizes = [
48+
"small",
49+
],
50+
deps = [":host"],
51+
)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.devtools.mobileharness.fe.v6.service.host;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
21+
import static org.junit.Assert.assertThrows;
22+
import static org.mockito.ArgumentMatchers.anyString;
23+
import static org.mockito.Mockito.when;
24+
25+
import com.google.common.util.concurrent.ListeningExecutorService;
26+
import com.google.devtools.mobileharness.fe.v6.service.host.provider.HostAuxiliaryInfoProvider;
27+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.GetHostHeaderInfoRequest;
28+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostActions;
29+
import com.google.devtools.mobileharness.fe.v6.service.proto.host.HostHeaderInfo;
30+
import com.google.devtools.mobileharness.fe.v6.service.shared.SubDeviceInfoListFactory;
31+
import com.google.devtools.mobileharness.fe.v6.service.shared.providers.LabInfoProvider;
32+
import com.google.devtools.mobileharness.fe.v6.service.shared.remotecontrol.RemoteControlEligibilityChecker;
33+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseFactory;
34+
import com.google.devtools.mobileharness.fe.v6.service.util.UniverseScope;
35+
import com.google.inject.Guice;
36+
import com.google.inject.testing.fieldbinder.Bind;
37+
import com.google.inject.testing.fieldbinder.BoundFieldModule;
38+
import java.time.InstantSource;
39+
import java.util.concurrent.ExecutionException;
40+
import org.junit.Before;
41+
import org.junit.Rule;
42+
import org.junit.Test;
43+
import org.junit.runner.RunWith;
44+
import org.junit.runners.JUnit4;
45+
import org.mockito.Mock;
46+
import org.mockito.junit.MockitoJUnit;
47+
import org.mockito.junit.MockitoRule;
48+
49+
@RunWith(JUnit4.class)
50+
public final class HostServiceLogicImplTest {
51+
52+
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
53+
54+
@Bind @Mock private LabInfoProvider labInfoProvider;
55+
@Bind @Mock private HostAuxiliaryInfoProvider hostAuxiliaryInfoProvider;
56+
@Bind @Mock private SubDeviceInfoListFactory subDeviceInfoListFactory;
57+
@Bind @Mock private RemoteControlEligibilityChecker remoteControlEligibilityChecker;
58+
@Bind private final ListeningExecutorService executor = newDirectExecutorService();
59+
@Bind @Mock private UniverseFactory universeFactory;
60+
@Bind @Mock private InstantSource instantSource;
61+
62+
private HostServiceLogicImpl hostServiceLogicImpl;
63+
64+
@Before
65+
public void setUp() {
66+
when(universeFactory.create(anyString())).thenReturn(new UniverseScope.SelfUniverse());
67+
hostServiceLogicImpl =
68+
Guice.createInjector(BoundFieldModule.of(this)).getInstance(HostServiceLogicImpl.class);
69+
}
70+
71+
@Test
72+
public void getHostHeaderInfo_success() throws Exception {
73+
GetHostHeaderInfoRequest request =
74+
GetHostHeaderInfoRequest.newBuilder().setHostName("host").build();
75+
76+
HostHeaderInfo response = hostServiceLogicImpl.getHostHeaderInfo(request).get();
77+
78+
assertThat(response.getHostName()).isEqualTo("host");
79+
assertThat(response.getActions()).isEqualTo(HostActions.getDefaultInstance());
80+
}
81+
82+
@Test
83+
public void getHostHeaderInfo_invalidUniverse_fails() throws Exception {
84+
GetHostHeaderInfoRequest request =
85+
GetHostHeaderInfoRequest.newBuilder().setHostName("host").setUniverse("invalid").build();
86+
87+
when(universeFactory.create("invalid")).thenThrow(new IllegalArgumentException("invalid"));
88+
89+
ExecutionException e =
90+
assertThrows(
91+
ExecutionException.class, () -> hostServiceLogicImpl.getHostHeaderInfo(request).get());
92+
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
93+
}
94+
}

0 commit comments

Comments
 (0)