Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JunitTestSample #137

Merged
merged 16 commits into from
Nov 15, 2022
2 changes: 0 additions & 2 deletions agent/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
id 'java'
id 'org.springframework.boot'
}

Expand Down Expand Up @@ -29,7 +28,6 @@ tasks.withType(JavaCompile) {
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'org.mockito:mockito-core:3.12.4'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootWebVersion
testCompile 'me.paulschwarz:spring-dotenv:2.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ public DeviceManager initDeviceManager(BlobStorageClient deviceLabBlobClient, AD

AgentType agentType = AgentType.formAgentType(agentTypeValue);
DeviceManager deviceManager = agentType.getManager();
if (deviceManager instanceof AndroidDeviceManager)
if (deviceManager instanceof AndroidDeviceManager) {
((AndroidDeviceManager) deviceManager).setADBOperateUtil(adbOperateUtil);
}
if (StringUtils.isNotBlank(adbServerHost)) {
logger.info("Setting the adb server hostname to {}", adbServerHost);
adbOperateUtil.setAdbServerHost(adbServerHost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.File;

@Service
@Data
@Slf4j
public class MetricUtil {
Expand Down
82 changes: 82 additions & 0 deletions agent/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
server:
port: 8886

spring:
application:
name: ${app.registry.name:device-network-agent}
output:
ansi:
enabled: always
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 2048MB
datasource:
url: jdbc:sqlite:${app.location}/hydra_lab_agent_db.sqlite
username: sqlite
password: 98765432
driver-class-name: org.sqlite.JDBC
hikari:
maximum-pool-size: 1
jpa:
database-platform: org.sqlite.hibernate.dialect.SQLiteDialect
show-sql: false
hibernate:
dialect: org.sqlite.hibernate.dialect.SQLiteDialect
ddl-auto: update
properties:
hibernate:
format_sql: false
order_inserts: true
order_updates: true
jdbc:
batch_size: 400
batch_versioned_data: true

logging:
config: classpath:logback-common.xml
level:
org.hibernate.SQL: INFO

app:
# register to Hydra Lab Center
registry:
# The url of Hydra Lab Center. If nginx enabled, switch to port of nginx
server: "localhost:9886"
# The Agent info registered in Hydra Lab Center
id: ${AGENT_ID}
secret: ${AGENT_SECRET}
name: ${AGENT_NAME:default}
# Agent Type {1 : 1*WINDOWS + n*ANDROIDS , 2 : 1*WINDOWS+1*ANDROID , 3 : iOS}
agent-type: ${AGENT_TYPE:1}
# config Azure Blob
blob:
connection: ${BLOB_CONNECTION_STR:@null}
fileLimitDay: 6
CDNUrl: ${CDN_URL:}
# Device Stability Monitor Configuration
device:
state-change:
count-threshold: ${STATE_CHANGE_COUNT_THRESHOLD:12}
window-time: ${STATE_CHANGE_WINDOW_TIME:5}
recovery-time: ${STATE_RECOVERY_TIME:3}
location: ${user.dir}
# Prometheus Configuration
management:
endpoints:
web:
exposure:
include: prometheus
health:
show-details: always
metrics:
tags:
application: ${spring.application.name}
metrics:
export:
prometheus:
pushgateway:
base-url: ${app.registry.server}/prometheus/pushgateway
enabled: false
job: ${spring.application.name}
push-rate: 10s
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.microsoft.hydralab.agent.service;

import com.microsoft.hydralab.agent.runner.espresso.EspressoRunner;
import com.microsoft.hydralab.agent.test.BaseTest;
import com.microsoft.hydralab.common.entity.center.TestTaskSpec;
import com.microsoft.hydralab.common.entity.common.TestTask;
import com.microsoft.hydralab.common.management.DeviceManager;
import com.microsoft.hydralab.common.management.impl.AndroidDeviceManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.annotation.Resource;

public class DeviceControlServiceTest extends BaseTest {

@Resource
DeviceControlService deviceControlService;
@Resource
DeviceManager deviceManager;
@MockBean
EspressoRunner espressoRunner;


@Test
public void getAllConnectedDevice() {

}

@Test
public void cancelTestTaskById() {
}

@Test
public void runTestTask() {
TestTaskSpec taskSpec = new TestTaskSpec();
taskSpec.runningType = TestTask.TestRunningType.INSTRUMENTATION;
TestTask testTask = deviceControlService.runTestTask(taskSpec);
}

@Test
public void getDeviceManager() {
baseLogger.info(String.valueOf(deviceManager instanceof AndroidDeviceManager));
Assertions.assertTrue(deviceManager instanceof AndroidDeviceManager, "Init DeviceManager Bean Error!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package com.microsoft.hydralab.agent.test;

import com.microsoft.hydralab.agent.service.AgentWebSocketClientService;
import com.microsoft.hydralab.agent.socket.AgentWebSocketClient;
import com.microsoft.hydralab.agent.util.MetricUtil;
import com.microsoft.hydralab.common.util.blob.BlobStorageClient;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.transaction.Transactional;

/**
* @author zhoule
* @date 11/10/2022
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@Transactional
@Rollback
@Disabled
public class BaseTest {
protected Logger baseLogger = LoggerFactory.getLogger(BaseTest.class);
@MockBean
BlobStorageClient blobStorageClient;
@MockBean
AgentWebSocketClient AgentWebSocketClient;
@MockBean
AgentWebSocketClientService agentWebSocketClientService;
@MockBean
@Qualifier("getMetricUtil")
MetricUtil metricUtil;
}
19 changes: 19 additions & 0 deletions azure-pipelines-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ stages:
(Get-Content center/src/main/resources/version.properties) -Replace '0.0.0', $hydraVersion| Set-Content center/src/main/resources/version.properties
workingDirectory: '$(Build.Repository.LocalPath)'
condition: and(succeeded(), eq(variables.fullBuild, 'true'), contains(variables['Build.SourceBranch'], 'Release/'))
- task: Gradle@3
displayName: Run JUnit Test
inputs:
gradleWrapperFile: 'gradlew'
tasks: 'test jacocoRootReport'
publishJUnitResults: true
testResultsFiles: '**/TEST-*.xml'
#codeCoverageToolOption: 'JaCoCo'
#codeCoverageClassFilesDirectories: 'build/classes/java/main'
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.11'
sonarQubeRunAnalysis: false
spotBugsAnalysis: false
- task: PublishCodeCoverageResults@1
displayName: Publich Code Coverage
inputs:
codeCoverageTool: 'JaCoCo'
summaryFileLocation: 'build/reports/jacoco/jacocoRootReport/*.xml'
reportDirectory: 'build/reports/jacoco/jacocoRootReport/html'
- task: Gradle@2
displayName: Build center
inputs:
Expand Down
50 changes: 48 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
apply from: "common.gradle"

apply plugin: "io.freefair.lombok"
apply plugin: "jacoco"
jacoco {
toolVersion = "0.8.8"
reportsDir = file('build/reports/jacoco')
}
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
/*
gradle.projectsEvaluated {
tasks.withType(JacocoCoverageVerification) {
violationRules {
rule {
limit {
minimum = 0.5
}
}
}
}
}
*/
}
buildscript {
repositories {
mavenLocal()
Expand Down Expand Up @@ -30,4 +59,21 @@ allprojects {
}
}
}
apply plugin: "io.freefair.lombok"

task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'
dependsOn(subprojects.test)

def projects = ['center', 'common', 'agent', 'sdk', 'gradle_plugin', 'T2C_Runner']
def jacocoProjects = subprojects.findAll { projects.contains(it.getName()) }

additionalSourceDirs.from = files(jacocoProjects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(jacocoProjects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(jacocoProjects.sourceSets.main.output)
executionData.from = files(jacocoProjects.jacocoTestReport.executionData)

reports {
html.enabled true
xml.enabled true
}
}
1 change: 0 additions & 1 deletion center/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ tasks.withType(JavaCompile) {
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'org.mockito:mockito-core:3.12.4'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootWebVersion
testCompile 'me.paulschwarz:spring-dotenv:2.3.0'
Expand Down
74 changes: 74 additions & 0 deletions center/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
server:
port: 9886
compression:
enabled: true
min-response-size: 102400
spring:
application:
name: device-network-center
output:
ansi:
enabled: always
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 2048MB
datasource:
url: jdbc:sqlite:hydra_lab_center_db.sqlite
username: sqlite
password: 98765432
driver-class-name: org.sqlite.JDBC
jpa:
database-platform: org.sqlite.hibernate.dialect.SQLiteDialect
show-sql: false
hibernate:
dialect: org.sqlite.hibernate.dialect.SQLiteDialect
ddl-auto: update
properties:
hibernate:
format_sql: false
order_inserts: true
order_updates: true
jdbc:
batch_size: 400
batch_versioned_data: true
security:
oauth2:
enabled: false

# log file split
logging:
config: classpath:logback-common.xml
level:
org.hibernate.SQL: INFO

app:
access-token-limit: 2
# Mail Address Format
default-user: 'test@test.com'
blob:
connection: ''
fileLimitDay: 6
CDNUrl: ${CDN_URL:@null}
SASExpiryTimeFont: ${BLOB_SAS_EXPIRY_FONT:120}
SASExpiryTimeAgent: ${BLOB_SAS_EXPIRY_AGENT:120}
SASExpiryUpdate: ${BLOB_SAS_EXPIRY_UPDATE:10}

management:
endpoints:
web:
exposure:
include: prometheus
health:
show-details: always
metrics:
tags:
application: ${spring.application.name}
metrics:
export:
prometheus:
pushgateway:
enabled: ${PUSHGATEWAY_ENABLED:false}
base-url: http://localhost:9091
job: ${spring.application.name}
push-rate: 10s
Loading