Skip to content

Commit 6ae960e

Browse files
Ishita-190Adi-204
andauthored
test: add unit tests for modelspresets.js (#1950)
Co-authored-by: Ishita Sati <ishasati19@gmail.com> Co-authored-by: Adi Boghawala <adiboghawala@gmail.com>
1 parent 3d9534e commit 6ae960e

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

packages/helpers/src/ModelsPresets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const websocketJavaPreset = {
1111
// Solution to handle imports dynamically
1212
const requiredImports = new Set();
1313
requiredImports.add('import java.util.Objects;');
14-
Object.values(model.properties).forEach(property => {
14+
Object.values(model.properties || {}).forEach(property => {
1515
const type = property.property.type;
1616

1717
if (type === 'Map<String, Object>') {
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
const { JavaModelsPresets } = require('../src/ModelsPresets');
2+
const { JAVA_COMMON_PRESET } = require('@asyncapi/modelina');
3+
4+
const PACKAGE_DECLARATION = 'package com.asyncapi.models;';
5+
const IMPORT_OBJECTS = 'import java.util.Objects;';
6+
const IMPORT_MAP = 'import java.util.Map;';
7+
const TEST_CLASS_CONTENT = 'public class TestClass {}';
8+
const MAP_STRING_OBJECT_TYPE = 'Map<String, Object>';
9+
const PACKAGE_WITH_NEWLINES = 'package com.asyncapi.models;\n\n';
10+
11+
describe('ModelsPresets', () => {
12+
describe('JavaModelsPresets structure', () => {
13+
it('should export JavaModelsPresets as an array', () => {
14+
expect(Array.isArray(JavaModelsPresets)).toBe(true);
15+
expect(JavaModelsPresets).toHaveLength(2);
16+
});
17+
18+
it('should contain JAVA_COMMON_PRESET with correct options', () => {
19+
const commonPreset = JavaModelsPresets[0];
20+
expect(commonPreset.preset).toBe(JAVA_COMMON_PRESET);
21+
expect(commonPreset.options).toEqual({
22+
equal: true,
23+
hashCode: true,
24+
classToString: true,
25+
marshalling: false
26+
});
27+
});
28+
29+
it('should contain websocketJavaPreset as second element', () => {
30+
const websocketPreset = JavaModelsPresets[1];
31+
expect(websocketPreset).toBeDefined();
32+
expect(websocketPreset.class).toBeDefined();
33+
expect(websocketPreset.enum).toBeDefined();
34+
expect(websocketPreset.union).toBeDefined();
35+
});
36+
});
37+
38+
describe('websocketJavaPreset class.self', () => {
39+
const websocketPreset = JavaModelsPresets[1];
40+
41+
it('should add package and basic imports for class without Map properties', () => {
42+
const mockModel = {
43+
properties: {
44+
prop1: { property: { type: 'String' } },
45+
prop2: { property: { type: 'Integer' } }
46+
}
47+
};
48+
const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel });
49+
50+
expect(result).toContain(PACKAGE_DECLARATION);
51+
expect(result).toContain(IMPORT_OBJECTS);
52+
expect(result).toContain(TEST_CLASS_CONTENT);
53+
expect(result).not.toContain(IMPORT_MAP);
54+
});
55+
56+
it('should add Map import when property type is Map<String, Object>', () => {
57+
const mockModel = {
58+
properties: {
59+
prop1: { property: { type: MAP_STRING_OBJECT_TYPE } },
60+
prop2: { property: { type: 'String' } }
61+
}
62+
};
63+
const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel });
64+
65+
expect(result).toContain(PACKAGE_DECLARATION);
66+
expect(result).toContain(IMPORT_OBJECTS);
67+
expect(result).toContain(IMPORT_MAP);
68+
expect(result).toContain(TEST_CLASS_CONTENT);
69+
});
70+
71+
it('should add Map import only once for multiple Map properties', () => {
72+
const mockModel = {
73+
properties: {
74+
prop1: { property: { type: MAP_STRING_OBJECT_TYPE } },
75+
prop2: { property: { type: MAP_STRING_OBJECT_TYPE } },
76+
prop3: { property: { type: MAP_STRING_OBJECT_TYPE } }
77+
}
78+
};
79+
const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel });
80+
81+
const mapImportCount = (result.match(/import java\.util\.Map;/g) || []).length;
82+
expect(mapImportCount).toBe(1);
83+
});
84+
85+
it('should handle model with no properties', () => {
86+
const mockModel = { properties: {} };
87+
const content = 'public class EmptyClass {}';
88+
const result = websocketPreset.class.self({ content, model: mockModel });
89+
90+
expect(result).toContain(PACKAGE_DECLARATION);
91+
expect(result).toContain(IMPORT_OBJECTS);
92+
expect(result).toContain('public class EmptyClass {}');
93+
});
94+
95+
it('should handle model with missing properties key', () => {
96+
const mockModel = {};
97+
const content = 'public class NoPropsClass {}';
98+
const result = websocketPreset.class.self({ content, model: mockModel });
99+
100+
expect(result).toContain(PACKAGE_DECLARATION);
101+
expect(result).toContain(IMPORT_OBJECTS);
102+
expect(result).toContain('public class NoPropsClass {}');
103+
});
104+
});
105+
106+
describe('websocketJavaPreset enum.self', () => {
107+
const websocketPreset = JavaModelsPresets[1];
108+
109+
it('should add package declaration to enum content', () => {
110+
const content = 'public enum Status { ACTIVE, INACTIVE }';
111+
const result = websocketPreset.enum.self({ content });
112+
113+
expect(result).toBe(`${PACKAGE_WITH_NEWLINES}public enum Status { ACTIVE, INACTIVE }`);
114+
});
115+
116+
it('should handle empty enum content', () => {
117+
const content = '';
118+
const result = websocketPreset.enum.self({ content });
119+
120+
expect(result).toBe(PACKAGE_WITH_NEWLINES);
121+
});
122+
});
123+
124+
describe('websocketJavaPreset union.self', () => {
125+
const websocketPreset = JavaModelsPresets[1];
126+
127+
it('should add package declaration to union content', () => {
128+
const content = 'public class UnionType {}';
129+
const result = websocketPreset.union.self({ content });
130+
131+
expect(result).toBe(`${PACKAGE_WITH_NEWLINES}public class UnionType {}`);
132+
});
133+
134+
it('should handle empty union content', () => {
135+
const content = '';
136+
const result = websocketPreset.union.self({ content });
137+
138+
expect(result).toBe(PACKAGE_WITH_NEWLINES);
139+
});
140+
});
141+
});

0 commit comments

Comments
 (0)