Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
javaazureappconfig
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jalaluddin Shaikh
javaazureappconfig
Commits
6a8de120
Commit
6a8de120
authored
Mar 23, 2020
by
Jalaluddin Shaikh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added JUnit Test Cases.
parent
fdc00389
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
174 additions
and
36 deletions
+174
-36
JavaAzureAppConfigApplication.java
src/main/java/com/example/JavaAzureAppConfigApplication.java
+3
-36
AzureAppConfigService.java
src/main/java/com/example/service/AzureAppConfigService.java
+112
-0
AzureAppConfigServiceTest.java
...t/java/com/example/service/AzureAppConfigServiceTest.java
+59
-0
No files found.
src/main/java/com/example/JavaAzureAppConfigApplication.java
View file @
6a8de120
...
...
@@ -2,47 +2,14 @@ package com.example;
import
com.azure.data.appconfiguration.ConfigurationClient
;
import
com.azure.data.appconfiguration.ConfigurationClientBuilder
;
import
com.azure.data.appconfiguration.models.ConfigurationSetting
;
import
com.azure.data.appconfiguration.models.SettingSelector
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.example.service.AzureAppConfigService
;
public
class
JavaAzureAppConfigApplication
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
JavaAzureAppConfigApplication
.
class
);
private
final
ConfigurationClient
configurationClient
;
private
final
SettingSelector
settingSelector
;
public
static
void
main
(
String
[]
args
)
{
new
JavaAzureAppConfigApplication
().
azureAppConfiguration
();
}
private
void
azureAppConfiguration
()
{
try
{
LOGGER
.
info
(
"Read Single Value by key"
);
ConfigurationSetting
configurationSetting
=
configurationClient
.
getConfigurationSetting
(
"config.message"
,
null
);
LOGGER
.
info
(
"Message: {}"
,
configurationSetting
.
getValue
());
LOGGER
.
info
(
"Writing Single Value by key"
);
configurationSetting
=
configurationClient
.
setConfigurationSetting
(
"config.datasource"
,
null
,
"Data Source URL"
);
LOGGER
.
info
(
"Data Source URL From Application: {}"
,
configurationSetting
.
getValue
());
LOGGER
.
info
(
"Read All Values..."
);
configurationClient
.
listConfigurationSettings
(
settingSelector
).
forEach
(
property
->
LOGGER
.
info
(
"Key: {} \t Value: {}"
,
property
.
getKey
(),
property
.
getValue
()));
LOGGER
.
info
(
"Remove Single Value by key"
);
configurationClient
.
deleteConfigurationSetting
(
"config.datasource"
,
null
);
LOGGER
.
info
(
"After Removing Key-Value Read All Values..."
);
configurationClient
.
listConfigurationSettings
(
settingSelector
).
forEach
(
property
->
LOGGER
.
info
(
"Key: {} \t Value: {}"
,
property
.
getKey
(),
property
.
getValue
()));
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
e
.
getMessage
(),
e
);
}
}
{
String
app_configuration_connection_string
=
System
.
getenv
(
"APP_CONFIGURATION_CONNECTION_STRING"
);
configurationClient
=
new
ConfigurationClientBuilder
().
connectionString
(
app_configuration_connection_string
).
buildClient
();
settingSelector
=
new
SettingSelector
();
ConfigurationClient
configurationClient
=
new
ConfigurationClientBuilder
().
connectionString
(
app_configuration_connection_string
).
buildClient
();
new
AzureAppConfigService
(
configurationClient
,
new
SettingSelector
()).
azureAppConfiguration
();
}
}
src/main/java/com/example/service/AzureAppConfigService.java
0 → 100644
View file @
6a8de120
package
com
.
example
.
service
;
import
com.azure.core.http.rest.PagedIterable
;
import
com.azure.data.appconfiguration.ConfigurationClient
;
import
com.azure.data.appconfiguration.models.ConfigurationSetting
;
import
com.azure.data.appconfiguration.models.SettingSelector
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* This Class is used to fetch from Azure Cloud App Configuration or set properties to Azure Cloud App Configuration
* Before running we need to set Connection String into ENV variable, so that our application will connect to Azure Cloud Portal
*/
public
class
AzureAppConfigService
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
AzureAppConfigService
.
class
);
private
final
ConfigurationClient
configurationClient
;
private
final
SettingSelector
settingSelector
;
public
AzureAppConfigService
(
ConfigurationClient
configurationClient
,
SettingSelector
settingSelector
)
{
this
.
configurationClient
=
configurationClient
;
this
.
settingSelector
=
settingSelector
;
}
/**
* To Fetch Value based on Key from Azure App Configuration
* @param key String
* @return the value based on key
*/
public
String
getValueByKey
(
String
key
)
{
try
{
LOGGER
.
info
(
"Read Single Value by key"
);
ConfigurationSetting
configurationSetting
=
configurationClient
.
getConfigurationSetting
(
key
,
null
);
return
configurationSetting
.
getValue
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
e
.
getMessage
());
return
null
;
}
}
/**
* To Send Value with respect to Key to Azure App Configuration
* @param key String
* @param value String
* @return the value based on key
*/
public
String
writeValueByKey
(
String
key
,
String
value
)
{
try
{
LOGGER
.
info
(
"Writing Single Value by key"
);
ConfigurationSetting
configurationSetting
=
configurationClient
.
setConfigurationSetting
(
key
,
null
,
value
);
return
configurationSetting
.
getValue
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
e
.
getMessage
());
return
null
;
}
}
/**
* To Fetch All Values from Azure App Configuration
* @return all values
*/
public
PagedIterable
<
ConfigurationSetting
>
readAllKeyValue
()
{
LOGGER
.
info
(
"Read All Values..."
);
try
{
return
configurationClient
.
listConfigurationSettings
(
settingSelector
);
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
e
.
getMessage
());
return
null
;
}
}
/**
* To Delete Value based on Key from Azure App Configuration
* @param key String
* @return the value based on key
*/
public
String
deleteValueByKey
(
String
key
)
{
try
{
LOGGER
.
info
(
"Remove Single Value by key"
);
ConfigurationSetting
configurationSetting
=
configurationClient
.
deleteConfigurationSetting
(
key
,
null
);
return
configurationSetting
.
getValue
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
e
.
getMessage
());
return
null
;
}
}
/**
* Execute All Operation
*/
public
void
azureAppConfiguration
()
{
try
{
String
readValueByKey
=
getValueByKey
(
"config.message"
);
LOGGER
.
info
(
"Message: {}"
,
readValueByKey
);
String
writeValueByKey
=
writeValueByKey
(
"config.datasource"
,
"Data Source URL"
);
LOGGER
.
info
(
"Data Source URL From Application: {}"
,
writeValueByKey
);
PagedIterable
<
ConfigurationSetting
>
readAllKeyValue
=
readAllKeyValue
();
readAllKeyValue
.
forEach
(
property
->
LOGGER
.
info
(
"Key: {} \t Value: {}"
,
property
.
getKey
(),
property
.
getValue
()));
String
deleteValueByKey
=
deleteValueByKey
(
"config.datasource"
);
LOGGER
.
warn
(
"[Key: {} \t Value: {}] has been removed successfully"
,
"config.datasource"
,
deleteValueByKey
);
LOGGER
.
info
(
"After Removing Key-Value Read All Values..."
);
readAllKeyValue
=
readAllKeyValue
();
readAllKeyValue
.
forEach
(
property
->
LOGGER
.
info
(
"Key: {} \t Value: {}"
,
property
.
getKey
(),
property
.
getValue
()));
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
e
.
getMessage
(),
e
);
}
}
}
src/test/java/com/example/service/AzureAppConfigServiceTest.java
0 → 100644
View file @
6a8de120
package
com
.
example
.
service
;
import
com.azure.data.appconfiguration.ConfigurationClient
;
import
com.azure.data.appconfiguration.ConfigurationClientBuilder
;
import
com.azure.data.appconfiguration.models.SettingSelector
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
public
class
AzureAppConfigServiceTest
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
AzureAppConfigServiceTest
.
class
);
AzureAppConfigService
azureAppConfigService
;
@Before
public
void
setUp
()
{
String
app_configuration_connection_string
=
System
.
getenv
(
"APP_CONFIGURATION_CONNECTION_STRING"
);
ConfigurationClient
configurationClient
=
new
ConfigurationClientBuilder
().
connectionString
(
app_configuration_connection_string
).
buildClient
();
azureAppConfigService
=
new
AzureAppConfigService
(
configurationClient
,
new
SettingSelector
());
}
@Test
public
void
getValueByKey
()
{
String
valueByKey
=
azureAppConfigService
.
getValueByKey
(
"config.message"
);
LOGGER
.
info
(
valueByKey
);
assertNotNull
(
valueByKey
);
assertEquals
(
"This is Azure App Config"
,
valueByKey
);
}
@Test
public
void
writeValueByKey
()
{
String
writeValueByKey
=
azureAppConfigService
.
writeValueByKey
(
"config.dataSource"
,
"Data Source URL"
);
LOGGER
.
info
(
writeValueByKey
);
assertNotNull
(
writeValueByKey
);
assertEquals
(
"Data Source URL"
,
writeValueByKey
);
}
@Test
public
void
readAllKeyValue
()
{
long
count
=
azureAppConfigService
.
readAllKeyValue
().
stream
().
count
();
assertEquals
(
3
,
count
);
}
@Test
public
void
deleteValueByKey
()
{
String
valueByKey
=
azureAppConfigService
.
deleteValueByKey
(
"config.dataSource"
);
LOGGER
.
info
(
valueByKey
);
assertNotNull
(
valueByKey
);
assertEquals
(
"Data Source URL"
,
valueByKey
);
}
@Test
public
void
azureAppConfiguration
()
{
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment