1- /*
2- * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org). All Rights Reserved.
3- *
4- * WSO2 LLC. licenses this file to you under the Apache License,
5- * Version 2.0 (the "License"); you may not use this file except
6- * in compliance with the License.
7- * You may obtain a copy of the License at
8- *
9- * http://www.apache.org/licenses/LICENSE-2.0
10- *
11- * Unless required by applicable law or agreed to in writing,
12- * software distributed under the License is distributed on an
13- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14- * KIND, either express or implied. See the License for the
15- * specific language governing permissions and limitations
16- * under the License.
17- */
18-
191package io .ballerina .stdlib .graphql .commons .utils ;
202
213import io .ballerina .stdlib .graphql .commons .types .EnumValue ;
3012import java .util .regex .Matcher ;
3113import java .util .regex .Pattern ;
3214
33- /**
34- * This class includes tests for SDL schema string generation and type ordering.
35- */
3615public class SdlSchemaStringGeneratorTest {
3716
3817 @ Test
3918 public void testSchemaTypeOrdering () {
40- // Create a schema with various types
4119 Schema schema = new Schema (null , false );
4220
43- // Add Query type
4421 Type queryType = schema .addType ("Query" , TypeKind .OBJECT , null );
4522 Type stringType = schema .addType ("String" , TypeKind .SCALAR , null );
4623 queryType .addField (new Field ("greeting" , stringType ));
47- queryType .addField (new Field ("user" , null )); // Will be set later
24+ queryType .addField (new Field ("user" , null ));
4825 schema .setQueryType (queryType );
4926
50- // Add Mutation type
5127 Type mutationType = schema .addType ("Mutation" , TypeKind .OBJECT , null );
52- mutationType .addField (new Field ("createUser" , null )); // Will be set later
28+ mutationType .addField (new Field ("createUser" , null ));
5329 schema .setMutationType (mutationType );
5430
55- // Add Subscription type
5631 Type subscriptionType = schema .addType ("Subscription" , TypeKind .OBJECT , null );
57- subscriptionType .addField (new Field ("userUpdated" , null )); // Will be set later
32+ subscriptionType .addField (new Field ("userUpdated" , null ));
5833 schema .setSubscriptionType (subscriptionType );
5934
60- // Add Interface
6135 Type profileInterface = schema .addType ("Profile" , TypeKind .INTERFACE , null );
6236 profileInterface .addField (new Field ("id" , stringType ));
6337 profileInterface .addField (new Field ("name" , stringType ));
6438
65- // Add Object types
6639 Type userType = schema .addType ("User" , TypeKind .OBJECT , null );
6740 userType .addField (new Field ("id" , stringType ));
6841 userType .addField (new Field ("name" , stringType ));
@@ -73,28 +46,22 @@ public void testSchemaTypeOrdering() {
7346 addressType .addField (new Field ("street" , stringType ));
7447 addressType .addField (new Field ("city" , stringType ));
7548
76- // Add Input type
7749 Type createUserInput = schema .addType ("CreateUserInput" , TypeKind .INPUT_OBJECT , null );
7850 createUserInput .addInputField (new InputValue ("name" , stringType , null , null ));
7951 createUserInput .addInputField (new InputValue ("email" , stringType , null , null ));
8052
81- // Add Enum
8253 Type statusEnum = schema .addType ("Status" , TypeKind .ENUM , null );
8354 statusEnum .addEnumValue (new EnumValue ("ACTIVE" , null ));
8455 statusEnum .addEnumValue (new EnumValue ("INACTIVE" , null ));
8556
86- // Add Union
8757 Type searchResultUnion = schema .addType ("SearchResult" , TypeKind .UNION , null );
8858 searchResultUnion .addPossibleType (userType );
8959 searchResultUnion .addPossibleType (addressType );
9060
91- // Add custom Scalar
9261 Type dateTimeScalar = schema .addType ("DateTime" , TypeKind .SCALAR , null );
9362
94- // Generate the SDL schema string
9563 String sdlSchema = SdlSchemaStringGenerator .generate (schema );
9664
97- // Verify the order: Query, Mutation, Subscription, Interface, Object types, Input, Enum, Union, Scalar
9865 int queryIndex = findTypeIndex (sdlSchema , "type Query" );
9966 int mutationIndex = findTypeIndex (sdlSchema , "type Mutation" );
10067 int subscriptionIndex = findTypeIndex (sdlSchema , "type Subscription" );
@@ -106,7 +73,6 @@ public void testSchemaTypeOrdering() {
10673 int unionIndex = findTypeIndex (sdlSchema , "union SearchResult" );
10774 int scalarIndex = findTypeIndex (sdlSchema , "scalar DateTime" );
10875
109- // Assert the ordering
11076 Assert .assertTrue (queryIndex != -1 , "Query type should be present" );
11177 Assert .assertTrue (mutationIndex != -1 , "Mutation type should be present" );
11278 Assert .assertTrue (subscriptionIndex != -1 , "Subscription type should be present" );
@@ -117,7 +83,6 @@ public void testSchemaTypeOrdering() {
11783 Assert .assertTrue (unionIndex != -1 , "Union type should be present" );
11884 Assert .assertTrue (scalarIndex != -1 , "Scalar type should be present" );
11985
120- // Verify the order is correct
12186 Assert .assertTrue (queryIndex < mutationIndex , "Query should come before Mutation" );
12287 Assert .assertTrue (mutationIndex < subscriptionIndex , "Mutation should come before Subscription" );
12388 Assert .assertTrue (subscriptionIndex < interfaceIndex , "Subscription should come before Interface" );
@@ -132,16 +97,13 @@ public void testSchemaTypeOrdering() {
13297
13398 @ Test
13499 public void testAlphabeticalOrderingWithinSameTypeKind () {
135- // Create a schema with multiple object types
136100 Schema schema = new Schema (null , false );
137101
138- // Add Query type
139102 Type queryType = schema .addType ("Query" , TypeKind .OBJECT , null );
140103 Type stringType = schema .addType ("String" , TypeKind .SCALAR , null );
141104 queryType .addField (new Field ("test" , stringType ));
142105 schema .setQueryType (queryType );
143106
144- // Add multiple object types (not in alphabetical order)
145107 Type zebraType = schema .addType ("Zebra" , TypeKind .OBJECT , null );
146108 zebraType .addField (new Field ("name" , stringType ));
147109
@@ -151,46 +113,32 @@ public void testAlphabeticalOrderingWithinSameTypeKind() {
151113 Type mangoType = schema .addType ("Mango" , TypeKind .OBJECT , null );
152114 mangoType .addField (new Field ("taste" , stringType ));
153115
154- // Generate the SDL schema string
155116 String sdlSchema = SdlSchemaStringGenerator .generate (schema );
156117
157- // Find positions
158118 int appleIndex = findTypeIndex (sdlSchema , "type Apple" );
159119 int mangoIndex = findTypeIndex (sdlSchema , "type Mango" );
160120 int zebraIndex = findTypeIndex (sdlSchema , "type Zebra" );
161121
162- // Verify alphabetical ordering within the same type kind
163122 Assert .assertTrue (appleIndex < mangoIndex , "Apple should come before Mango (alphabetical order)" );
164123 Assert .assertTrue (mangoIndex < zebraIndex , "Mango should come before Zebra (alphabetical order)" );
165124 }
166125
167126 @ Test
168127 public void testMinimalSchemaWithOnlyQuery () {
169- // Create a minimal schema with only Query type
170128 Schema schema = new Schema (null , false );
171129
172130 Type queryType = schema .addType ("Query" , TypeKind .OBJECT , null );
173131 Type stringType = schema .addType ("String" , TypeKind .SCALAR , null );
174132 queryType .addField (new Field ("hello" , stringType ));
175133 schema .setQueryType (queryType );
176134
177- // Generate the SDL schema string
178135 String sdlSchema = SdlSchemaStringGenerator .generate (schema );
179136
180- // Verify Query is present
181137 Assert .assertTrue (sdlSchema .contains ("type Query" ), "Schema should contain Query type" );
182138 Assert .assertTrue (sdlSchema .contains ("hello" ), "Query should have hello field" );
183139 }
184140
185- /**
186- * Helper method to find the index of a type definition in the schema string.
187- *
188- * @param schema the schema string
189- * @param typePattern the pattern to search for (e.g., "type Query", "interface Profile")
190- * @return the index of the type definition, or -1 if not found
191- */
192141 private int findTypeIndex (String schema , String typePattern ) {
193- // Use regex to find the pattern at the start of a line
194142 Pattern pattern = Pattern .compile ("^" + Pattern .quote (typePattern ), Pattern .MULTILINE );
195143 Matcher matcher = pattern .matcher (schema );
196144 if (matcher .find ()) {
0 commit comments