Skip to content

Commit 9004a1c

Browse files
Utils update
1 parent 77b6958 commit 9004a1c

File tree

16 files changed

+151
-204
lines changed

16 files changed

+151
-204
lines changed

.env

Whitespace-only changes.

.github/workflows/sast-scan.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/workflows/secrets-scan.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# CHANGELOG
22

3+
## v1.12.2
4+
5+
### Date: 08-AUG-2023
6+
7+
- Updated Utils SDK to v1.2.3
8+
9+
## v1.12.1
10+
11+
### Date: 07-Jun-2023
12+
13+
- Added Support For Nested Assets
14+
- General Code Improvement
15+
316
## v1.12.0
417

518
### Date: 25-APR-2023

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.contentstack.sdk</groupId>
77
<artifactId>java</artifactId>
8-
<version>1.12.1-SNAPSHOT</version>
8+
<version>1.12.2-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010
<name>contentstack-java</name>
1111
<description>Java SDK for Contentstack Content Delivery API</description>
@@ -22,7 +22,6 @@
2222
<dotenv-source.version>3.0.0</dotenv-source.version>
2323
<rxjava-source.version>3.1.6</rxjava-source.version>
2424
<retrofit-source.version>2.9.0</retrofit-source.version>
25-
<converter-gson-source.version>2.9.0</converter-gson-source.version>
2625
<loggin.version>4.11.0</loggin.version>
2726
<jococo-plugin.version>0.8.5</jococo-plugin.version>
2827
<lombok-source.version>1.18.28</lombok-source.version>
@@ -37,7 +36,7 @@
3736
<json-version>20230227</json-version>
3837
<jacoco-maven-plugin-version>0.8.7</jacoco-maven-plugin-version>
3938
<maven-release-plugin-version>2.5.3</maven-release-plugin-version>
40-
<contentstack-utils-version>1.2.2</contentstack-utils-version>
39+
<contentstack-utils-version>1.2.3</contentstack-utils-version>
4140
</properties>
4241

4342
<parent>
@@ -129,12 +128,13 @@
129128
<version>${retrofit-source.version}</version>
130129
<scope>compile</scope>
131130
</dependency>
131+
<!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson -->
132132
<dependency>
133133
<groupId>com.squareup.retrofit2</groupId>
134134
<artifactId>converter-gson</artifactId>
135-
<version>${converter-gson-source.version}</version>
136-
<scope>compile</scope>
135+
<version>${retrofit-source.version}</version>
137136
</dependency>
137+
138138
<dependency>
139139
<groupId>com.squareup.okhttp3</groupId>
140140
<artifactId>logging-interceptor</artifactId>

src/main/java/com/contentstack/sdk/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class Constants {
2222

2323
private static final Logger logger = Logger.getLogger(Constants.class.getSimpleName());
24-
protected static final String SDK_VERSION = "1.12.1";
24+
protected static final String SDK_VERSION = "1.12.2";
2525
protected static final String ENVIRONMENT = "environment";
2626
protected static final String CONTENT_TYPE_UID = "content_type_uid";
2727
protected static final String ENTRY_UID = "entry_uid";
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.contentstack.sdk;
2+
3+
import io.github.cdimascio.dotenv.Dotenv;
4+
import io.github.cdimascio.dotenv.DotenvException;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.rmi.AccessException;
9+
10+
public class Credentials {
11+
static Dotenv env = getEnv();
12+
13+
/**
14+
* The provided Java code defines a method named `getEnv()` that attempts to load environment variables from a `.env` file using the `Dotenv` library. If loading the environment variables encounters an exception (specifically, a `DotenvException`), the code takes an alternative path by creating an empty `.env` file in the current working directory.
15+
* <p>
16+
* Here's a breakdown of what the code is doing step by step:
17+
* <p>
18+
* 1. The method `public static Dotenv getEnv()` is defined. It returns an instance of the `Dotenv` class, which is used to manage environment variables loaded from the `.env` file.
19+
* <p>
20+
* 2. Inside the `try` block, the code tries to load environment variables using `Dotenv.load()`. If successful, the loaded environment variables are stored in the `env` variable.
21+
* <p>
22+
* 3. If loading the environment variables from the `.env` file encounters an exception (a `DotenvException`), the code enters the `catch` block.
23+
* <p>
24+
* 4. In the `catch` block, it gets the current working directory using `System.getProperty("user.dir")` and creates a `File` object named `envFile` representing the `.env` file in the current directory.
25+
* <p>
26+
* 5. The code attempts to create an empty `.env` file using `envFile.createNewFile()`.
27+
* <p>
28+
* 6. If there's an error during the file creation process (an `IOException`), an error message is printed to the standard error output, and the exception's stack trace is printed for debugging purposes.
29+
* <p>
30+
* 7. Finally, regardless of whether the environment variables were successfully loaded or a new `.env` file was created, the method returns the `env` variable, which may either contain the loaded environment variables or be `null` if an exception occurred during the loading process.
31+
* <p>
32+
* In summary, this code defines a method that attempts to load environment variables from a `.env` file using the `Dotenv` library. If loading fails due to an exception, it creates an empty `.env` file in the current working directory and then returns the `Dotenv` instance, which may or may not have loaded environment variables depending on whether an exception occurred.
33+
*
34+
* @return Dotenv
35+
*/
36+
public static Dotenv getEnv() {
37+
try {
38+
env = Dotenv.load();
39+
} catch (DotenvException ex) {
40+
String currentDirectory = System.getProperty("user.dir");
41+
File envFile = new File(currentDirectory, ".env");
42+
try {
43+
// Create .env file in the current directory
44+
envFile.createNewFile();
45+
} catch (IOException e) {
46+
System.err.println("An error occurred while creating .env file.");
47+
e.printStackTrace();
48+
}
49+
}
50+
return env;
51+
}
52+
53+
54+
public final static String pwd = (env.get("PWD") != null) ? env.get("PWD") : "contentstack-java";
55+
public final static String HOST = (env.get("HOST") != null) ? env.get("HOST") : "cdn.contentstack.io";
56+
public final static String API_KEY = (env.get("API_KEY") != null) ? env.get("API_KEY") : "***REMOVED***";
57+
public final static String DELIVERY_TOKEN = (env.get("DELIVERY_TOKEN") != null) ? env.get("DELIVERY_TOKEN") : "***REMOVED***";
58+
public final static String ENVIRONMENT = (env.get("ENVIRONMENT") != null) ? env.get("ENVIRONMENT") : "env1";
59+
public final static String CONTENT_TYPE = (env.get("contentType") != null) ? env.get("contentType") : "product";
60+
public final static String ENTRY_UID = (env.get("assetUid") != null) ? env.get("assetUid") : "blt884786476373";
61+
62+
private static Stack stack;
63+
64+
private Credentials() throws AccessException {
65+
// Private constructor to prevent direct instantiation
66+
throw new AccessException("Can not access credential access");
67+
}
68+
69+
public static Stack getStack() {
70+
71+
if (stack == null) {
72+
synchronized (Credentials.class) {
73+
if (stack == null) {
74+
try {
75+
Config config = new Config();
76+
config.setHost(HOST);
77+
stack = Contentstack.stack(API_KEY, DELIVERY_TOKEN, ENVIRONMENT, config);
78+
} catch (IllegalAccessException e) {
79+
throw new RuntimeException(e);
80+
}
81+
}
82+
}
83+
}
84+
return stack;
85+
}
86+
87+
}

src/test/java/com/contentstack/sdk/TestAsset.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.contentstack.sdk;
22

3-
import io.github.cdimascio.dotenv.Dotenv;
43
import org.json.JSONObject;
54
import org.junit.jupiter.api.*;
65

@@ -11,21 +10,10 @@
1110
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1211
class TestAsset {
1312

14-
protected String API_KEY, DELIVERY_TOKEN, ENV;
1513
private final Logger logger = Logger.getLogger(TestAsset.class.getName());
1614
private String assetUid;
17-
private Stack stack;
18-
19-
@BeforeAll
20-
public void initBeforeTests() throws IllegalAccessException {
21-
Dotenv dotenv = Dotenv.load();
22-
API_KEY = dotenv.get("API_KEY");
23-
DELIVERY_TOKEN = dotenv.get("DELIVERY_TOKEN");
24-
ENV = dotenv.get("ENVIRONMENT");
25-
Config config = new Config();
26-
config.setHost(dotenv.get("HOST"));
27-
stack = Contentstack.stack(API_KEY, DELIVERY_TOKEN, ENV, config);
28-
}
15+
private Stack stack = Credentials.getStack();
16+
2917

3018
@Test
3119
@Order(1)

src/test/java/com/contentstack/sdk/TestAssetLibrary.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.contentstack.sdk;
22

3-
import io.github.cdimascio.dotenv.Dotenv;
43
import org.junit.jupiter.api.*;
54

65
import java.util.List;
@@ -11,21 +10,9 @@
1110
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1211
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1312
class TestAssetLibrary {
14-
15-
protected String API_KEY, DELIVERY_TOKEN, ENV;
1613
private final Logger logger = Logger.getLogger(TestAssetLibrary.class.getName());
17-
private Stack stack;
14+
private final Stack stack = Credentials.getStack();
1815

19-
@BeforeAll
20-
public void initBeforeTests() throws IllegalAccessException {
21-
Dotenv dotenv = Dotenv.load();
22-
API_KEY = dotenv.get("API_KEY");
23-
DELIVERY_TOKEN = dotenv.get("DELIVERY_TOKEN");
24-
ENV = dotenv.get("ENVIRONMENT");
25-
Config config = new Config();
26-
config.setHost(dotenv.get("HOST"));
27-
stack = Contentstack.stack(API_KEY, DELIVERY_TOKEN, ENV, config);
28-
}
2916

3017
@Test
3118
@Order(1)
@@ -55,7 +42,6 @@ void testAssetSetHeader() {
5542
AssetLibrary assetLibrary = stack.assetLibrary();
5643
assetLibrary.setHeader("headerKey", "headerValue");
5744
Assertions.assertTrue(assetLibrary.headers.containsKey("headerKey"));
58-
logger.info("passed...");
5945
}
6046

6147
@Test
@@ -64,57 +50,49 @@ void testAssetRemoveHeader() {
6450
assetLibrary.setHeader("headerKey", "headerValue");
6551
assetLibrary.removeHeader("headerKey");
6652
Assertions.assertFalse(assetLibrary.headers.containsKey("headerKey"));
67-
logger.info("passed...");
6853
}
6954

7055
@Test
7156
void testAssetSortAscending() {
7257
AssetLibrary assetLibrary = stack.assetLibrary().sort("ascending", AssetLibrary.ORDERBY.ASCENDING);
7358
Assertions.assertFalse(assetLibrary.headers.containsKey("asc"));
74-
logger.info("passed...");
7559
}
7660

7761
@Test
7862
void testAssetSortDescending() {
7963
AssetLibrary assetLibrary = stack.assetLibrary();
8064
assetLibrary.sort("descending", AssetLibrary.ORDERBY.DESCENDING);
8165
Assertions.assertFalse(assetLibrary.headers.containsKey("desc"));
82-
logger.info("passed...");
8366
}
8467

8568
@Test
8669
void testAssetIncludeCount() {
8770
AssetLibrary assetLibrary = stack.assetLibrary().includeCount();
8871
Assertions.assertFalse(assetLibrary.headers.containsKey("include_count"));
89-
logger.info("passed...");
9072
}
9173

9274
@Test
9375
void testAssetIncludeRelativeUrl() {
9476
AssetLibrary assetLibrary = stack.assetLibrary();
9577
assetLibrary.includeRelativeUrl();
9678
Assertions.assertFalse(assetLibrary.headers.containsKey("relative_urls"));
97-
logger.info("passed...");
9879
}
9980

10081
@Test
10182
void testAssetGetCount() {
10283
AssetLibrary assetLibrary = stack.assetLibrary().includeRelativeUrl();
10384
Assertions.assertEquals(0, assetLibrary.getCount());
104-
logger.info("passed...");
10585
}
10686

10787
@Test
10888
void testIncludeFallback() {
10989
AssetLibrary assetLibrary = stack.assetLibrary().includeFallback();
11090
Assertions.assertFalse(assetLibrary.headers.containsKey("include_fallback"));
111-
logger.info("passed...");
11291
}
11392

11493
@Test
11594
void testIncludeOwner() {
11695
AssetLibrary assetLibrary = stack.assetLibrary().includeMetadata();
11796
Assertions.assertFalse(assetLibrary.headers.containsKey("include_owner"));
118-
logger.info("passed...");
11997
}
12098
}

src/test/java/com/contentstack/sdk/TestContentType.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.contentstack.sdk;
22

3-
import io.github.cdimascio.dotenv.Dotenv;
43
import org.json.JSONArray;
54
import org.json.JSONObject;
65
import org.junit.jupiter.api.*;
@@ -11,18 +10,8 @@
1110
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1211
class TestContentType {
1312

14-
protected String API_KEY, DELIVERY_TOKEN, ENV;
1513
private final Logger logger = Logger.getLogger(TestContentType.class.getName());
16-
private Stack stack;
17-
18-
@BeforeAll
19-
public void initBeforeTests() throws IllegalAccessException {
20-
Dotenv dotenv = Dotenv.load();
21-
API_KEY = dotenv.get("API_KEY");
22-
DELIVERY_TOKEN = dotenv.get("DELIVERY_TOKEN");
23-
ENV = dotenv.get("ENVIRONMENT");
24-
stack = Contentstack.stack(API_KEY, DELIVERY_TOKEN, ENV);
25-
}
14+
private final Stack stack = Credentials.getStack();
2615

2716
@Test
2817
@Order(1)

0 commit comments

Comments
 (0)